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 14 15 public abstract class Expression implements Field.Member { 16 17 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 )ht.get(x)).intValue();n++) 26 r=r.differentiate(x); 27 } 28 return r; 29 } 30 31 34 public abstract Expression differentiate(Variable x); 35 36 41 public abstract Expression evaluate(); 42 43 48 protected abstract int getPriority(); 49 50 51 52 55 58 public Field.Member inverse() { 59 return new Power(this,-1); 60 } 61 62 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 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 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 98 public AbelianGroup.Member negate() { 99 return new Negative(this); 100 } 101 102 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 117 public Ring.Member rise(int f) { 118 return new Power(this, f); 119 } 120 121 122 123 126 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 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 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 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 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 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 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 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 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 231 public static void main(String 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 |