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

Annotation of /trunk/pg/macros/PGauxiliaryFunctions.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1080 - (view) (download) (as text)

1 : apizer 1080
2 : sh002i 1050 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) > 0 ); # does the right thing for negative numbers
80 :     $out;
81 :     }
82 :    
83 :     sub max {
84 :    
85 :     my $maxVal = shift;
86 :     my @input = @_;
87 : apizer 1080
88 : sh002i 1050 foreach my $num (@input) {
89 :     $maxVal = $num if ($maxVal < $num);
90 :     }
91 : apizer 1080
92 : sh002i 1050 $maxVal;
93 :    
94 : apizer 1080 }
95 : sh002i 1050
96 :     sub min {
97 :    
98 :     my $minVal = shift;
99 :     my @input = @_;
100 : apizer 1080
101 : sh002i 1050 foreach my $num (@input) {
102 :     $minVal = $num if ($minVal > $num);
103 :     }
104 :    
105 :     $minVal;
106 :    
107 : apizer 1080 }
108 :    
109 : sh002i 1050 #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 : apizer 1080 sub gcf {
153 : sh002i 1050 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 b is the smaller number
157 :     if ($a < $b) {
158 :     my $temp = $a;
159 :     $a = $b;
160 :     $b = $temp;
161 :     }
162 : apizer 1080
163 : sh002i 1050 return $a if $b == 0;
164 : apizer 1080
165 : sh002i 1050 my $q = int($a/$b); # quotient
166 :     my $r = $a % $b; # remainder
167 :    
168 :     return $b if $r == 0;
169 :    
170 :     my $tempR = $r;
171 : apizer 1080
172 : sh002i 1050 while ($r != 0) {
173 :    
174 : apizer 1080 #keep track of what $r was in the last loop, as this is the value
175 : sh002i 1050 #we will want when $r is set to 0
176 :     $tempR = $r;
177 :    
178 :     $a = $b;
179 :     $b = $r;
180 :     $q = $a/$b;
181 :     $r = $a % $b;
182 :    
183 :     }
184 :    
185 :     $tempR;
186 :     }
187 :    
188 :    
189 :     #greatest common factor.
190 :     #same as gcf, but both names are sufficiently common names
191 :     sub gcd {
192 :     return gcf($_[0], $_[1]);
193 :     }
194 :    
195 :     #returns 1 for a prime number, else 0
196 :     #VS 6/30/2000
197 :     sub isPrime {
198 :     my $num = shift;
199 :     return 1 if ($num == 2 or $num == 3);
200 :     return 0 if ($num == 1 or $num == 0);
201 :     for (my $i = 3; $i <= $num/2; $i++) { return 0 if ($num % $i == 0); }
202 :     return 1;
203 :     }
204 :    
205 :     #reduces a fraction, returning an array containing ($numerator, $denominator)
206 :     #VS 7/10/2000
207 :     sub reduce {
208 :    
209 :     my $num = shift;
210 :     my $denom = shift;
211 :     my $gcd = gcd($num, $denom);
212 :    
213 :     $num = $num/$gcd;
214 :     $denom = $denom/$gcd;
215 :    
216 :     # formats such that only the numerator will be negative
217 :     if ($num/$denom < 0) {$num = -abs($num); $denom = abs($denom);}
218 :     else {$num = abs($num); $denom = abs($denom);}
219 :    
220 :     my @frac = ($num, $denom);
221 :     @frac;
222 :     }
223 :    
224 :    
225 : apizer 1080 # takes a number and fixed object, as in "$a x" and formats
226 : sh002i 1050 # to account for when $a = 0, 1, -1
227 :     # Usage: format($scalar, "quoted string");
228 :     # Example: format(-1, "\pi") returns "-\pi"
229 :     # VS 8/1/2000 - slight adaption of code from T. Shemanske of Dartmouth College
230 :     sub preformat {
231 :     my $num = shift;
232 :     my $obj = shift;
233 :     my $out;
234 :    
235 : apizer 1080
236 : sh002i 1050 if ($num == 0) { return 0; }
237 :     elsif ($num == 1) { return $obj; }
238 :     elsif ($num == -1) { return "-".$obj; }
239 :    
240 :     return $num.$obj;
241 :     }
242 :    
243 : gage 1071 # Combinations and permutations
244 : sh002i 1050
245 : gage 1071 sub C {
246 :     my $n = shift;
247 :     my $k = shift;
248 :     my $ans = 1;
249 :    
250 :     if($k>($n-$k)) { $k = $n-$k; }
251 :     for (1..$k) { $ans = ($ans*($n-$_+1))/$_; }
252 :     return $ans;
253 :     }
254 :    
255 :     sub Comb {
256 :     C(@_);
257 :     }
258 :    
259 :     sub P {
260 :     my $n = shift;
261 :     my $k = shift;
262 :     my $perm = 1;
263 :    
264 :     if($n != int($n) or $n < 0) {
265 :     warn 'Non-negative integer required.';
266 :     return;
267 :     }
268 :     if($k>$n) {
269 :     warn 'Second argument of Permutation bigger than first.';
270 :     return;
271 :     }
272 :     for (($n-$k+1)..$n) { $perm *= $_;}
273 :     return $perm;
274 :     }
275 :    
276 :     sub Perm {
277 :     P(@_);
278 :     }
279 :    
280 : sh002i 1050 #factorial
281 :    
282 :     sub fact {
283 : gage 1071 P($_[0], $_[0]);
284 : sh002i 1050 }
285 : gage 1071
286 : sh002i 1050 # return 1 so that this file can be included with require
287 :     1

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9