KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > function > Multiply


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

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); // check the stack
25

26         Object JavaDoc product = stack.pop();
27         Object JavaDoc param;
28         int i = 1;
29         
30         // repeat summation for each one of the current parameters
31
while (i < curNumberOfParameters) {
32             // get the parameter from the stack
33
param = stack.pop();
34             
35             // multiply it with the product, order is important
36
// if matricies are used
37
product = mul(param,product);
38                 
39             i++;
40         }
41                 
42         stack.push(product);
43
44         return;
45     }
46     
47     public Object JavaDoc mul(Object JavaDoc param1, Object JavaDoc 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 JavaDoc)
55                 return mul((Complex)param1, (Number JavaDoc)param2);
56             else if (param2 instanceof Vector)
57                 return mul((Vector)param2, (Complex)param1);
58         }
59         else if (param1 instanceof Number JavaDoc)
60         {
61             if (param2 instanceof Complex)
62                 return mul((Complex)param2, (Number JavaDoc)param1);
63             else if (param2 instanceof Number JavaDoc)
64                 return mul((Number JavaDoc)param1, (Number JavaDoc)param2);
65             else if (param2 instanceof Vector)
66                 return mul((Vector)param2, (Number JavaDoc)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 JavaDoc)
73                 return mul((Vector)param1, (Number JavaDoc)param2);
74         }
75         
76         throw new ParseException("Invalid parameter type");
77     }
78     
79     public Double JavaDoc mul(Number JavaDoc d1, Number JavaDoc d2)
80     {
81         return new Double JavaDoc(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 JavaDoc d)
90     {
91         return c.mul(d.doubleValue());
92     }
93     
94     public Vector mul(Vector v, Number JavaDoc d)
95     {
96         Vector result = new Vector();
97
98         for (int i=0; i<v.size(); i++)
99             result.addElement(mul((Number JavaDoc)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 JavaDoc)v.elementAt(i)));
110         
111         return result;
112     }
113 }
114
Popular Tags