KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > symbolic > Evaluation


1 package JSci.maths.symbolic;
2  
3 import JSci.maths.*;
4 import JSci.maths.fields.*;
5 import JSci.maths.groups.*;
6 import JSci.maths.analysis.*;
7
8 import java.util.*;
9
10 /** This class associates a <code>JSci.maths.analysis.RealFunction</code> to
11  * an <code>Expression</code> argument, to generate an <code>Expression</code>.
12  * <br>
13  * This class will substitute the Function class.
14  * <br>
15  * See the <code>main()</code> for example.
16  */

17 public class Evaluation extends Expression {
18
19     private final RealFunction fn;
20     private final Expression arg;
21
22     /**
23     * @param n the function; for example,
24     * @param a the argument
25     */

26     public Evaluation(RealFunction n,Expression a) {
27     fn=n;
28     arg=a;
29     }
30
31     public String JavaDoc toString() { return "f<"+fn+">"+"("+arg+")"; }
32
33     public int getPriority() {return 15;}
34
35     public Expression differentiate(Variable x) {
36     return Expression.product(
37                   new Evaluation(fn.differentiate(),arg),
38                   arg.differentiate(x)
39                   );
40     }
41
42     public boolean equals(Object JavaDoc o) {
43     if (!Evaluation.class.isInstance(o)) return false;
44     Evaluation f = (Evaluation)o;
45     return (fn.equals(f.fn) && arg.equals(f.arg));
46     }
47
48     public Object JavaDoc getSet() { return RealField.getInstance(); }
49     
50     public Expression evaluate() {
51     Expression sarg = arg.evaluate();
52     
53     if (sarg instanceof Constant) {
54         MathDouble a = (MathDouble)((Constant)sarg).getValue();
55         return new Constant(new MathDouble(fn.map(a.doubleValue())));
56     }
57
58     return new Evaluation(fn,sarg);
59
60     }
61
62     /** An example */
63     public static void main(String JavaDoc [] args) {
64     Variable xVar = new Variable("x",RealField.getInstance());
65     Expression expr = new Evaluation(new Exponential(1.0,1.0,0.0),xVar);
66     System.out.println("expr = "+expr);
67     xVar.setValue(new MathDouble(1.0));
68     System.out.println("x = 1");
69     System.out.println("expr = "+expr.evaluate());
70     xVar.setValue(null);
71     System.out.println("d/dx expr = "+expr.differentiate(xVar).evaluate());
72     }
73     
74
75 }
76
77
78
79
Popular Tags