[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 11 - (download) (as text) (annotate)
Mon Jun 18 15:21:51 2001 UTC (11 years, 11 months ago) by sam
File size: 4145 byte(s)
another setup script test (changed #! lines)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9