[system] / branches / gage_dev / pg / lib / PowerPolynomial.pm Repository:
ViewVC logotype

Annotation of /branches/gage_dev/pg/lib/PowerPolynomial.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : sh002i 2558 #! /usr/local/bin/perl
2 :    
3 :     package PowerPolynomial;
4 :    
5 :     #@ISA = qw(Function);
6 :    
7 :    
8 :     sub givenVariableAndHash { # Give anonymous hash of coefficients
9 :     my ($class, $variable, $hash) = @_;
10 :     my $terms = {};
11 :     my @exponents = keys(%{$hash});
12 :     my $p;
13 :     my $q;
14 :     my $cp;
15 :     my $cq;
16 :     foreach my $ith (@exponents) {
17 :     $cp = $hash -> {$ith} -> [0] -> [0];
18 :     $cq = $hash -> {$ith} -> [0] -> [1];
19 :     $cq = (!defined($cq)) ? 1 : $cq ;
20 :     $terms ->{$ith} -> {'coef'} = new Fraction($cp, $cq);
21 :     $p = $hash -> {$ith} -> [1] -> [0];
22 :     $q = $hash -> {$ith} -> [1] -> [1];
23 :     $q = (!defined($q)) ? 1 : $q ;
24 :     $terms ->{$ith} -> {'exp'} = new Fraction($p, $q);
25 :     }
26 :     my $self = { 'variable' => $variable,
27 :     'terms' => $terms,
28 :     };
29 :     bless $self, $class;
30 :     return($self);
31 :     }
32 :    
33 :     sub givenVariableAndTerms { # Give reference to hash of Fraction coefficients and exponents
34 :     my ($class, $variable, $hash) = @_;
35 :     my $terms = {};
36 :     my @exponents = keys(%{$hash});
37 :     foreach my $ith (@exponents) {
38 :     $terms ->{$ith} -> {'coef'} = $hash -> {$ith} -> {'coef'} -> copy ;
39 :     $terms ->{$ith} -> {'exp'} = $hash -> {$ith} -> {'exp'} -> copy ;
40 :     }
41 :     my $self = { 'variable' => $variable,
42 :     'terms' => $terms,
43 :     };
44 :     bless $self, $class;
45 :     return($self);
46 :     }
47 :    
48 :    
49 :    
50 :    
51 :     sub terms {
52 :     my $self = shift;
53 :     if (@_) {$self -> {'terms'} = shift;}
54 :     return ($self -> {'terms'});
55 :     }
56 :    
57 :     sub variable {
58 :     my $self = shift;
59 :     if (@_) {$self->{'variable'} = shift;}
60 :     return($self ->{'variable'});
61 :     }
62 :    
63 :     sub printSelf {
64 :     my $self = shift;
65 :     my $variable = $self->variable();
66 :     my $terms = $self -> terms();
67 :     my @exponents = keys(%{$terms});
68 :     my $string = "";
69 :     my $numTerms = scalar(@exponents);
70 :     my $jth;
71 :     my $coef;
72 :     my $exp;
73 :     my $p;
74 :     my $q;
75 :     foreach my $i (0..$numTerms-1) {
76 :     $jth = $exponents[$i];
77 :     $coef = $terms -> {$jth} -> [0];
78 :     $p = $terms -> {$jth} -> [1] -> [0];
79 :     $q = $terms -> {$jth} -> [1] -> [1];
80 :     $exp = ( !defined($q) or $q==1) ? "$p" : "$p/$q";
81 :     $string .= " + " . $coef ." x^{" . $jth ."}";
82 :     }
83 :     return($string);
84 :     }
85 :    
86 :     sub TeXizeHighFirst {
87 :     my $self = shift;
88 :     my $variable = $self->variable();
89 :     my $terms = $self->terms();
90 :     my @exponents =keys(%{$terms});
91 :     my @sorted_exponents = &PGtranslator::PGsort(sub {$_[0] <=> $_[1]}, @exponents);
92 :     my $numTerms = scalar(@sorted_exponents);
93 :     my $string = "";
94 :     my $found = 0;
95 :     my $coef;
96 :     my $jth;
97 :     my $p;
98 :     my $q;
99 :     my $exp;
100 :     my $numer;
101 :     my $denom;
102 :     foreach my $i (0..$numTerms-1) {
103 :     $jth = $sorted_exponents[$numTerms -1 -$i];
104 :     if ($jth != 0) {
105 :     $exp = $terms -> {$jth} -> {'exp'};
106 :     $exp -> reduce();
107 :     $numer = $exp -> numerator;
108 :     $numerAbs = ($numer < 0) ? -$numer : $numer;
109 :     $denom = $exp -> denominator;
110 :     $expStr = $exp -> TeXize;
111 :     $vterm = ($jth == 1) ? "$variable" :
112 :     ( $denom == 1 and $numer >0 ) ? "$variable^{$expStr}"
113 :     : ($denom == 1 and $numer == -1) ? "\\frac{1}{$variable}"
114 :     : ($denom == 1 and $numer < 0) ? "\\frac{1}{$variable^{$numerAbs}}"
115 :     : ($denom == 2 and $numer == 1) ? "\\sqrt{$variable}"
116 :     : ($denom == 2 and $numer > 0) ? "\\sqrt{$variable^{$numer}}"
117 :     : ($denom == 2 and $numer == -1) ? "\\frac{1}{\\sqrt{$variable}}"
118 :     : ($denom == 2 and $numer < 0) ? "\\frac{1}{\\sqrt{$variable^{$numerAbs}}}"
119 :     : ($denom != 1 and $numer == 1) ? "\\sqrt[$denom]{$variable}"
120 :     : ($denom != 1 and $numer > 0) ? "\\sqrt[$denom]{$variable^{$numer}}"
121 :     : ($denom != 1 and $numer == -1) ? "\\frac{1}{\\sqrt[$denom]{$variable}}"
122 :     : ($denom != 1 and $numer < 0) ? "\\frac{1}{\\sqrt[$denom]{$variable^{$numerAbs}}}"
123 :     : "$variable^{$expStr}";
124 :     if (($coef = $terms -> {$jth} -> {'coef'} ->scalar) != 0)
125 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> TeXize;
126 :     if ($found) {
127 :     $string .=
128 :     ($coef == 1) ? " + $vterm"
129 :     : ($coef > 0) ? " + $coefStr $vterm"
130 :     : ($coef == -1) ? " - $vterm"
131 :     : " $coefStr $vterm";
132 :     }
133 :     else {
134 :     $found=1;
135 :     $string .=
136 :     ($coef == 1) ? " $vterm"
137 :     : ($coef > 0) ? " $coefStr $vterm"
138 :     : ($coef == -1) ? " - $vterm"
139 :     : " $coefStr $vterm";
140 :     }
141 :     }
142 :     }
143 :     else {
144 :     if (($coef = $terms -> {$jth} -> {'coef'} ->scalar) != 0)
145 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> TeXize;
146 :     if ($found) {
147 :     $string .= ($coef > 0) ? " + $coefStr "
148 :     : " $coefStr " ;
149 :     }
150 :     else {
151 :     $found=1;
152 :     $string .= ($coef > 0) ? " $coefStr "
153 :     : " $coefStr " ;
154 :     }
155 :     }
156 :     }
157 :     }
158 :     return($string);
159 :     }
160 :    
161 :     sub TeXizeHighFirstAt {
162 :     my ($self, $at, @rest) = @_;
163 :     my $variable = $self->variable();
164 :     if (defined($at)) {
165 :     if (ref($at) ne '') {
166 :     $variable = $at -> TeXizeHighFirstAt(@rest); }
167 :     else {
168 :     $variable = $at;
169 :     }
170 :     $variable = '\left(' . $variable . '\right)';
171 :     }
172 :     my $terms = $self->terms();
173 :     my @exponents =keys(%{$terms});
174 :     my @sorted_exponents = &PGtranslator::PGsort(sub {$_[0] <=> $_[1]}, @exponents);
175 :     my $numTerms = scalar(@sorted_exponents);
176 :     my $string = "";
177 :     my $found = 0;
178 :     my $coef;
179 :     my $jth;
180 :     my $p;
181 :     my $q;
182 :     my $exp;
183 :     my $numer;
184 :     my $denom;
185 :     foreach my $i (0..$numTerms-1) {
186 :     $jth = $sorted_exponents[$numTerms -1 -$i];
187 :     if ($jth != 0) {
188 :     $exp = $terms -> {$jth} -> {'exp'};
189 :     $exp -> reduce();
190 :     $numer = $exp -> numerator;
191 :     $numerAbs = ($numer < 0) ? -$numer : $numer;
192 :     $denom = $exp -> denominator;
193 :     $expStr = $exp -> TeXize;
194 :     $vterm = ($jth == 1) ? "$variable" :
195 :     ( $denom == 1 and $numer >0 ) ? "$variable^{$expStr}"
196 :     : ($denom == 1 and $numer == -1) ? "\\frac{1}{$variable}"
197 :     : ($denom == 1 and $numer < 0) ? "\\frac{1}{$variable^{$numerAbs}}"
198 :     : ($denom == 2 and $numer == 1) ? "\\sqrt{$variable}"
199 :     : ($denom == 2 and $numer > 0) ? "\\sqrt{$variable^{$numer}}"
200 :     : ($denom == 2 and $numer == -1) ? "\\frac{1}{\\sqrt{$variable}}"
201 :     : ($denom == 2 and $numer < 0) ? "\\frac{1}{\\sqrt{$variable^{$numerAbs}}}"
202 :     : ($denom != 1 and $numer == 1) ? "\\sqrt[$denom]{$variable}"
203 :     : ($denom != 1 and $numer > 0) ? "\\sqrt[$denom]{$variable^{$numer}}"
204 :     : ($denom != 1 and $numer == -1) ? "\\frac{1}{\\sqrt[$denom]{$variable}}"
205 :     : ($denom != 1 and $numer < 0) ? "\\frac{1}{\\sqrt[$denom]{$variable^{$numerAbs}}}"
206 :     : "$variable^{$expStr}";
207 :     if (($coef = $terms -> {$jth} -> {'coef'} ->scalar) != 0)
208 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> TeXize;
209 :     if ($found) {
210 :     $string .=
211 :     ($coef == 1) ? " + $vterm"
212 :     : ($coef > 0) ? " + $coefStr $vterm"
213 :     : ($coef == -1) ? " - $vterm"
214 :     : " $coefStr $vterm";
215 :     }
216 :     else {
217 :     $found=1;
218 :     $string .=
219 :     ($coef == 1) ? " $vterm"
220 :     : ($coef > 0) ? " $coefStr $vterm"
221 :     : ($coef == -1) ? " - $vterm"
222 :     : " $coefStr $vterm";
223 :     }
224 :     }
225 :     }
226 :     else {
227 :     if (($coef = $terms -> {$jth} -> {'coef'} ->scalar) != 0)
228 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> TeXize;
229 :     if ($found) {
230 :     $string .= ($coef > 0) ? " + $coefStr "
231 :     : " $coefStr " ;
232 :     }
233 :     else {
234 :     $found=1;
235 :     $string .= ($coef > 0) ? " $coefStr "
236 :     : " $coefStr " ;
237 :     }
238 :     }
239 :     }
240 :     }
241 :     return($string);
242 :     }
243 :    
244 :     sub TeXizeLowFirst {
245 :     my $self = shift;
246 :     my $variable = $self->variable();
247 :     my $terms = $self->terms();
248 :     my @exponents =keys(%{$terms});
249 :     my @sorted_exponents = &PGtranslator::PGsort(sub {$_[1] <=> $_[2]}, @exponents);
250 :     my $numTerms = scalar(@sorted_exponents);
251 :     my $string = "";
252 :     my $found = 0;
253 :     my $coef;
254 :     my $jth;
255 :     my $p;
256 :     my $q;
257 :     my $exp;
258 :     foreach my $i (0..$numTerms-1) {
259 :     $jth = $sorted_exponents[$numTerms -1 -$i];
260 :     if ($jth != 0) {
261 :     $exp = $terms -> {$jth} -> {'exp'};
262 :     $expStr = $exp -> TeXize;
263 :     $vterm = ($jth == 1) ? "$variable" : "$variable^{$expStr}";
264 :     if (($coef = $terms -> {$jth} -> {'coef'} ->scalar) != 0)
265 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> TeXize;
266 :     if ($found) {
267 :     $string .=
268 :     ($coef == 1) ? " + $vterm"
269 :     : ($coef > 0) ? " + $coefStr $vterm"
270 :     : ($coef == -1) ? " - $vterm"
271 :     : " $coefStr $vterm";
272 :     }
273 :     else {
274 :     $found=1;
275 :     $string .=
276 :     ($coef == 1) ? " $vterm"
277 :     : ($coef > 0) ? " $coefStr $vterm"
278 :     : ($coef == -1) ? " - $vterm"
279 :     : " $coefStr $vterm";
280 :     }
281 :     }
282 :     }
283 :     else {
284 :     if (($coef = $terms -> {$jth} -> {'coef'} ->scalar) != 0)
285 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> TeXize;
286 :     if ($found) {
287 :     $string .= ($coef > 0) ? " + $coefStr "
288 :     : " $coefStr " ;
289 :     }
290 :     else {
291 :     $found=1;
292 :     $string .= ($coef > 0) ? " $coefStr "
293 :     : " $coefStr " ;
294 :     }
295 :     }
296 :     }
297 :     }
298 :     return($string);
299 :     }
300 :    
301 :    
302 :     sub perlizeHighFirst {
303 :     my $self = shift;
304 :     my $variable = $self->variable();
305 :     my $terms = $self->terms();
306 :     my @exponents =keys(%{$terms});
307 :     my @sorted_exponents = &PGtranslator::PGsort(sub {$_[0] <=> $_[1]}, @exponents);
308 :     my $numTerms = scalar(@sorted_exponents);
309 :     my $string = "";
310 :     my $found = 0;
311 :     my $coef;
312 :     my $jth;
313 :     my $p;
314 :     my $q;
315 :     my $exp;
316 :     foreach my $i (0..$numTerms-1) {
317 :     $jth = $sorted_exponents[$numTerms -1 -$i];
318 :     if ($jth != 0) {
319 :     $exp = $terms -> {$jth} -> {'exp'};
320 :     $expStr = $exp -> perlize;
321 :     $vterm = ($jth == 1) ? "$variable" : "$variable**($expStr)";
322 :     if (($coef = $terms -> {$jth} -> {'coef'} -> scalar) != 0)
323 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> perlize;
324 :     if ($found) {
325 :     $string .=
326 :     ($coef == 1) ? " + $vterm"
327 :     : ($coef > 0) ? " + $coefStr * $vterm"
328 :     : ($coef == -1) ? " - $vterm"
329 :     # : " - $coefStr * $vterm";
330 :     : " - " . ($terms ->{$jth} -> {'coef'} -> times(-1)) -> perlize
331 :     . $vterm;
332 :     }
333 :     else {
334 :     $found=1;
335 :     $string .=
336 :     ($coef == 1) ? " $vterm"
337 :     : ($coef > 0) ? " $coefStr * $vterm"
338 :     : ($coef == -1) ? " - $vterm"
339 :     # : " - $coefStr * $vterm";
340 :     : " - " . ($terms ->{$jth} -> {'coef'} -> times(-1)) -> perlize
341 :     . $vterm;
342 :     }
343 :     }
344 :     }
345 :     else {
346 :     if (($coef = $terms -> {$jth} -> {'coef'}) != 0)
347 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> perlize;
348 :     if ($found) {
349 :     $string .= ($coef > 0) ? " + $coefStr "
350 :     # : " - $coefStr " ;
351 :     : " - " . ($terms ->{$jth} -> {'coef'} -> times(-1)) -> perlize;
352 :     }
353 :     else {
354 :     $found=1;
355 :     $string .= ($coef > 0) ? " $coefStr "
356 :     # : " - $coefStr " ;
357 :     : " - " . ($terms ->{$jth} -> {'coef'} -> times(-1)) -> perlize;
358 :     }
359 :     }
360 :     }
361 :     }
362 :     return($string);
363 :     }
364 :    
365 :     sub perlizeHighFirstAt {
366 :     my ($self, $at, @rest) = @_;
367 :     my $variable = $self->variable();
368 :     if (defined($at)) {
369 :     if (ref($at) ne '') {
370 :     $variable = $at -> perlizeHighFirstAt(@rest); }
371 :     else {
372 :     $variable = $at;
373 :     }
374 :     $variable = '(' . $variable . ')';
375 :     }
376 :     my $terms = $self->terms();
377 :     my @exponents =keys(%{$terms});
378 :     my @sorted_exponents = &PGtranslator::PGsort(sub {$_[0] <=> $_[1]}, @exponents);
379 :     my $numTerms = scalar(@sorted_exponents);
380 :     my $string = "";
381 :     my $found = 0;
382 :     my $coef;
383 :     my $jth;
384 :     my $p;
385 :     my $q;
386 :     my $exp;
387 :     foreach my $i (0..$numTerms-1) {
388 :     $jth = $sorted_exponents[$numTerms -1 -$i];
389 :     if ($jth != 0) {
390 :     $exp = $terms -> {$jth} -> {'exp'};
391 :     $expStr = $exp -> perlize;
392 :     $vterm = ($jth == 1) ? "$variable" : "$variable**($expStr)";
393 :     if (($coef = $terms -> {$jth} -> {'coef'} ->scalar) != 0)
394 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> perlize;
395 :     if ($found) {
396 :     $string .=
397 :     ($coef == 1) ? " + $vterm"
398 :     : ($coef > 0) ? " + $coefStr * $vterm"
399 :     : ($coef == -1) ? " - $vterm"
400 :     : " $coefStr * $vterm";
401 :     }
402 :     else {
403 :     $found=1;
404 :     $string .=
405 :     ($coef == 1) ? " $vterm"
406 :     : ($coef > 0) ? " $coefStr * $vterm"
407 :     : ($coef == -1) ? " - $vterm"
408 :     : " $coefStr * $vterm";
409 :     }
410 :     }
411 :     }
412 :     else {
413 :     if (($coef = $terms -> {$jth} -> {'coef'}) != 0)
414 :     {$coefStr = $terms -> {$jth} -> {'coef'} -> perlize;
415 :     if ($found) {
416 :     $string .= ($coef > 0) ? " + $coefStr "
417 :     : " $coefStr " ;
418 :     }
419 :     else {
420 :     $found=1;
421 :     $string .= ($coef > 0) ? " $coefStr "
422 :     : " $coefStr " ;
423 :     }
424 :     }
425 :     }
426 :     }
427 :     return($string);
428 :     }
429 :    
430 :    
431 :     sub scalarMult {
432 :     my $self = shift;
433 :     my $scalar = shift;
434 :     if (!(defined($scalar))) {return(undef());}
435 :     my $variable = $self -> variable;
436 :     my $terms = $self -> terms;
437 :     my @exponents = keys(%{$terms});
438 :     foreach my $ith (@exponents) {
439 :     $terms ->{$ith} -> {'coef'} = $terms -> {$ith} -> {'coef'} -> times($scalar);
440 :     }
441 :     my $scalarM = givenVariableAndTerms PowerPolynomial($variable, $terms);
442 :     return($scalarM);
443 :     }
444 :    
445 :    
446 :    
447 :     sub derivative {
448 :     my $self = shift;
449 :     my $class = ref($self);
450 :     my $variable = shift;
451 :     if (!(defined($variable))) { $variable = $self -> variable;}
452 :     my $terms = $self->terms();
453 :     my @exponents =keys(%{$terms});
454 :     my $derTerms = {};
455 :     my ($derExp, $derCoef, $derExponent);
456 :     foreach my $exp (@exponents) {
457 :     if ($exp != 0) {
458 :     $derExp = $exp - 1;
459 :     $derCoef = $terms->{$exp}->{'coef'} -> times($terms -> {$exp} -> {'exp'});
460 :     $derExponent = $terms -> {$exp} -> {'exp'} -> minus(1);
461 :     $derTerms -> {$derExp} -> {'coef'} = $derCoef;
462 :     $derTerms -> {$derExp} -> {'exp'} = $derExponent;
463 :     }
464 :     }
465 :     my $derivative = {'variable' => $variable,
466 :     'terms' => $derTerms,
467 :     };
468 :     bless $derivative, $class;
469 :     return($derivative);
470 :     }
471 :    
472 :     sub antiDerivative {
473 :     my $self = shift;
474 :     my $class = ref($self);
475 :     my $variable = shift;
476 :     if (!(defined($variable))) { $variable = $self -> variable;}
477 :     my $terms = $self->terms();
478 :     my @exponents =keys(%{$terms});
479 :     my $antiDerTerms = {};
480 :     my ($antiDerExp, $antiDerCoef, $antiDerExponent);
481 :     foreach my $exp (@exponents) {
482 :     if ($exp != -1) {
483 :     $antiDerExp = $exp + 1;
484 :     $antiDerCoef = $terms->{$exp}->{'coef'} -> divBy($terms->{$exp}->{'exp'}->plus(1));
485 :     $antiDerExponent = $terms -> {$exp} -> {'exp'} -> plus(1);
486 :     $antiDerTerms -> {$antiDerExp} -> {'coef'} = $antiDerCoef;
487 :     $antiDerTerms -> {$antiDerExp} -> {'exp'} = $antiDerExponent;
488 :     }
489 :     }
490 :     my $antiDerivative = {'variable' => $variable,
491 :     'terms' => $antiDerTerms,
492 :     };
493 :     bless $antiDerivative, $class;
494 :     return($antiDerivative);
495 :     }
496 :    
497 :     1;
498 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9