[system] / trunk / webwork / system / courseScripts / PGauxiliaryFunctions.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/courseScripts/PGauxiliaryFunctions.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 38 - (download) (as text) (annotate)
Thu Jun 21 01:07:21 2001 UTC (12 years ago) by maria
File size: 4398 byte(s)
added back the fact (factorial) function that has been accidentally deleted

    1 #!/usr/local/bin/webwork-perl
    2 sub _PGauxiliaryFunctions_init {
    3 
    4 }
    5 
    6 sub tan {
    7     sin($_[0])/cos($_[0]);
    8 }
    9 sub cot {
   10     cos($_[0])/sin($_[0]);
   11 }
   12 sub sec {
   13     1/cos($_[0]);
   14 }
   15 sub csc {
   16   1/sin($_[0]);
   17 }
   18 sub ln {
   19     log($_[0]);
   20 }
   21 sub logten {
   22     log($_[0])/log(10);
   23 }
   24 sub arcsin {
   25     atan2 ($_[0],sqrt(1-$_[0]*$_[0]));
   26 }
   27 sub asin {
   28     atan2 ($_[0],sqrt(1-$_[0]*$_[0]));
   29 }
   30 sub arccos {
   31     atan2 (sqrt(1-$_[0]*$_[0]),$_[0]);
   32 }
   33 sub acos {
   34     atan2 (sqrt(1-$_[0]*$_[0]),$_[0]);
   35 }
   36 sub arctan {
   37     atan2($_[0],1);
   38 }
   39 sub atan {
   40     atan2($_[0],1);
   41 }
   42 sub arccot {
   43     atan2(1,$_[0]);
   44 }
   45 sub acot {
   46     atan2(1,$_[0]);
   47 }
   48 sub sinh {
   49     (exp($_[0]) - exp(-$_[0]))/2;
   50 }
   51 sub cosh {
   52     (exp($_[0]) + exp(-$_[0]))/2;
   53 }
   54 sub tanh {
   55     (exp($_[0]) - exp(-$_[0]))/(exp($_[0]) + exp(-$_[0]));
   56 }
   57 sub sech {
   58     2/(exp($_[0]) + exp(-$_[0]));
   59 }
   60 sub sgn {
   61   my $x = shift;
   62   my $out;
   63   $out = 1 if $x > 0;
   64   $out = 0 if $x == 0;
   65   $out = -1 if $x<0;
   66   $out;
   67 }
   68 sub step {     # heavyside function (1 or x>0)
   69   my $x = shift;
   70   ($x > 0 ) ? 1 : 0;
   71 }
   72 sub ceil {
   73   my $x = shift;
   74   - floor(-$x);
   75 }
   76 sub floor {
   77   my $input = shift;
   78   my $out = int $input;
   79   $out -- if ( $out <= 0 and $out-$input);  # does the right thing for negative numbers
   80   $out;
   81 }
   82 
   83 sub max {
   84 
   85         my $maxVal = shift;
   86         my @input = @_;
   87 
   88         foreach my $num (@input) {
   89                 $maxVal = $num if ($maxVal < $num);
   90         }
   91 
   92         $maxVal;
   93 
   94 }
   95 
   96 sub min {
   97 
   98         my $minVal = shift;
   99         my @input = @_;
  100 
  101         foreach my $num (@input) {
  102                 $minVal = $num if ($minVal > $num);
  103         }
  104 
  105         $minVal;
  106 
  107 }
  108 
  109 #round added 6/12/2000 by David Etlinger
  110 sub round {
  111   my $input = shift;
  112   my $out;
  113   if( $input >= 0 ) {
  114     $out = int ($input + .5);
  115   }
  116   else {
  117     $out = ceil($input - .5);
  118   }
  119   $out;
  120 }
  121 
  122 #least common multiple
  123 #VS 6/29/2000
  124 sub lcm {
  125   my $a = shift;
  126   my $b = shift;
  127 
  128   #reorder such that $a is the smaller number
  129   if ($a > $b) {
  130     my $temp = $a;
  131     $a = $b;
  132     $b = $temp;
  133   }
  134 
  135   my $lcm = 0;
  136   my $curr = $b;;
  137 
  138   while($lcm == 0) {
  139     $lcm = $curr if ($curr % $a == 0);
  140     $curr += $b;
  141   }
  142 
  143   $lcm;
  144 
  145 }
  146 
  147 
  148 # greatest common factor
  149 # takes in two scalar values and uses the Euclidean Algorithm to return the gcf
  150 #VS 6/29/2000
  151 
  152 sub gcf {
  153         my $a = abs(shift); # absolute values because this will yield the same gcd,
  154         my $b = abs(shift); # but allows use of the mod operation
  155 
  156   # reorder such that a is the smaller number
  157   if ($a < $b) {
  158     my $temp = $a;
  159     $a = $b;
  160     $b = $temp;
  161   }
  162 
  163   my $q = int($a/$b); # quotient
  164   my $r = $a % $b;  # remainder
  165 
  166   return $b if $r == 0;
  167 
  168   my $tempR = $r;
  169 
  170   while ($r != 0) {
  171 
  172     #keep track of what $r was in the last loop, as this is the value
  173     #we will want when $r is set to 0
  174     $tempR = $r;
  175 
  176     $a = $b;
  177     $b = $r;
  178     $q = $a/$b;
  179     $r = $a % $b;
  180 
  181   }
  182 
  183   $tempR;
  184 }
  185 
  186 
  187 #greatest common factor.
  188 #same as gcf, but both names are sufficiently common names
  189 sub gcd {
  190         return gcf($_[0], $_[1]);
  191 }
  192 
  193 #returns 1 for a prime number, else 0
  194 #VS 6/30/2000
  195 sub isPrime {
  196         my $num = shift;
  197         return 1 if ($num == 2 or $num == 3);
  198         return 0 if ($num == 1 or $num == 0);
  199         for (my $i = 3; $i <= $num/2; $i++) { return 0 if ($num % $i == 0); }
  200         return 1;
  201 }
  202 
  203 #reduces a fraction, returning an array containing ($numerator, $denominator)
  204 #VS 7/10/2000
  205 sub reduce {
  206 
  207   my $num = shift;
  208   my $denom = shift;
  209   my $gcd = gcd($num, $denom);
  210 
  211   $num = $num/$gcd;
  212   $denom = $denom/$gcd;
  213 
  214   # formats such that only the numerator will be negative
  215   if ($num/$denom < 0) {$num = -abs($num); $denom = abs($denom);}
  216   else {$num = abs($num); $denom = abs($denom);}
  217 
  218   my @frac = ($num, $denom);
  219   @frac;
  220 }
  221 
  222 
  223 # takes a number and fixed object, as in "$a x" and formats
  224 # to account for when $a = 0, 1, -1
  225 # Usage: format($scalar, "quoted string");
  226 # Example: format(-1, "\pi") returns "-\pi"
  227 # VS 8/1/2000  -  slight adaption of code from T. Shemanske of Dartmouth College
  228 sub preformat {
  229   my $num = shift;
  230   my $obj = shift;
  231   my $out;
  232 
  233 
  234   if ($num == 0) { return 0; }
  235   elsif ($num == 1) { return $obj; }
  236   elsif ($num == -1) { return "-".$obj; }
  237 
  238   return $num.$obj;
  239 }
  240 
  241 
  242 #factorial
  243 
  244 sub fact {
  245   my $num = shift;
  246   my $prod = 1;
  247   if ($num != int($num) or $num < 0) {
  248     warn 'Non-negative integer required.';
  249     return;
  250     }
  251 
  252         for (1..$num) { $prod *= $_ }
  253 
  254   return $prod;
  255 }
  256 # return 1 so that this file can be included with require
  257 1

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9