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 Multiply extends PostfixMathCommand 16 { 17 18 public Multiply() { 19 numberOfParameters = -1; 20 } 21 22 public void run(Stack stack) throws ParseException 23 { 24 checkStack(stack); 26 Object product = stack.pop(); 27 Object param; 28 int i = 1; 29 30 while (i < curNumberOfParameters) { 32 param = stack.pop(); 34 35 product = mul(param,product); 38 39 i++; 40 } 41 42 stack.push(product); 43 44 return; 45 } 46 47 public Object mul(Object param1, Object param2) 48 throws ParseException 49 { 50 if (param1 instanceof Complex) 51 { 52 if (param2 instanceof Complex) 53 return mul((Complex)param1, (Complex)param2); 54 else if (param2 instanceof Number ) 55 return mul((Complex)param1, (Number )param2); 56 else if (param2 instanceof Vector) 57 return mul((Vector)param2, (Complex)param1); 58 } 59 else if (param1 instanceof Number ) 60 { 61 if (param2 instanceof Complex) 62 return mul((Complex)param2, (Number )param1); 63 else if (param2 instanceof Number ) 64 return mul((Number )param1, (Number )param2); 65 else if (param2 instanceof Vector) 66 return mul((Vector)param2, (Number )param1); 67 } 68 else if (param1 instanceof Vector) 69 { 70 if (param2 instanceof Complex) 71 return mul((Vector)param1, (Complex)param2); 72 else if (param2 instanceof Number ) 73 return mul((Vector)param1, (Number )param2); 74 } 75 76 throw new ParseException("Invalid parameter type"); 77 } 78 79 public Double mul(Number d1, Number d2) 80 { 81 return new Double (d1.doubleValue()*d2.doubleValue()); 82 } 83 84 public Complex mul(Complex c1, Complex c2) 85 { 86 return c1.mul(c2); 87 } 88 89 public Complex mul(Complex c, Number d) 90 { 91 return c.mul(d.doubleValue()); 92 } 93 94 public Vector mul(Vector v, Number d) 95 { 96 Vector result = new Vector(); 97 98 for (int i=0; i<v.size(); i++) 99 result.addElement(mul((Number )v.elementAt(i), d)); 100 101 return result; 102 } 103 104 public Vector mul(Vector v, Complex c) 105 { 106 Vector result = new Vector(); 107 108 for (int i=0; i<v.size(); i++) 109 result.addElement(mul(c, (Number )v.elementAt(i))); 110 111 return result; 112 } 113 } 114 | Popular Tags |