KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djepJUnit > RpTest


1 package org.lsmp.djepJUnit;
2
3 import junit.framework.*;
4 import org.nfunk.jep.*;
5 import org.nfunk.jep.type.*;
6 import org.lsmp.djep.matrixJep.*;
7 import org.lsmp.djep.vectorJep.values.*;
8 import org.lsmp.djep.rpe.*;
9
10 /* @author rich
11  * Created on 19-Nov-2003
12  *
13  * This code is covered by a Creative Commons
14  * Attribution, Non Commercial, Share Alike license
15  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
16  */

17
18 /**
19  * JUnit test for full Matrix Rp evaluator
20  *
21  * @author Rich Morris
22  * Created on 19-Nov-2003
23  */

24 public class RpTest extends TestCase {
25     JEP j;
26     public static final boolean SHOW_BAD=false;
27     
28     public RpTest(String JavaDoc name) {
29         super(name);
30     }
31
32     public static void main(String JavaDoc args[]) {
33         // Create an instance of this class and analyse the file
34

35         TestSuite suite= new TestSuite(RpTest.class);
36 // DJepTest jt = new DJepTest("DJepTest");
37
// jt.setUp();
38
suite.run(new TestResult());
39     }
40     /** strings for each variable */
41     String JavaDoc matStrs[][] = new String JavaDoc[10][10];
42     String JavaDoc vecStrs[] = new String JavaDoc[10];
43
44     protected void setUp() {
45         j = new JEP();
46         j.addStandardConstants();
47         j.addStandardFunctions();
48         j.addComplex();
49         //j.setTraverse(true);
50
j.setAllowAssignment(true);
51         j.setAllowUndeclared(true);
52         j.setImplicitMul(true);
53
54     }
55
56     public static Test suite() {
57         return new TestSuite(RpTest.class);
58     }
59
60     public void testGood()
61     {
62         assertEquals(1,1);
63     }
64
65     public void myAssertEquals(String JavaDoc msg,String JavaDoc actual,String JavaDoc expected)
66     {
67         if(!actual.equals(expected))
68             System.out.println("Error \""+msg+"\" is \n<"+actual+"> should be \n<"+expected+">");
69         assertEquals("<"+msg+">",expected,actual);
70         System.out.println("Success: Value of <"+msg+"> is <"+actual+">");
71     }
72
73     public void valueTest(String JavaDoc expr,double dub) throws ParseException,Exception JavaDoc
74     {
75         valueTest(expr,new Double JavaDoc(dub));
76     }
77     public void valueTest(String JavaDoc expr,Object JavaDoc expected) throws ParseException,Exception JavaDoc
78     {
79         Node node = j.parse(expr);
80         Object JavaDoc res = j.evaluate(node);
81         if(j.hasError())
82             fail("Evaluation Failure: "+expr+j.getErrorInfo());
83         assertEquals("<"+expr+">",expected,res);
84         System.out.println("Sucess value of <"+expr+"> is "+res);
85     }
86
87     public void valueTest(String JavaDoc expr,String JavaDoc expected) throws ParseException,Exception JavaDoc
88     {
89         Node node = j.parse(expr);
90         Object JavaDoc res = j.evaluate(node);
91         if(j.hasError())
92             fail("Evaluation Failure: "+expr+j.getErrorInfo());
93         assertEquals("<"+expr+">",expected,res.toString());
94         System.out.println("Sucess value of <"+expr+"> is "+res.toString());
95     }
96
97     public void complexValueTest(String JavaDoc expr,Complex expected,double tol) throws Exception JavaDoc
98     {
99         Node node = j.parse(expr);
100         Object JavaDoc res = j.evaluate(node);
101         assertTrue("<"+expr+"> expected: <"+expected+"> but was <"+res+">",
102             expected.equals((Complex) res,tol));
103         System.out.println("Sucess value of <"+expr+"> is "+res);
104     }
105
106     public Object JavaDoc calcValue(String JavaDoc expr) throws ParseException,Exception JavaDoc
107     {
108         Node node = j.parse(expr);
109         Object JavaDoc res = j.evaluate(node);
110         return res;
111     }
112 /*
113     public void simplifyTest(String expr,String expected) throws ParseException,Exception
114     {
115         Node node = j.parse(expr);
116         String res = j.toString(node);
117         
118         Node node2 = j.parse(expected);
119         Node matEqn2 = j.preprocess(node2);
120         Node simp2 = j.simplify(matEqn2);
121         String res2 = j.toString(simp2);
122
123
124         if(!res2.equals(res))
125             System.out.println("Error: Value of \""+expr+"\" is \""+res+"\" should be \""+res2+"\"");
126         assertEquals("<"+expr+">",res2,res);
127         System.out.println("Sucess: Value of \""+expr+"\" is \""+res+"\"");
128             
129 // System.out.print("Full Brackets:\t");
130 // j.pv.setFullBrackets(true);
131 // j.pv.println(simp);
132 // j.pv.setFullBrackets(false);
133
134     }
135 */

136
137 /*
138     public void simplifyTestString(String expr,String expected) throws ParseException
139     {
140         Node node = j.parse(expr);
141         Node matEqn = j.preprocess(node);
142         String res = j.toString(matEqn);
143         
144         if(!expected.equals(res))
145             System.out.println("Error: Value of \""+expr+"\" is \""+res+"\" should be \""+expected+"\"");
146         assertEquals("<"+expr+">",expected,res);
147         System.out.println("Sucess: Value of \""+expr+"\" is \""+res+"\"");
148             
149 // System.out.print("Full Brackets:\t");
150 // j.pv.setFullBrackets(true);
151 // j.pv.println(simp);
152 // j.pv.setFullBrackets(false);
153
154     }
155 */

156     void rpTest(String JavaDoc eqns[], String JavaDoc eqn2) throws ParseException,Exception JavaDoc
157     {
158         for(int i=0;i<eqns.length;++i) {
159             System.out.println("eqns "+eqns[i]);
160             Node node = j.parse(eqns[i]);
161             j.evaluate(node);
162         }
163         Node node3 = j.parse(eqn2);
164         RpEval rpe = new RpEval(j);
165         RpCommandList list = rpe.compile(node3);
166 // rpe.copyVars();
167
double rpRes = rpe.evaluate(list);
168
169         Object JavaDoc matRes = j.evaluate(node3);
170 // System.out.println("rpRes: "+rpRes.getClass().getName()+" = "+rpRes.toString());
171
if(j.hasError())
172             fail("Evaluation Failure: "+eqn2+j.getErrorInfo());
173         myAssertEquals("<"+eqn2+">",""+rpRes,matRes.toString());
174
175         if(!matRes.equals(new Double JavaDoc(rpRes)))
176             fail("Expected <"+matRes+"> found <"+rpRes+">");
177     }
178
179     /** As before but don't test with MatrixJep.evaluate */
180     void rpTest2(String JavaDoc eqns[]) throws ParseException,Exception JavaDoc
181     {
182         Node nodes[] = new Node[eqns.length];
183         double rpRes[] = new double[eqns.length];
184         RpEval rpe = new RpEval(j);
185         for(int i=0;i<eqns.length;++i) {
186             System.out.println("eqns "+eqns[i]);
187             nodes[i] = j.parse(eqns[i]);
188             RpCommandList list = rpe.compile(nodes[i]);
189             rpRes[i] = rpe.evaluate(list);
190             System.out.println("<"+eqns[i]+"> "+rpRes[i]);
191         }
192         for(int i=0;i<eqns.length;++i) {
193             Object JavaDoc matRes = j.evaluate(nodes[i]);
194             if(!matRes.equals(new Double JavaDoc(rpRes[i])))
195                     fail("Expected <"+matRes+"> found <"+rpRes[i]+">");
196         }
197         rpe.cleanUp();
198     }
199
200     public void testRp() throws ParseException,Exception JavaDoc
201     {
202         rpTest(new String JavaDoc[0],"1*2*3+4*5*6+7*8*9");
203         
204         rpTest(new String JavaDoc[]{"x1=1","x2=2","x3=3","x4=4","x5=5","x6=6","x7=7","x8=8","x9=9"},
205             "x1*x2*x3+x4*x5*x6+x7*x8*x9");
206
207     }
208
209     public void testAssign() throws ParseException,Exception JavaDoc
210     {
211         rpTest2(new String JavaDoc[]{"x=5","x+x"});
212         j.setVarValue("x",new Double JavaDoc(6.0));
213         rpTest2(new String JavaDoc[]{"x+x"});
214     }
215     
216     public void testLogical() throws ParseException,Exception JavaDoc
217     {
218         rpTest2(new String JavaDoc[]{"1&&1","1&&0","0&&0","0&&1","3.14&&1"});
219         rpTest2(new String JavaDoc[]{"1||1","1||0","0||0","0||1","3.14||0"});
220         rpTest2(new String JavaDoc[]{"!0","!1","!3.14","!-3.14"});
221         
222         rpTest2(new String JavaDoc[]{"1>1","1>0","0>0","0>1","3.14>1"});
223         rpTest2(new String JavaDoc[]{"1<1","1<0","0<0","0<1","3.14<1"});
224         rpTest2(new String JavaDoc[]{"1>=1","1>=0","0>=0","0>=1","3.14>=1"});
225         rpTest2(new String JavaDoc[]{"1<=1","1<=0","0<=0","0<=1","3.14<=1"});
226         rpTest2(new String JavaDoc[]{"1==1","1==0","0==0","0==1","3.14==1"});
227         rpTest2(new String JavaDoc[]{"1!=1","1!=0","0!=0","0!=1","3.14!=1"});
228
229     }
230     boolean TESTALL = false;
231     public void testFun() throws ParseException,Exception JavaDoc
232     {
233         rpTest2(new String JavaDoc[]{"x=5","y=4","x/y","x%y","x^y"});
234         rpTest2(new String JavaDoc[]{"x=0.5","cos(x)","sin(x)","tan(x)","asin(x)","acos(x)","atan(x)"});
235         rpTest2(new String JavaDoc[]{"x=0.5","cosh(x)","sinh(x)","tanh(x)","asinh(x)","acosh(x+1)","atanh(x)"});
236         rpTest2(new String JavaDoc[]{"x=0.5","sqrt(x)","ln(x)","log(x)","exp(x)","abs(x)"});
237
238         rpTest2(new String JavaDoc[]{"x=0.5","cos(x)^2+sin(x)^2"});
239 // rpTest2(new String[]{"x=0.5","sec(x)","cosec(x)","cot(x)"});
240
}
241 /* public void testSimpleSum() throws ParseException
242     {
243         valueTest("1+2",3);
244         valueTest("2*6+3",15);
245         valueTest("2*(6+3)",18);
246     }
247     
248     public void testOperators() throws ParseException
249     {
250 // if(!Operator.OP_MULTIPLY.isDistributiveOver(Operator.OP_ADD))
251 // fail("* should be distrib over +");
252 // if(Operator.OP_MULTIPLY.isDistributiveOver(Operator.OP_DIVIDE))
253 // fail("* should not be distrib over /");
254 // if(Operator.OP_MULTIPLY.getPrecedence() > Operator.OP_ADD.getPrecedence())
255 // fail("* should have a lower precedence than +");
256
257         valueTest("T=1",1);
258         valueTest("F=0",0);
259         calcValue("a=F"); calcValue("b=F"); calcValue("c=F");
260         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
261         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
262         calcValue("a=F"); calcValue("b=F"); calcValue("c=T");
263         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
264         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
265         calcValue("a=F"); calcValue("b=T"); calcValue("c=F");
266         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
267         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
268         calcValue("a=F"); calcValue("b=T"); calcValue("c=T");
269         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
270         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
271
272         calcValue("a=T"); calcValue("b=F"); calcValue("c=F");
273         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
274         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
275         calcValue("a=T"); calcValue("b=F"); calcValue("c=T");
276         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
277         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
278         calcValue("a=T"); calcValue("b=T"); calcValue("c=F");
279         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
280         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
281         calcValue("a=T"); calcValue("b=T"); calcValue("c=T");
282         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
283         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
284     }
285     
286     public void testSimp() throws ParseException
287     {
288         simplifyTest("2+3","5");
289         simplifyTest("2*3","6");
290         simplifyTest("2^3","8");
291         simplifyTest("3/2","1.5");
292         simplifyTest("2*3+4","10");
293         simplifyTest("2*(3+4)","14");
294
295         simplifyTest("0+x","x");
296         simplifyTest("x+0","x");
297         simplifyTest("0-x","0-x");
298         simplifyTest("x-0","x");
299         simplifyTest("0*x","0");
300         simplifyTest("x*0","0");
301         simplifyTest("1*x","x");
302         simplifyTest("x*1","x");
303         simplifyTest("-1*x","-x");
304         simplifyTest("x*-1","-x");
305         simplifyTest("-(-x)","x");
306         simplifyTest("-(-(-x))","-x");
307         simplifyTest("(-1)*(-1)*x","x");
308         simplifyTest("(-1)*(-1)*(-1)*x","-x");
309         
310         simplifyTest("0/x","0");
311         simplifyTest("x/0","1/0");
312         
313         simplifyTest("x^0","1");
314         simplifyTest("x^1","x");
315         simplifyTest("0^x","0");
316         simplifyTest("1^x","1");
317
318         // (a+b)+c
319         simplifyTest("(2+3)+x","5+x");
320         simplifyTest("(2+x)+3","5+x");
321         simplifyTest("(x+2)+3","5+x");
322         // a+(b+c)
323         simplifyTest("x+(2+3)","5+x");
324         simplifyTest("2+(x+3)","5+x");
325         simplifyTest("2+(3+x)","5+x");
326         // (a+b)-c
327         simplifyTest("(2+3)-x","5-x");
328         simplifyTest("(2+x)-3","x-1");
329         simplifyTest("(x+2)-3","x-1");
330         // (a-b)+c
331         simplifyTest("(2-3)+x","-1+x");
332         simplifyTest("(2-x)+3","5-x");
333         simplifyTest("(x-2)+3","1+x");
334         // a-(b+c)
335         simplifyTest("x-(2+3)","x-5");
336         simplifyTest("2-(x+3)","-1-x");
337         simplifyTest("2-(3+x)","-1-x");
338         // a+(b-c)
339         simplifyTest("x+(2-3)","x-1");
340         simplifyTest("2+(x-3)","-1+x");
341         simplifyTest("2+(3-x)","5-x");
342         // a-(b-c)
343         simplifyTest("x-(2-3)","1+x");
344         simplifyTest("2-(x-3)","5-x");
345         simplifyTest("2-(3-x)","-1+x");
346         // (a-b)-c
347         simplifyTest("(2-3)-x","-1-x");
348         simplifyTest("(2-x)-3","-1-x");
349         simplifyTest("(x-2)-3","x-5");
350
351         // (a*b)*c
352         simplifyTest("(2*3)*x","6*x");
353         simplifyTest("(2*x)*3","6*x");
354         simplifyTest("(x*2)*3","6*x");
355         // a+(b+c)
356         simplifyTest("x*(2*3)","6*x");
357         simplifyTest("2*(x*3)","6*x");
358         simplifyTest("2*(3*x)","6*x");
359         // (a+b)-c
360         simplifyTest("(2*3)/x","6/x");
361         simplifyTest("(3*x)/2","1.5*x");
362         simplifyTest("(x*3)/2","1.5*x");
363         // (a-b)+c
364         simplifyTest("(3/2)*x","1.5*x");
365         simplifyTest("(3/x)*2","6/x");
366         simplifyTest("(x/2)*3","1.5*x");
367         // a-(b+c)
368         simplifyTest("x/(2*3)","x/6");
369         simplifyTest("3/(x*2)","1.5/x");
370         simplifyTest("3/(2*x)","1.5/x");
371         // a+(b-c)
372         simplifyTest("x*(3/2)","1.5*x");
373         simplifyTest("3*(x/2)","1.5*x");
374         simplifyTest("3*(2/x)","6/x");
375         // a-(b-c)
376         simplifyTest("x/(3/2)","x/1.5");
377         simplifyTest("2/(x/3)","6/x");
378         simplifyTest("3/(2/x)","1.5*x");
379         // (a-b)-c
380         simplifyTest("(3/2)/x","1.5/x");
381         simplifyTest("(3/x)/2","1.5/x");
382         simplifyTest("(x/3)/2","x/6");
383
384
385         simplifyTest("x*(3+2)","5*x");
386         simplifyTest("3*(x+2)","6+3*x");
387         simplifyTest("3*(2+x)","6+3*x");
388         simplifyTest("(3+2)*x","5*x");
389         simplifyTest("(3+x)*2","6+2*x");
390         simplifyTest("(x+3)*2","6+x*2");
391
392         simplifyTest("x*(3-2)","x");
393         simplifyTest("3*(x-2)","-6+3*x");
394         simplifyTest("3*(2-x)","6-3*x");
395         simplifyTest("(3-2)*x","x");
396         simplifyTest("(3-x)*2","6-2*x");
397         simplifyTest("(x-3)*2","-6+2*x");
398
399         simplifyTest("3+(x/4)","3+x/4");
400         simplifyTest("2*(x/4)","0.5*x");
401         simplifyTest("(2*(3+(x/4)))","6+0.5*x");
402         simplifyTest("1+(2*(3+(x/4)))","7+0.5*x");
403         simplifyTest("((3+(x/4))*2)+1","7+0.5*x");
404         
405     }
406
407     public void testComplex() throws Exception
408     {
409         double tol = 0.00000001;
410
411         complexValueTest("z=complex(3,2)",new Complex(3,2),tol);
412         complexValueTest("z*z-z",new Complex(2,10),tol);
413         complexValueTest("z^3",new Complex(-9,46),tol);
414         complexValueTest("(z*z-z)/z",new Complex(2,2),tol);
415         complexValueTest("w=polar(2,pi/2)",new Complex(0,2),tol);
416         
417     }
418
419     public void testIf() throws ParseException
420     {
421         valueTest("if(1,2,3)",2);
422         valueTest("if(-1,2,3)",3);
423         valueTest("if(0,2,3)",3);
424         valueTest("if(1,2,3,4)",2);
425         valueTest("if(-1,2,3,4)",3);
426         valueTest("if(0,2,3,4)",4);
427         valueTest("if(0>=0,2,3,4)",2);
428         valueTest("x=3",3);
429         valueTest("if(x==3,1,-1)",1);
430         valueTest("if(x!=3,1,-1)",-1);
431         valueTest("if(x>=3,1,-1)",1);
432         valueTest("if(x>3,1,-1)",-1);
433         valueTest("if(x<=3,1,-1)",1);
434         valueTest("if(x<3,1,-1)",-1);
435     }
436
437     public void testAssign() throws ParseException
438     {
439         valueTest("x=3",3);
440         valueTest("y=3+4",7);
441         valueTest("z=x+y",10);
442         valueTest("a=b=c=z",10);
443         valueTest("b",10);
444         valueTest("d=f=a-b",0);
445     }
446
447                         
448     public void testDiff() throws ParseException
449     {
450         simplifyTest("diff(x^2,x)","2 x");
451         simplifyTest("diff(x^3,x)","3 x^2");
452         simplifyTest("diff(x,x)","1");
453         simplifyTest("diff(1,x)","0");
454         simplifyTest("diff(x^2+x+1,x)","2 x+1");
455         simplifyTest("diff((x+x^2)*(x+x^3),x)","(1+2*x)*(x+x^3)+(x+x^2)*(1+3*x^2)");
456         simplifyTest("diff((x+x^2)/(x+x^3),x)","((1+2*x)*(x+x^3)-(x+x^2)*(1+3*x^2))/((x+x^3)*(x+x^3))");
457         simplifyTest("diff(sin(x),x)","cos(x)");
458         simplifyTest("diff(-(x-5)^3,x)","-(3.0*(x-5.0)^2.0)");
459         
460
461         simplifyTest("diff((x+1)^2,x)","2+2*x");
462         simplifyTest("diff((x+y)^2,x)","2*(x+y)");
463         simplifyTest("diff((x+x^2)^3,x)","3*(x+x^2)^2*(1+2*x)");
464         
465         simplifyTest("diff(sin(x+1),x)","cos(x+1)");
466         simplifyTest("diff(sin(x+x^2),x)","cos(x+x^2)*(1+2*x)");
467
468         simplifyTest("diff(cos(x),x)","-sin(x)");
469         simplifyTest("diff(tan(x),x)","1/((cos(x))^2)");
470
471         simplifyTest("diff(sec(x),x)","sec(x)*tan(x)");
472         simplifyTest("diff(cosec(x),x)","-cosec(x) * cot(x)");
473         simplifyTest("diff(cot(x),x)","-(cosec(x))^2");
474         
475         simplifyTest("diff(sec(x),x)","sec(x) * tan(x)");
476         simplifyTest("diff(cosec(x),x)","-cosec(x) * cot(x)");
477         simplifyTest("diff(cot(x),x)","-(cosec(x))^2");
478             
479         simplifyTest("diff(asin(x),x)","1/(sqrt(1-x^2))");
480         simplifyTest("diff(acos(x),x)","-1/(sqrt(1-x^2))");
481         simplifyTest("diff(atan(x),x)","1/(1+x^2)");
482
483         simplifyTest("diff(sinh(x),x)","cosh(x)");
484         simplifyTest("diff(cosh(x),x)","sinh(x)");
485         simplifyTest("diff(tanh(x),x)","1-(tanh(x))^2");
486
487         simplifyTest("diff(asinh(x),x)","1/(sqrt(1+x^2))");
488         simplifyTest("diff(acosh(x),x)","1/(sqrt(x^2-1))");
489         simplifyTest("diff(atanh(x),x)","1/(1-x^2)");
490
491         simplifyTest("diff(sqrt(x),x)","1/(2 (sqrt(x)))");
492         
493         simplifyTest("diff(exp(x),x)","exp(x)");
494 // simplifyTest("diff(pow(x,y),x)","y*(pow(x,y-1))");
495 // simplifyTest("diff(pow(x,y),y)","(ln(x)) (pow(x,y))");
496         simplifyTest("diff(ln(x),x)","1/x");
497         simplifyTest("diff(log(x),x)","(1/ln(10)) /x");
498         simplifyTest("diff(abs(x),x)","abs(x)/x");
499         simplifyTest("diff(angle(x,y),x)","y/(x^2+y^2)");
500         simplifyTest("diff(angle(x,y),y)","-x/(x^2+y^2)");
501         simplifyTest("diff(mod(x,y),x)","1");
502         simplifyTest("diff(mod(x,y),y)","0");
503         simplifyTest("diff(sum(x,x^2,x^3),x)","sum(1,2 x,3 x^2)");
504
505 // addDiffRule(new PassThroughDiffRule(this,"sum"));
506 // addDiffRule(new PassThroughDiffRule(this,"re"));
507 // addDiffRule(new PassThroughDiffRule(this,"im"));
508 // addDiffRule(new PassThroughDiffRule(this,"rand"));
509 //
510 // MacroFunction complex = new MacroFunction("complex",2,"x+i*y",xjep);
511 // xjep.addFunction("complex",complex);
512 // addDiffRule(new MacroFunctionDiffRules(this,complex));
513 //
514 // addDiffRule(new PassThroughDiffRule(this,"\"<\"",new Comparative(0)));
515 // addDiffRule(new PassThroughDiffRule(this,"\">\"",new Comparative(1)));
516 // addDiffRule(new PassThroughDiffRule(this,"\"<=\"",new Comparative(2)));
517 // addDiffRule(new PassThroughDiffRule(this,"\">=\"",new Comparative(3)));
518 // addDiffRule(new PassThroughDiffRule(this,"\"!=\"",new Comparative(4)));
519 // addDiffRule(new PassThroughDiffRule(this,"\"==\"",new Comparative(5)));
520     }
521
522     public void myAssertEquals(String msg,String actual,String expected)
523     {
524         if(!actual.equals(expected))
525             System.out.println("Error \""+msg+"\" is \""+actual+" should be "+expected+"\"");
526         assertEquals("<"+msg+">",expected,actual);
527         System.out.println("Success: Value of \""+msg+"\" is \""+actual+"\"");
528     }
529     public void testAssignDiff() throws ParseException
530     {
531         simplifyTestString("y=x^5","y=x^5.0");
532         simplifyTestString("z=diff(y,x)","z=5.0*x^4.0");
533         Node n1 = ((DSymbolTable) j.getSymbolTable()).getPartialDeriv("y",new String[]{"x"}).getEquation();
534         myAssertEquals("dy/dx","5.0*x^4.0",j.toString(n1));
535         simplifyTestString("w=diff(z,x)","w=20.0*x^3.0");
536         Node n2 = ((DSymbolTable) j.getSymbolTable()).getPartialDeriv("y",new String[]{"x","x"}).getEquation();
537         myAssertEquals("d^2y/dxdx","20.0*x^3.0",j.toString(n2));
538         j.getSymbolTable().clearValues();
539         valueTest("x=2",2);
540         valueTest("y",32); // x^5
541         valueTest("z",80); // 5 x^4
542         valueTest("w",160); // 20 x^3
543     }
544
545     public void testMatrix() throws ParseException
546     {
547         j.getSymbolTable().clearValues();
548         valueTest("x=2",2);
549         valueTest("y=[x^3,x^2,x]","[8.0,4.0,2.0]");
550         valueTest("z=diff(y,x)","[12.0,4.0,1.0]");
551         valueTest("3*y","[24.0,12.0,6.0]");
552         valueTest("y*4","[32.0,16.0,8.0]");
553         valueTest("y*z","[[96.0,32.0,8.0],[48.0,16.0,4.0],[24.0,8.0,2.0]]");
554         valueTest("z*y","[[96.0,48.0,24.0],[32.0,16.0,8.0],[8.0,4.0,2.0]]");
555         valueTest("w=y^z","[-4.0,16.0,-16.0]");
556         simplifyTestString("diff(w,x)","[3.0*x^2.0,2.0*x,1.0]^z+y^[6.0*x,2.0,0.0]");
557         simplifyTestString("diff(y . z,x)","[3.0*x^2.0,2.0*x,1.0].z+y.[6.0*x,2.0,0.0]");
558         valueTest("w.y",0.0);
559         valueTest("w.z",0.0);
560         valueTest("sqrt(w . z)",0.0);
561         valueTest("sqrt([3,4].[3,4])",5.0); // tests result is unwrapped from scaler
562         valueTest("y+z","[20.0,8.0,3.0]");
563         valueTest("y-z","[-4.0,0.0,1.0]");
564         j.getSymbolTable().clearValues();
565         // the following two tests insure that ^ is printed correctly
566         simplifyTestString("y^z","y^z");
567         simplifyTestString("[8.0,4.0,2.0]^[12.0,4.0,1.0]","[8.0,4.0,2.0]^[12.0,4.0,1.0]");
568         simplifyTestString("y=[cos(x),sin(x)]","y=[cos(x),sin(x)]");
569         simplifyTestString("z=diff(y,x)","z=[-sin(x),cos(x)]");
570         valueTest("y.y",1.0);
571         valueTest("y.z",0.0);
572         valueTest("z.z",1.0);
573         j.getSymbolTable().clearValues();
574         valueTest("x=[[1,2],[3,4]]","[[1.0,2.0],[3.0,4.0]]");
575         valueTest("y=[1,-1]","[1.0,-1.0]");
576         valueTest("x*y","[-1.0,-1.0]");
577         valueTest("y*x","[-2.0,-2.0]");
578         valueTest("x+[y,y]","[[2.0,1.0],[4.0,3.0]]");
579         valueTest("ele(y,1)","1.0"); // Value: 2.0
580         valueTest("ele(y,2)","-1.0"); // Value: 2.0
581         valueTest("ele(x,[1,1])","1.0"); // Value: 2.0
582         valueTest("ele(x,[1,2])","2.0"); // Value: 2.0
583         valueTest("ele(x,[2,1])","3.0"); // Value: 2.0
584         valueTest("ele(x,[2,2])","4.0"); // Value: 2.0
585     }
586     public void testBad() throws ParseException
587     {
588         if(SHOW_BAD)
589         {
590             simplifyTest("1&&(1||x)","1");
591             simplifyTest("diff(sgn(x),x)","0"); // sgn not implemented
592             simplifyTest("diff(re(x+i y),x)","1"); // not smart enought to work out re(i) = 1
593             simplifyTest("diff(re(x+i y),y)","0");
594             simplifyTest("diff(im(x+i y),x)","0");
595             simplifyTest("diff(im(x+i y),y)","1");
596             simplifyTest("(x/2)*3","x*1.5");
597         }
598     }
599     */

600 }
601
Popular Tags