1 9 package org.nfunk.jep.function; 10 11 import java.util.*; 12 import org.nfunk.jep.*; 13 import org.nfunk.jep.type.*; 14 15 public class Divide extends PostfixMathCommand 16 { 17 public Divide() 18 { 19 numberOfParameters = 2; 20 } 21 22 public void run(Stack inStack) 23 throws ParseException 24 { 25 checkStack(inStack); Object param2 = inStack.pop(); 27 Object param1 = inStack.pop(); 28 inStack.push(div(param1, param2)); return; 30 } 31 32 public Object div(Object param1, Object param2) 33 throws ParseException 34 { 35 if (param1 instanceof Complex) 36 { 37 if (param2 instanceof Complex) 38 return div((Complex)param1, (Complex)param2); 39 else if (param2 instanceof Number ) 40 return div((Complex)param1, (Number )param2); 41 else if (param2 instanceof Vector) 42 return div((Complex)param1, (Vector)param2); 43 } 44 else if (param1 instanceof Number ) 45 { 46 if (param2 instanceof Complex) 47 return div((Number )param1, (Complex)param2); 48 else if (param2 instanceof Number ) 49 return div((Number )param1, (Number )param2); 50 else if (param2 instanceof Vector) 51 return div((Number )param1, (Vector)param2); 52 } 53 else if (param1 instanceof Vector) 54 { 55 if (param2 instanceof Complex) 56 return div((Vector)param1, (Complex)param2); 57 else if (param2 instanceof Number ) 58 return div((Vector)param1, (Number )param2); 59 } 60 61 throw new ParseException("Invalid parameter type"); 62 } 63 64 65 public Double div(Number d1, Number d2) 66 { 67 return new Double (d1.doubleValue() / d2.doubleValue()); 68 } 69 70 public Complex div(Complex c1, Complex c2) 71 { 72 return c1.div(c2); 73 } 74 75 public Complex div(Number d, Complex c) 76 { 77 Complex c1 = new Complex(d.doubleValue(), 0); 78 79 return c1.div(c); 80 } 81 82 public Complex div(Complex c, Number d) 83 { 84 return new Complex(c.re()/d.doubleValue(), c.im()/d.doubleValue()); 85 } 86 87 public Vector div(Vector v, Number d) 88 { 89 Vector result = new Vector(); 90 91 for (int i=0; i<v.size(); i++) 92 result.addElement(div((Number )v.elementAt(i), d)); 93 94 return result; 95 } 96 97 public Vector div(Number d, Vector v) 98 { 99 Vector result = new Vector(); 100 101 for (int i=0; i<v.size(); i++) 102 result.addElement(div(d, (Number )v.elementAt(i))); 103 104 return result; 105 } 106 107 public Vector div(Vector v, Complex c) 108 { 109 Vector result = new Vector(); 110 111 for (int i=0; i<v.size(); i++) 112 result.addElement(div((Number )v.elementAt(i), c)); 113 114 return result; 115 } 116 117 public Vector div(Complex c, Vector v) 118 { 119 Vector result = new Vector(); 120 121 for (int i=0; i<v.size(); i++) 122 result.addElement(div(c, (Number )v.elementAt(i))); 123 124 return result; 125 } 126 } 127 | Popular Tags |