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