1 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 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 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 42 43 public Object sub(Object param1, Object 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 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 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 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 |