KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* @author rich
2  * Created on 27-Jul-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.vectorJep.function;
9 import org.lsmp.djep.vectorJep.*;
10 import org.lsmp.djep.vectorJep.values.*;
11 import org.nfunk.jep.*;
12 import org.nfunk.jep.function.Subtract;
13 /**
14  * An extension of the Add command to allow it to add MVector's and Matrix's.
15  * @author Rich Morris
16  * Created on 27-Jul-2003
17  */

18 public class MSubtract extends Subtract implements BinaryOperatorI {
19
20     public Dimensions calcDim(Dimensions ldim,Dimensions rdim)
21     {
22         if(ldim.equals(rdim)) return ldim;
23         else return null;
24     }
25
26     /** calculates the value.
27      * @param res - results will be stored in this object
28      * @param lhs - lhs value
29      * @param rhs - rhs value
30      * @return res
31      */

32     public MatrixValueI calcValue(MatrixValueI res,MatrixValueI lhs,MatrixValueI rhs) throws ParseException
33     {
34         int len = res.getNumEles();
35         for(int i=0;i<len;++i)
36             res.setEle(i,super.sub(lhs.getEle(i),rhs.getEle(i)));
37         return res;
38     }
39     /**
40      * Adds two objects.
41      */

42     
43     public Object JavaDoc sub(Object JavaDoc param1, Object JavaDoc param2) throws ParseException
44     {
45         if(param1 instanceof MVector && param2 instanceof MVector)
46             return sub((MVector) param1,(MVector) param2);
47         if(param1 instanceof Matrix && param2 instanceof Matrix)
48             return sub((Matrix) param1,(Matrix) param2);
49         else if(param1 instanceof Tensor && param2 instanceof Tensor)
50             return sub((Tensor) param1,(Tensor) param2);
51         else
52             return super.sub(param1,param2);
53     }
54     
55     /** Adds two vectors. */
56     public MVector sub(MVector lhs,MVector rhs) throws ParseException
57     {
58         if(lhs.getNumEles() != rhs.getNumEles()) throw new ParseException("Miss match in sizes ("+lhs.getNumEles()+","+rhs.getNumEles()+") when trying to add vectors!");
59         MVector res = new MVector(lhs.getNumEles());
60         return (MVector) calcValue(res,lhs,rhs);
61     }
62
63     /** Adds two matricies. */
64     public Matrix sub(Matrix lhs,Matrix rhs) throws ParseException
65     {
66         if(lhs.getNumRows() != rhs.getNumRows()) throw new ParseException("Miss match in number of rows ("+lhs.getNumRows()+","+rhs.getNumRows()+") when trying to add vectors!");
67         if(lhs.getNumCols() != rhs.getNumCols()) throw new ParseException("Miss match in number of cols ("+lhs.getNumCols()+","+rhs.getNumCols()+") when trying to add vectors!");
68         Matrix res = new Matrix(lhs.getNumRows(),lhs.getNumCols());
69         return (Matrix) calcValue(res,lhs,rhs);
70     }
71
72     /** Adds two matricies. */
73     public Tensor sub(Tensor lhs,Tensor rhs) throws ParseException
74     {
75         if(lhs.getNumEles() != rhs.getNumEles()) throw new ParseException("Miss match in sizes ("+lhs.getNumEles()+","+rhs.getNumEles()+") when trying to add vectors!");
76         Tensor res = new Tensor(lhs.getDim());
77         return (Tensor) calcValue(res,lhs,rhs);
78     }
79 }
80
Popular Tags