KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.symbolic;
2  
3 import JSci.maths.*;
4
5 import JSci.maths.*;
6 import JSci.maths.fields.*;
7 import JSci.maths.groups.*;
8
9 import java.util.*;
10 import java.io.*;
11
12 /** The basic abstract class for doing analytical calculations.
13 */

14
15 public abstract class Expression implements Field.Member {
16
17     /** Differentiation of the expression with respect to the variables
18      * @param ht table with variable / derivative order
19      */

20     public Expression differentiate(Hashtable ht) {
21     Expression r = this;
22     Variable x;
23     for (Enumeration e = ht.keys();e.hasMoreElements();) {
24         x=(Variable)e.nextElement();
25         for (int n=0;n<((Integer JavaDoc)ht.get(x)).intValue();n++)
26         r=r.differentiate(x);
27     }
28     return r;
29     }
30
31     /** Differentiation of the expression with respect to a variable
32      * @param x the variable
33      */

34     public abstract Expression differentiate(Variable x);
35     
36     /** This method substitutes the variable with the variable
37      * values, if non-null; they can be <code>Contant</code>s or
38      * other <code>Expression</code>s. Simplification is performed.
39      * @return the evaluated Expression. Can be a Constant.
40      */

41     public abstract Expression evaluate();
42
43     /** Get the priority of the operator described by the expression.
44      * This is needed only for allowing toString()
45      * to generate the parenthesis when needed.
46      * @return the priority
47      */

48     protected abstract int getPriority();
49
50
51
52     ///////////////////////////////////////////////////////////////////////
53
// For implementing Field
54

55     /**
56      * @return the inverse member.
57      */

58     public Field.Member inverse() {
59     return new Power(this,-1);
60     }
61     
62     /**
63      * The multiplication law with inverse.
64      * @param f a field member
65      */

66     public Field.Member divide(Field.Member f) {
67     Expression e;
68     if (f instanceof Expression) e=(Expression)f.inverse();
69     else e=new Constant(f.inverse());
70     return new Product(this,e);
71     }
72     
73     /**
74      * The multiplication law.
75      * @param r a ring member
76      */

77     public Ring.Member multiply(Ring.Member r) {
78     Expression e;
79     if (r instanceof Expression) e=(Expression)r;
80     else e=new Constant(r);
81     return new Product(this,e);
82     }
83
84     /**
85      * The group composition law.
86      * @param g a group member
87      */

88     public AbelianGroup.Member add(AbelianGroup.Member g) {
89     Expression e;
90     if (g instanceof Expression) e=(Expression)g;
91     else e=new Constant(g);
92     return new Sum(this,e);
93     }
94
95     /**
96      * Returns the negative member.
97      */

98     public AbelianGroup.Member negate() {
99     return new Negative(this);
100     }
101
102     /**
103      * The group composition law with inverse.
104      * @param g a group member
105      */

106     public AbelianGroup.Member subtract(AbelianGroup.Member g) {
107     Expression e;
108     if (g instanceof Expression) e=(Expression)g.negate();
109     else e=new Constant(g.negate());
110     return new Sum(this,e);
111     }
112
113     // other operations
114
/** Power of an expression. Only constant and integer exponent.
115      * @param f the exponent.
116      */

117     public Ring.Member rise(int f) {
118     return new Power(this, f);
119     }
120
121
122
123     ////////////////////////////////////////////////////////////////////
124
// static methods for making operations
125

126     /** Method for generating an Expression by a formal operation on
127      * a set member
128      */

129     public static Expression negative(AbelianGroup.Member r) {
130     if (r instanceof Expression) return (Expression)r.negate();
131     else return new Constant(r.negate());
132     }
133
134     /** Method for generating an Expression by a formal operation on
135      * a set member
136      */

137     public static Expression sum(AbelianGroup.Member a, AbelianGroup.Member b) {
138     Expression ea,eb;
139     if (a instanceof Expression) ea=(Expression)a;
140     else ea=new Constant(a);
141     if (b instanceof Expression) eb=(Expression)b;
142     else eb=new Constant(b);
143     return (Expression)ea.add(eb);
144     }
145
146     /** Method for generating an Expression by a formal operation on
147      * a set member
148      */

149     public static Expression sum(AbelianGroup.Member[] args) {
150     Expression[] exprs = new Expression[args.length];
151     for (int j=0;j<args.length;j++) {
152         if (args[j] instanceof Expression) exprs[j]=(Expression)args[j];
153         else exprs[j]=new Constant(args[j]);
154     }
155     return new Sum(exprs);
156     }
157
158     /** Method for generating an Expression by a formal operation on
159      * a set member
160      */

161     public static Expression difference(AbelianGroup.Member a, AbelianGroup.Member b) {
162     Expression ea,eb;
163     if (a instanceof Expression) ea=(Expression)a;
164     else ea=new Constant(a);
165     if (b instanceof Expression) eb=(Expression)b;
166     else eb=new Constant(b);
167     return (Expression)ea.subtract(eb);
168     }
169
170     /** Method for generating an Expression by a formal operation on
171      * a set member
172      */

173     public static Expression product(Ring.Member a, Ring.Member b) {
174     Expression ea,eb;
175     if (a instanceof Expression) ea=(Expression)a;
176     else ea=new Constant(a);
177     if (b instanceof Expression) eb=(Expression)b;
178     else eb=new Constant(b);
179     return (Expression)ea.multiply(eb);
180     }
181
182     /** Method for generating an Expression by a formal operation on
183      * a set member
184      */

185     public static Expression product(Ring.Member[] args) {
186     Expression[] exprs = new Expression[args.length];
187     for (int j=0;j<args.length;j++) {
188         if (args[j] instanceof Expression) exprs[j]=(Expression)args[j];
189         else exprs[j]=new Constant(args[j]);
190     }
191     return new Product(exprs);
192     }
193
194     /** Method for generating an Expression by a formal operation on
195      * a set member
196      */

197     public static Expression inverse(Field.Member f) {
198     if (f instanceof Expression) return (Expression)f.inverse();
199     else return new Constant(f.inverse());
200     }
201
202     /** Method for generating an Expression by a formal operation on
203      * a set member
204      */

205     public static Expression divide(Field.Member f,Field.Member g) {
206     Expression ef,eg;
207     if (f instanceof Expression) ef=(Expression)f;
208     else ef=new Constant(f);
209     if (g instanceof Expression) eg=(Expression)g;
210     else eg=new Constant(g);
211     return (Expression)ef.divide(eg);
212     }
213
214     /** Method for generating an Expression by a formal operation on
215      * a set member
216      */

217     public static Expression power(Field.Member f,int g) {
218     Expression ef;
219     if (f instanceof Expression) ef=(Expression)f;
220     else ef=new Constant(f);
221     return new Power(ef, g);
222     }
223
224
225
226
227     /** Put some expressions like "sin(2*x)/x+3/z" in the command line.
228      * The program writes the expression, the simplifyed expression and
229      * the derivative.
230      */

231     public static void main(String JavaDoc args[]) throws ParseException {
232     for (int j=0;j<args.length;j++) {
233         Map vars=new Hashtable();
234         vars.put("y",new Variable("y",RealField.getInstance()));
235         Expression e=ExpressionParser.parse(args[j],vars);
236         System.out.println("F = "+e);
237         System.out.println("F = "+e.evaluate());
238         System.out.println("dF/dx = "+e.differentiate((Variable)vars.get("x")).evaluate());
239         System.out.println("dF/dy = "+e.differentiate((Variable)vars.get("y")).evaluate());
240         System.out.println(" ");
241     }
242     }
243
244 }
245
Popular Tags