[system] / trunk / pg / macros / PGcommonFunctions.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/PGcommonFunctions.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3305 - (download) (as text) (annotate)
Tue Jun 21 19:56:00 2005 UTC (14 years, 8 months ago) by jj
File size: 4308 byte(s)
New context which allows students to use C(n,r) and P(n,r) in their
answers.  To support this, the webwork versions of these functions
have been moved from PGaux... to PGcommon...

    1 ##############################################
    2 #
    3 #  Implements functions that are common to
    4 #  the new Parser.pm and the old PGauxiliaryFunctions.pl
    5 #
    6 
    7 sub _PGcommonFunctions_init {}
    8 
    9 #
   10 #  Make these interact nicely with Parser.pm
   11 #
   12 package CommonFunction;
   13 
   14 #
   15 #  Either call Parser (if it has been loaded) or
   16 #  the functions below.  (If it's ever the case
   17 #  that both the Parser and PGauxiliaryFunctions.pl are
   18 #  both preloaded, then there will be no need for
   19 #  this, as you can always use the Parser versions.
   20 #  We only need this because Parser might not be loaded.)
   21 #
   22 
   23 sub Call {
   24   my $self = shift;
   25   my $fn = shift;
   26   if ($main::_parser_loaded) {
   27     return Parser::Function->call($fn,@_)
   28       if Parser::Context->current->{functions}{$fn};
   29   }
   30   return $self->$fn(@_);
   31 }
   32 
   33 sub log {CORE::log($_[1])}
   34 sub ln {CORE::log($_[1])}
   35 sub logten {CORE::log($_[1])/CORE::log(10)}
   36 
   37 sub tan {CORE::sin($_[1])/CORE::cos($_[1])}
   38 sub cot {CORE::cos($_[1])/CORE::sin($_[1])}
   39 sub sec {1/CORE::cos($_[1])}
   40 sub csc {1/CORE::sin($_[1])}
   41 
   42 sub asin {CORE::atan2($_[1],CORE::sqrt(1-$_[1]*$_[1]))}
   43 sub acos {CORE::atan2(CORE::sqrt(1-$_[1]*$_[1]),$_[1])}
   44 sub atan {CORE::atan2($_[1],1)}
   45 sub acot {CORE::atan2(1,$_[1])}
   46 sub asec {acos($_[0],1.0/$_[1])}
   47 sub acsc {asin($_[0],1.0/$_[1])}
   48 
   49 sub sinh {(CORE::exp($_[1])-CORE::exp(-$_[1]))/2}
   50 sub cosh {(CORE::CORE::exp($_[1])+CORE::CORE::exp(-$_[1]))/2}
   51 sub tanh {(CORE::exp($_[1])-CORE::exp(-$_[1]))/(CORE::exp($_[1])+CORE::exp(-$_[1]))}
   52 sub sech {2/(CORE::exp($_[1])+CORE::exp(-$_[1]))}
   53 sub csch {2.0/(CORE::exp($_[1])-CORE::exp(-$_[1]))}
   54 sub coth {(CORE::exp($_[1])+CORE::exp(-$_[1]))/(CORE::exp($_[1])-CORE::exp(-$_[1]))}
   55 
   56 sub asinh {CORE::log($_[1]+CORE::sqrt($_[1]*$_[1]+1.0))}
   57 sub acosh {CORE::log($_[1]+CORE::sqrt($_[1]*$_[1]-1.0))}
   58 sub atanh {CORE::log((1.0+$_[1])/(1.0-$_[1]))/2.0}
   59 sub asech {CORE::log((1.0+CORE::sqrt(1-$_[1]*$_[1]))/$_[1])}
   60 sub acsch {CORE::log((1.0+CORE::sqrt(1+$_[1]*$_[1]))/$_[1])}
   61 sub acoth {CORE::log(($_[1]+1.0)/($_[1]-1.0))/2.0}
   62 
   63 sub sgn {$_[1] <=> 0}
   64 
   65 #our @ISA = qw(Parser::Function::numeric2);
   66 sub C {
   67   shift; my ($n,$r) = @_; my $C = 1;
   68   return (0) if($r>$n);
   69   $r = $n-$r if ($r > $n-$r); # find the smaller of the two
   70   for (1..$r) {$C = ($C*($n-$_+1))/$_}
   71   return $C
   72 }
   73 
   74 sub P {
   75   shift; my ($n,$r) = @_; my $P = 1;
   76   return (0) if($r>$n);
   77   for (1..$r) {$P *= ($n-$_+1)}
   78   return $P
   79 }
   80 
   81 
   82 #
   83 #  Back to main package
   84 #
   85 package main;
   86 
   87 #
   88 #  Make main versions call the checker to see
   89 #  which package-specific version to call
   90 #
   91 
   92 sub ln {CommonFunction->Call('log',@_)}
   93 sub logten {CommonFunction->Call('logten',@_)}
   94 
   95 sub tan {CommonFunction->Call('tan',@_)}
   96 sub cot {CommonFunction->Call('cot',@_)}
   97 sub sec {CommonFunction->Call('sec',@_)}
   98 sub csc {CommonFunction->Call('csc',@_)}
   99 
  100 sub arcsin {CommonFunction->Call('asin',@_)}; sub asin {CommonFunction->Call('asin',@_)}
  101 sub arccos {CommonFunction->Call('acos',@_)}; sub acos {CommonFunction->Call('acos',@_)}
  102 sub arctan {CommonFunction->Call('atan',@_)}; sub atan {CommonFunction->Call('atan',@_)}
  103 sub arccot {CommonFunction->Call('acot',@_)}; sub acot {CommonFunction->Call('acot',@_)}
  104 sub arcsec {CommonFunction->Call('asec',@_)}; sub asec {CommonFunction->Call('asec',@_)}
  105 sub arccsc {CommonFunction->Call('acsc',@_)}; sub acsc {CommonFunction->Call('acsc',@_)}
  106 
  107 sub sinh {CommonFunction->Call('sinh',@_)}
  108 sub cosh {CommonFunction->Call('cosh',@_)}
  109 sub tanh {CommonFunction->Call('tanh',@_)}
  110 sub sech {CommonFunction->Call('sech',@_)}
  111 sub csch {CommonFunction->Call('csch',@_)}
  112 sub coth {CommonFunction->Call('coth',@_)}
  113 
  114 sub arcsinh {CommonFunction->Call('asinh',@_)}; sub asinh {CommonFunction->Call('asinh',@_)}
  115 sub arccosh {CommonFunction->Call('acosh',@_)}; sub acosh {CommonFunction->Call('acosh',@_)}
  116 sub arctanh {CommonFunction->Call('atanh',@_)}; sub atanh {CommonFunction->Call('atanh',@_)}
  117 sub arcsech {CommonFunction->Call('asech',@_)}; sub asech {CommonFunction->Call('asech',@_)}
  118 sub arccsch {CommonFunction->Call('acsch',@_)}; sub acsch {CommonFunction->Call('acsch',@_)}
  119 sub arccoth {CommonFunction->Call('acoth',@_)}; sub acoth {CommonFunction->Call('acoth',@_)}
  120 
  121 sub sgn {CommonFunction->Call('sgn',@_)}
  122 
  123 sub C {CommonFunction->Call('C', @_)}
  124 sub P {CommonFunction->Call('P', @_)}
  125 sub Comb {CommonFunction->Call('C', @_)}
  126 sub Perm {CommonFunction->Call('P', @_)}
  127 
  128 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9