KickJava   Java API By Example, From Geeks To Geeks.

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


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 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); // check the stack
26
Object JavaDoc param2 = inStack.pop();
27         Object JavaDoc param1 = inStack.pop();
28         inStack.push(div(param1, param2)); //push the result on the inStack
29
return;
30     }
31     
32     public Object JavaDoc div(Object JavaDoc param1, Object JavaDoc 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 JavaDoc)
40                 return div((Complex)param1, (Number JavaDoc)param2);
41             else if (param2 instanceof Vector)
42                 return div((Complex)param1, (Vector)param2);
43         }
44         else if (param1 instanceof Number JavaDoc)
45         {
46             if (param2 instanceof Complex)
47                 return div((Number JavaDoc)param1, (Complex)param2);
48             else if (param2 instanceof Number JavaDoc)
49                 return div((Number JavaDoc)param1, (Number JavaDoc)param2);
50             else if (param2 instanceof Vector)
51                 return div((Number JavaDoc)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 JavaDoc)
58                 return div((Vector)param1, (Number JavaDoc)param2);
59         }
60
61         throw new ParseException("Invalid parameter type");
62     }
63
64
65     public Double JavaDoc div(Number JavaDoc d1, Number JavaDoc d2)
66     {
67         return new Double JavaDoc(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 JavaDoc 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 JavaDoc d)
83     {
84         return new Complex(c.re()/d.doubleValue(), c.im()/d.doubleValue());
85     }
86     
87     public Vector div(Vector v, Number JavaDoc d)
88     {
89         Vector result = new Vector();
90
91         for (int i=0; i<v.size(); i++)
92             result.addElement(div((Number JavaDoc)v.elementAt(i), d));
93         
94         return result;
95     }
96     
97     public Vector div(Number JavaDoc 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 JavaDoc)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 JavaDoc)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 JavaDoc)v.elementAt(i)));
123         
124         return result;
125     }
126 }
127
Popular Tags