KickJava   Java API By Example, From Geeks To Geeks.

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


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.djep.*;
7 import org.lsmp.djep.matrixJep.*;
8 /* @author rich
9  * Created on 19-Nov-2003
10  *
11  * This code is covered by a Creative Commons
12  * Attribution, Non Commercial, Share Alike license
13  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
14  */

15
16 /**
17  * JUnit test for full MatrixJep
18  *
19  * @author Rich Morris
20  * Created on 19-Nov-2003
21  */

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

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

126     }
127
128     public void simplifyTestString(String JavaDoc expr,String JavaDoc expected) throws ParseException
129     {
130         Node node = j.parse(expr);
131         Node matEqn = j.preprocess(node);
132         String JavaDoc res = j.toString(matEqn);
133         
134         if(!expected.equals(res))
135             System.out.println("Error: Value of \""+expr+"\" is \""+res+"\" should be \""+expected+"\"");
136         assertEquals("<"+expr+">",expected,res);
137         System.out.println("Sucess: Value of \""+expr+"\" is \""+res+"\"");
138             
139 // System.out.print("Full Brackets:\t");
140
// j.pv.setFullBrackets(true);
141
// j.pv.println(simp);
142
// j.pv.setFullBrackets(false);
143

144     }
145
146     public void testSimpleSum() throws ParseException
147     {
148         valueTest("1+2",3);
149         valueTest("2*6+3",15);
150         valueTest("2*(6+3)",18);
151     }
152     
153     public void testOperators() throws ParseException
154     {
155 // if(!Operator.OP_MULTIPLY.isDistributiveOver(Operator.OP_ADD))
156
// fail("* should be distrib over +");
157
// if(Operator.OP_MULTIPLY.isDistributiveOver(Operator.OP_DIVIDE))
158
// fail("* should not be distrib over /");
159
// if(Operator.OP_MULTIPLY.getPrecedence() > Operator.OP_ADD.getPrecedence())
160
// fail("* should have a lower precedence than +");
161

162         valueTest("T=1",1);
163         valueTest("F=0",0);
164         calcValue("a=F"); calcValue("b=F"); calcValue("c=F");
165         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
166         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
167         calcValue("a=F"); calcValue("b=F"); calcValue("c=T");
168         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
169         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
170         calcValue("a=F"); calcValue("b=T"); calcValue("c=F");
171         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
172         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
173         calcValue("a=F"); calcValue("b=T"); calcValue("c=T");
174         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
175         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
176
177         calcValue("a=T"); calcValue("b=F"); calcValue("c=F");
178         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
179         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
180         calcValue("a=T"); calcValue("b=F"); calcValue("c=T");
181         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
182         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
183         calcValue("a=T"); calcValue("b=T"); calcValue("c=F");
184         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
185         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
186         calcValue("a=T"); calcValue("b=T"); calcValue("c=T");
187         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
188         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
189     }
190     
191     public void testSimp() throws ParseException
192     {
193         simplifyTest("2+3","5");
194         simplifyTest("2*3","6");
195         simplifyTest("2^3","8");
196         simplifyTest("3/2","1.5");
197         simplifyTest("2*3+4","10");
198         simplifyTest("2*(3+4)","14");
199
200         simplifyTest("0+x","x");
201         simplifyTest("x+0","x");
202         simplifyTest("0-x","0-x");
203         simplifyTest("x-0","x");
204         simplifyTest("0*x","0");
205         simplifyTest("x*0","0");
206         simplifyTest("1*x","x");
207         simplifyTest("x*1","x");
208         simplifyTest("-1*x","-x");
209         simplifyTest("x*-1","-x");
210         simplifyTest("-(-x)","x");
211         simplifyTest("-(-(-x))","-x");
212         simplifyTest("(-1)*(-1)*x","x");
213         simplifyTest("(-1)*(-1)*(-1)*x","-x");
214         
215         simplifyTest("0/x","0");
216         simplifyTest("x/0","1/0");
217         
218         simplifyTest("x^0","1");
219         simplifyTest("x^1","x");
220         simplifyTest("0^x","0");
221         simplifyTest("1^x","1");
222
223         // (a+b)+c
224
simplifyTest("(2+3)+x","5+x");
225         simplifyTest("(2+x)+3","5+x");
226         simplifyTest("(x+2)+3","5+x");
227         // a+(b+c)
228
simplifyTest("x+(2+3)","5+x");
229         simplifyTest("2+(x+3)","5+x");
230         simplifyTest("2+(3+x)","5+x");
231         // (a+b)-c
232
simplifyTest("(2+3)-x","5-x");
233         simplifyTest("(2+x)-3","x-1");
234         simplifyTest("(x+2)-3","x-1");
235         // (a-b)+c
236
simplifyTest("(2-3)+x","-1+x");
237         simplifyTest("(2-x)+3","5-x");
238         simplifyTest("(x-2)+3","1+x");
239         // a-(b+c)
240
simplifyTest("x-(2+3)","x-5");
241         simplifyTest("2-(x+3)","-1-x");
242         simplifyTest("2-(3+x)","-1-x");
243         // a+(b-c)
244
simplifyTest("x+(2-3)","x-1");
245         simplifyTest("2+(x-3)","-1+x");
246         simplifyTest("2+(3-x)","5-x");
247         // a-(b-c)
248
simplifyTest("x-(2-3)","1+x");
249         simplifyTest("2-(x-3)","5-x");
250         simplifyTest("2-(3-x)","-1+x");
251         // (a-b)-c
252
simplifyTest("(2-3)-x","-1-x");
253         simplifyTest("(2-x)-3","-1-x");
254         simplifyTest("(x-2)-3","x-5");
255
256         // (a*b)*c
257
simplifyTest("(2*3)*x","6*x");
258         simplifyTest("(2*x)*3","6*x");
259         simplifyTest("(x*2)*3","6*x");
260         // a+(b+c)
261
simplifyTest("x*(2*3)","6*x");
262         simplifyTest("2*(x*3)","6*x");
263         simplifyTest("2*(3*x)","6*x");
264         // (a+b)-c
265
simplifyTest("(2*3)/x","6/x");
266         simplifyTest("(3*x)/2","1.5*x");
267         simplifyTest("(x*3)/2","1.5*x");
268         // (a-b)+c
269
simplifyTest("(3/2)*x","1.5*x");
270         simplifyTest("(3/x)*2","6/x");
271         simplifyTest("(x/2)*3","1.5*x");
272         // a-(b+c)
273
simplifyTest("x/(2*3)","x/6");
274         simplifyTest("3/(x*2)","1.5/x");
275         simplifyTest("3/(2*x)","1.5/x");
276         // a+(b-c)
277
simplifyTest("x*(3/2)","1.5*x");
278         simplifyTest("3*(x/2)","1.5*x");
279         simplifyTest("3*(2/x)","6/x");
280         // a-(b-c)
281
simplifyTest("x/(3/2)","x/1.5");
282         simplifyTest("2/(x/3)","6/x");
283         simplifyTest("3/(2/x)","1.5*x");
284         // (a-b)-c
285
simplifyTest("(3/2)/x","1.5/x");
286         simplifyTest("(3/x)/2","1.5/x");
287         simplifyTest("(x/3)/2","x/6");
288
289
290         simplifyTest("x*(3+2)","5*x");
291         simplifyTest("3*(x+2)","6+3*x");
292         simplifyTest("3*(2+x)","6+3*x");
293         simplifyTest("(3+2)*x","5*x");
294         simplifyTest("(3+x)*2","6+2*x");
295         simplifyTest("(x+3)*2","6+x*2");
296
297         simplifyTest("x*(3-2)","x");
298         simplifyTest("3*(x-2)","-6+3*x");
299         simplifyTest("3*(2-x)","6-3*x");
300         simplifyTest("(3-2)*x","x");
301         simplifyTest("(3-x)*2","6-2*x");
302         simplifyTest("(x-3)*2","-6+2*x");
303
304         simplifyTest("3+(x/4)","3+x/4");
305         simplifyTest("2*(x/4)","0.5*x");
306         simplifyTest("(2*(3+(x/4)))","6+0.5*x");
307         simplifyTest("1+(2*(3+(x/4)))","7+0.5*x");
308         simplifyTest("((3+(x/4))*2)+1","7+0.5*x");
309         
310     }
311
312     public void testComplex() throws Exception JavaDoc
313     {
314         double tol = 0.00000001;
315
316         complexValueTest("z=complex(3,2)",new Complex(3,2),tol);
317         complexValueTest("z*z-z",new Complex(2,10),tol);
318         complexValueTest("z^3",new Complex(-9,46),tol);
319         complexValueTest("(z*z-z)/z",new Complex(2,2),tol);
320         complexValueTest("w=polar(2,pi/2)",new Complex(0,2),tol);
321         
322     }
323
324     public void testIf() throws ParseException
325     {
326         valueTest("if(1,2,3)",2);
327         valueTest("if(-1,2,3)",3);
328         valueTest("if(0,2,3)",3);
329         valueTest("if(1,2,3,4)",2);
330         valueTest("if(-1,2,3,4)",3);
331         valueTest("if(0,2,3,4)",4);
332         valueTest("if(0>=0,2,3,4)",2);
333         valueTest("x=3",3);
334         valueTest("if(x==3,1,-1)",1);
335         valueTest("if(x!=3,1,-1)",-1);
336         valueTest("if(x>=3,1,-1)",1);
337         valueTest("if(x>3,1,-1)",-1);
338         valueTest("if(x<=3,1,-1)",1);
339         valueTest("if(x<3,1,-1)",-1);
340     }
341
342     public void testAssign() throws ParseException
343     {
344         valueTest("x=3",3);
345         valueTest("y=3+4",7);
346         valueTest("z=x+y",10);
347         valueTest("a=b=c=z",10);
348         valueTest("b",10);
349         valueTest("d=f=a-b",0);
350     }
351
352                         
353     public void testDiff() throws ParseException
354     {
355         simplifyTest("diff(x^2,x)","2 x");
356         simplifyTest("diff(x^3,x)","3 x^2");
357         simplifyTest("diff(x,x)","1");
358         simplifyTest("diff(1,x)","0");
359         simplifyTest("diff(x^2+x+1,x)","2 x+1");
360         simplifyTest("diff((x+x^2)*(x+x^3),x)","(1+2*x)*(x+x^3)+(x+x^2)*(1+3*x^2)");
361         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))");
362         simplifyTest("diff(sin(x),x)","cos(x)");
363         simplifyTest("diff(-(x-5)^3,x)","-(3.0*(x-5.0)^2.0)");
364         
365
366         simplifyTest("diff((x+1)^2,x)","2+2*x");
367         simplifyTest("diff((x+y)^2,x)","2*(x+y)");
368         simplifyTest("diff((x+x^2)^3,x)","3*(x+x^2)^2*(1+2*x)");
369         
370         simplifyTest("diff(sin(x+1),x)","cos(x+1)");
371         simplifyTest("diff(sin(x+x^2),x)","cos(x+x^2)*(1+2*x)");
372
373         simplifyTest("diff(cos(x),x)","-sin(x)");
374         simplifyTest("diff(tan(x),x)","1/((cos(x))^2)");
375
376         simplifyTest("diff(sec(x),x)","sec(x)*tan(x)");
377         simplifyTest("diff(cosec(x),x)","-cosec(x) * cot(x)");
378         simplifyTest("diff(cot(x),x)","-(cosec(x))^2");
379         
380         simplifyTest("diff(sec(x),x)","sec(x) * tan(x)");
381         simplifyTest("diff(cosec(x),x)","-cosec(x) * cot(x)");
382         simplifyTest("diff(cot(x),x)","-(cosec(x))^2");
383             
384         simplifyTest("diff(asin(x),x)","1/(sqrt(1-x^2))");
385         simplifyTest("diff(acos(x),x)","-1/(sqrt(1-x^2))");
386         simplifyTest("diff(atan(x),x)","1/(1+x^2)");
387
388         simplifyTest("diff(sinh(x),x)","cosh(x)");
389         simplifyTest("diff(cosh(x),x)","sinh(x)");
390         simplifyTest("diff(tanh(x),x)","1-(tanh(x))^2");
391
392         simplifyTest("diff(asinh(x),x)","1/(sqrt(1+x^2))");
393         simplifyTest("diff(acosh(x),x)","1/(sqrt(x^2-1))");
394         simplifyTest("diff(atanh(x),x)","1/(1-x^2)");
395
396         simplifyTest("diff(sqrt(x),x)","1/(2 (sqrt(x)))");
397         
398         simplifyTest("diff(exp(x),x)","exp(x)");
399 // simplifyTest("diff(pow(x,y),x)","y*(pow(x,y-1))");
400
// simplifyTest("diff(pow(x,y),y)","(ln(x)) (pow(x,y))");
401
simplifyTest("diff(ln(x),x)","1/x");
402         simplifyTest("diff(log(x),x)","(1/ln(10)) /x");
403         simplifyTest("diff(abs(x),x)","abs(x)/x");
404         simplifyTest("diff(angle(x,y),x)","y/(x^2+y^2)");
405         simplifyTest("diff(angle(x,y),y)","-x/(x^2+y^2)");
406         simplifyTest("diff(mod(x,y),x)","1");
407         simplifyTest("diff(mod(x,y),y)","0");
408         simplifyTest("diff(sum(x,x^2,x^3),x)","sum(1,2 x,3 x^2)");
409
410 // addDiffRule(new PassThroughDiffRule(this,"sum"));
411
// addDiffRule(new PassThroughDiffRule(this,"re"));
412
// addDiffRule(new PassThroughDiffRule(this,"im"));
413
// addDiffRule(new PassThroughDiffRule(this,"rand"));
414
//
415
// MacroFunction complex = new MacroFunction("complex",2,"x+i*y",xjep);
416
// xjep.addFunction("complex",complex);
417
// addDiffRule(new MacroFunctionDiffRules(this,complex));
418
//
419
// addDiffRule(new PassThroughDiffRule(this,"\"<\"",new Comparative(0)));
420
// addDiffRule(new PassThroughDiffRule(this,"\">\"",new Comparative(1)));
421
// addDiffRule(new PassThroughDiffRule(this,"\"<=\"",new Comparative(2)));
422
// addDiffRule(new PassThroughDiffRule(this,"\">=\"",new Comparative(3)));
423
// addDiffRule(new PassThroughDiffRule(this,"\"!=\"",new Comparative(4)));
424
// addDiffRule(new PassThroughDiffRule(this,"\"==\"",new Comparative(5)));
425
}
426
427     public void myAssertEquals(String JavaDoc msg,String JavaDoc actual,String JavaDoc expected)
428     {
429         if(!actual.equals(expected))
430             System.out.println("Error \""+msg+"\" is \""+actual+" should be "+expected+"\"");
431         assertEquals("<"+msg+">",expected,actual);
432         System.out.println("Success: Value of \""+msg+"\" is \""+actual+"\"");
433     }
434     public void testAssignDiff() throws ParseException
435     {
436         simplifyTestString("y=x^5","y=x^5.0");
437         simplifyTestString("z=diff(y,x)","z=5.0*x^4.0");
438         Node n1 = ((DSymbolTable) j.getSymbolTable()).getPartialDeriv("y",new String JavaDoc[]{"x"}).getEquation();
439         myAssertEquals("dy/dx","5.0*x^4.0",j.toString(n1));
440         simplifyTestString("w=diff(z,x)","w=20.0*x^3.0");
441         Node n2 = ((DSymbolTable) j.getSymbolTable()).getPartialDeriv("y",new String JavaDoc[]{"x","x"}).getEquation();
442         myAssertEquals("d^2y/dxdx","20.0*x^3.0",j.toString(n2));
443         j.getSymbolTable().clearValues();
444         valueTest("x=2",2);
445         valueTest("y",32); // x^5
446
valueTest("z",80); // 5 x^4
447
valueTest("w",160); // 20 x^3
448
}
449
450     public void testMatrix() throws ParseException
451     {
452         j.getSymbolTable().clearValues();
453         valueTest("x=2",2);
454         valueTest("y=[x^3,x^2,x]","[8.0,4.0,2.0]");
455         valueTest("z=diff(y,x)","[12.0,4.0,1.0]");
456         valueTest("3*y","[24.0,12.0,6.0]");
457         valueTest("y*4","[32.0,16.0,8.0]");
458         valueTest("y*z","[[96.0,32.0,8.0],[48.0,16.0,4.0],[24.0,8.0,2.0]]");
459         valueTest("z*y","[[96.0,48.0,24.0],[32.0,16.0,8.0],[8.0,4.0,2.0]]");
460         valueTest("w=y^z","[-4.0,16.0,-16.0]");
461         simplifyTestString("diff(w,x)","[3.0*x^2.0,2.0*x,1.0]^z+y^[6.0*x,2.0,0.0]");
462         simplifyTestString("diff(y . z,x)","[3.0*x^2.0,2.0*x,1.0].z+y.[6.0*x,2.0,0.0]");
463         valueTest("w.y",0.0);
464         valueTest("w.z",0.0);
465         valueTest("sqrt(w . z)",0.0);
466         valueTest("sqrt([3,4].[3,4])",5.0); // tests result is unwrapped from scaler
467
valueTest("y+z","[20.0,8.0,3.0]");
468         valueTest("y-z","[-4.0,0.0,1.0]");
469         j.getSymbolTable().clearValues();
470         // the following two tests insure that ^ is printed correctly
471
simplifyTestString("y^z","y^z");
472         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]");
473         simplifyTestString("y=[cos(x),sin(x)]","y=[cos(x),sin(x)]");
474         simplifyTestString("z=diff(y,x)","z=[-sin(x),cos(x)]");
475         valueTest("y.y",1.0);
476         valueTest("y.z",0.0);
477         valueTest("z.z",1.0);
478         j.getSymbolTable().clearValues();
479         valueTest("x=[[1,2],[3,4]]","[[1.0,2.0],[3.0,4.0]]");
480         valueTest("y=[1,-1]","[1.0,-1.0]");
481         valueTest("x*y","[-1.0,-1.0]");
482         valueTest("y*x","[-2.0,-2.0]");
483         valueTest("x+[y,y]","[[2.0,1.0],[4.0,3.0]]");
484         valueTest("ele(y,1)","1.0"); // Value: 2.0
485
valueTest("ele(y,2)","-1.0"); // Value: 2.0
486
valueTest("ele(x,[1,1])","1.0"); // Value: 2.0
487
valueTest("ele(x,[1,2])","2.0"); // Value: 2.0
488
valueTest("ele(x,[2,1])","3.0"); // Value: 2.0
489
valueTest("ele(x,[2,2])","4.0"); // Value: 2.0
490
}
491     public void testBad() throws ParseException
492     {
493         if(SHOW_BAD)
494         {
495             simplifyTest("1&&(1||x)","1");
496             simplifyTest("diff(sgn(x),x)","0"); // sgn not implemented
497
simplifyTest("diff(re(x+i y),x)","1"); // not smart enought to work out re(i) = 1
498
simplifyTest("diff(re(x+i y),y)","0");
499             simplifyTest("diff(im(x+i y),x)","0");
500             simplifyTest("diff(im(x+i y),y)","1");
501             simplifyTest("(x/2)*3","x*1.5");
502         }
503     }
504 }
505
Popular Tags