[system] / trunk / webwork / system / courseScripts / PGpolynomialmacros.pl Repository: Repository Listing bbplugincoursesdistsnplrochestersystemwww

Diff of /trunk/webwork/system/courseScripts/PGpolynomialmacros.pl

Revision 271 Revision 272
3## This file is PGpolynomialmacros.pl ## 3## This file is PGpolynomialmacros.pl ##
4## It contains rountines used to create and manipulate ## 4## It contains rountines used to create and manipulate ##
5## polynomials for WeBWorK ## 5## polynomials for WeBWorK ##
6## ## 6## ##
7## Copyright 2002 Mark Schmitt ## 7## Copyright 2002 Mark Schmitt ##
8## Version 1.1.2 ##
8########################################################## 9##########################################################
9 10
10## In the current version, there is no attempt to verify that correct arrays are being passed to the routines. 11## In the current version, there is no attempt to verify that correct arrays are being passed to the routines.
11## This ought to be changed in the next incarnation. 12## This ought to be changed in the next incarnation.
12## It is assumed that arrays passed to the routines have no leading zeros, and represent the coefficients of 13## It is assumed that arrays passed to the routines have no leading zeros, and represent the coefficients of
24## Stringification : converting an array of coefficients into a properly formatted polynomial string 25## Stringification : converting an array of coefficients into a properly formatted polynomial string
26## Polynomial Subtraction 27## Polynomial Subtraction
27 28
28 29
29
30## 30##
31## @sum = PolyAdd(~~@x,~~@y); 31## ValidPoly
32## Returns the sum of polynomials x and y
33## 32##
33sub ValidPoly {
34 my \$xref = @_;
35 if (\${\$xref}[0] != 0) {return 1;}
36 else {return 0;}
37}
38
35 my (\$xref,\$yref) = @_; 40 my (\$xref,\$yref) = @_;
36 @local_x = @{\$xref}; 41 @local_x = @{\$xref};
37 @local_y = @{\$yref}; 42 @local_y = @{\$yref};
38 if (\$#local_x < \$#local_y) { 43 if (\$#local_x < \$#local_y) {
44 foreach \$i (0..\$#local_x) { 49 foreach \$i (0..\$#local_x) {
45 \$sum[\$i] = \$local_x[\$i] + \$local_y[\$i]; 50 \$sum[\$i] = \$local_x[\$i] + \$local_y[\$i];
46 } 51 }
47 return @sum; 52 return @sum;
48} 53}
49## 54
50## @diff = PolySub(~~@x,~~@y);
51## Returns the polynomial x-y
52##
53sub PolySub{ 55sub PolySub{
54 my (\$xref,\$yref) = @_; 56 my (\$xref,\$yref) = @_;
55 @local_x = @{\$xref}; 57 @local_x = @{\$xref};
56 @local_y = @{\$yref}; 58 @local_y = @{\$yref};
57 if (\$#local_x < \$#local_y) { 59 if (\$#local_x < \$#local_y) {
65 } 67 }
66 return @diff; 68 return @diff;
67} 69}
68 70
69## 71##
70## @product = PolyMult(~~@coefficientArray1,~~@coefficientArray2); 72## @newPoly = PolyMult(~~@coefficientArray1,~~@coefficientArray2);
71## 73##
72## accepts two arrays containing coefficients in descending order 74## accepts two arrays containing coefficients in descending order
73## returns an array with the coefficients of the product 75## returns an array with the coefficients of the product
74## 76##
75 77
86 return @result; 88 return @result;
87} 89}
88 90
89## 91##
90## (@quotient,\$remainder) = SynDiv(~~@dividend,~~@divisor); 92## (@quotient,\$remainder) = SynDiv(~~@dividend,~~@divisor);
91## Returns the quotient in an array and the remainder in a scalar
92## 93##
93 94
94sub SynDiv{ 95sub SynDiv{
95 my (\$dividendref,\$divisorref)=@_; 96 my (\$dividendref,\$divisorref)=@_;
96 my @dividend = @{\$dividendref}; 97 my @dividend = @{\$dividendref};
102 } 103 }
103 return @quotient; 104 return @quotient;
104} 105}
105 106
106## 107##
107## (\$quotient,\$remainder) = LongDiv(~~@dividend,~~@divisor);
108## 108##
109## Returns references to the arrays that store the quotient and remainder 109##
110##
110## 111##
111 112
112sub LongDiv{ 113sub LongDiv{
113 my (\$dividendref,\$divisorref)=@_; 114 my (\$dividendref,\$divisorref)=@_;
114 my @dividend = @{\$dividendref}; 115 my @dividend = @{\$dividendref};
213 } 214 }
214 elsif (\$j > 0 && \$j!=1) { 215 elsif (\$j > 0 && \$j!=1) {
215 if (\$poly[\$i] >0) { 216 if (\$poly[\$i] >0) {
216 if (\$poly[\$i]!=1){ 217 if (\$poly[\$i]!=1){
217 \$string = \$string."+\$poly[\$i] x^{\$j}";} 218 \$string = \$string."+\$poly[\$i] x^{\$j}";}
218 else {\$string = \$string."x^{\$j}";}} 219 else {\$string = \$string."+x^{\$j}";}}
219 elsif (\$poly[\$i] == 0) {} 220 elsif (\$poly[\$i] == 0) {}
220 elsif (\$poly[\$i] == -1) {\$string=\$string."-x^{\$j}";} 221 elsif (\$poly[\$i] == -1) {\$string=\$string."-x^{\$j}";}
221 else {\$string = \$string."\$poly[\$i] x^{\$j}";} 222 else {\$string = \$string."\$poly[\$i] x^{\$j}";}
222 } 223 }
223 elsif (\$j == 1) { 224 elsif (\$j == 1) {
224 if (\$poly[\$i] > 0){ 225 if (\$poly[\$i] > 0){
225 if (\$poly[\$i]!=1){ 226 if (\$poly[\$i]!=1){
226 \$string = \$string."+\$poly[\$i] x";} 227 \$string = \$string."+\$poly[\$i] x";}
227 else {\$string = \$string."x";}} 228 else {\$string = \$string."+x";}}
228 elsif (\$poly[\$i] == 0) {} 229 elsif (\$poly[\$i] == 0) {}
229 elsif (\$poly[\$i] == -1){\$string=\$string."-x";} 230 elsif (\$poly[\$i] == -1){\$string=\$string."-x";}
230 else {\$string=\$string."\$poly[\$i] x";} 231 else {\$string=\$string."\$poly[\$i] x";}
231 } 232 }
232 else { 233 else {
237 } 238 }
238 } 239 }
239 return \$string; 240 return \$string;
240} 241}
241 242
243sub PolyFunc {
244 my \$temp = \$_[0];
245 my @poly = @{\$temp};
246 \$func = "";
247 foreach \$i (0..\$#poly) {
248 \$j = \$#poly-\$i;
249 if (\$poly[\$i] > 0) {\$func = \$func."+\$poly[\$i]*x**(\$j)";}
250 else {\$func = \$func."\$poly[\$i]*x**(\$j)";}
251 }
252 return \$func;
253}
242 254
243## 255##
244## (\$maxpos,\$maxneg) = Descartes(~~@poly); 256## (\$maxpos,\$maxneg) = Descartes(~~@poly);
245## 257##
246## accepts an array containing the coefficients, in descending order, of a 258## accepts an array containing the coefficients, in descending order, of a

Legend:
 Removed from v.271 changed lines Added in v.272