KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > vectorJep > function > MDot


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.24
4       December 30 2002
5       (c) Copyright 2002, Nathan Funk
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9 package org.lsmp.djep.vectorJep.function;
10
11 import java.util.*;
12
13 import org.lsmp.djep.vectorJep.*;
14 import org.lsmp.djep.vectorJep.values.*;
15
16 import org.nfunk.jep.*;
17
18 /**
19  * The MDot operator.
20  * @author Rich Morris
21  * Created on 23-Feb-2004
22  */

23 public class MDot extends MMultiply implements BinaryOperatorI
24 {
25     public MDot() {
26         numberOfParameters = 2;
27     }
28     public Dimensions calcDim(Dimensions l,Dimensions r) {
29         if(l.equals(r) && l.is1D()) return Dimensions.ONE;
30         else return null;
31     }
32     
33     /** calculates the value.
34      * @param res - results will be stored in this object
35      * @param lhs - lhs value
36      * @param rhs - rhs value
37      * @return res
38      */

39     public MatrixValueI calcValue(MatrixValueI res,MatrixValueI lhs,MatrixValueI rhs) throws ParseException
40     {
41         return calcValue((Scaler) res,(MVector) lhs,(MVector) rhs);
42     }
43
44     public Scaler calcValue(Scaler res,MVector lhs,MVector rhs) throws ParseException
45     {
46         int len = lhs.getNumEles();
47         Object JavaDoc val = mul(lhs.getEle(0),rhs.getEle(0));
48         for(int i=1;i<len;++i)
49             val = add.add(val,mul(lhs.getEle(i),rhs.getEle(i)));
50         res.setEle(0,val);
51         return res;
52     }
53
54     public void run(Stack stack) throws ParseException
55     {
56         checkStack(stack); // check the stack
57

58         Object JavaDoc param1,param2;
59  
60         // get the parameter from the stack
61

62         param2 = stack.pop();
63         param1 = stack.pop();
64             
65             // multiply it with the product
66

67         stack.push(dot(param1, param2));
68
69         return;
70     }
71
72     /** returns param1 . param2. Defaults to scaler mult if parameters are not vectors. */
73     public Object JavaDoc dot(Object JavaDoc param1, Object JavaDoc param2) throws ParseException
74     {
75         if(param1 instanceof MVector && param2 instanceof MVector)
76             return dot((MVector) param1,(MVector) param2);
77         else
78             return super.mul(param1,param2);
79     }
80     
81     /** returns lhs . rhs */
82     public Object JavaDoc dot(MVector lhs, MVector rhs) throws ParseException
83     {
84         if(!lhs.getDim().equals(rhs.getDim())) throw new ParseException("Dot: Miss match in sizes ("+lhs.getDim()+","+rhs.getDim()+")");
85         Scaler res = new Scaler();
86         calcValue(res,lhs,rhs);
87         return res.getEle(0);
88     }
89 }
90
Popular Tags