All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.lsmp.djep.vectorJep.function.MMultiply Maven / Gradle / Ivy

Go to download

JEP is a Java library for parsing and evaluating mathematical expressions.

The newest version!
/* @author rich
 * Created on 27-Jul-2003
 */
package org.lsmp.djep.vectorJep.function;
import org.lsmp.djep.vectorJep.*;
import org.lsmp.djep.vectorJep.values.*;
import org.nfunk.jep.*;
import org.nfunk.jep.function.*;
import java.util.*;

/**
 * An extension of the Multiply to with vectors and matricies.
 * Must faster (1/3) if used with MatrixJep and calcValue routines.
 * Note vector * vector treated as col_vec * row_vec -> matrix.
 *  
 * @author Rich Morris
 * Created on 27-Jul-2003
 * TODO add handeling of tensors
 * @since 2.3.2 Improved error reporting
 */
public class MMultiply extends Multiply implements BinaryOperatorI {
	
	protected Add add = new Add();
	protected Subtract sub = new Subtract();
	
	public MMultiply()
	{
		//add = (Add) Operator.OP_ADD.getPFMC();
		//sub = (Subtract) Operator.OP_SUBTRACT.getPFMC();
		numberOfParameters = 2;
	}




	/**
	 *  need to redo this as the standard jep version assumes commutivity.
	 */	
	public void run(Stack stack) throws ParseException 
	{
		checkStack(stack); // check the stack
		//if(this.curNumberOfParameters!=2) throw new ParseException("Multiply: should have two children its got "+stack.size());
		Object param2 = stack.pop();
		Object param1 = stack.pop();
		Object product = mul(param1, param2);
		stack.push(product);
		return;
	}

	/**
	 * Multiply two objects.
	 */

	public Object mul(Object param1, Object param2) throws ParseException 
	{
		if(param1 instanceof MatrixValueI && param2 instanceof MatrixValueI)
		{
			return mul((MatrixValueI) param1,(MatrixValueI) param2);
		}
		else if(param1 instanceof MatrixValueI)
		{
			MatrixValueI l = (MatrixValueI) param1;
			MatrixValueI res = Tensor.getInstance(l.getDim());
			for(int i=0;i Vector
				return l;
			case 1: // Vector * Vector -> Matrix
				return Dimensions.valueOf(l.getFirstDim(),r.getFirstDim());
			case 2: // Vector * Matrix -> Vector
				if(l.getLastDim() == r.getFirstDim())
					 return Dimensions.valueOf(r.getLastDim());
				break;
			default: // Tensor res
				throw new ParseException("Sorry I don't know how to multiply a vector by a tensor");
			}
			break;
		case 2: // Matrix * ?
			switch(rrank)
			{
			case 0: // Matrix * Scaler -> Matrix
				return l;
			case 1: // Matrix * Vector -> Vector
				if(l.getLastDim() == r.getFirstDim())
					 return Dimensions.valueOf(l.getFirstDim());
				break;
			case 2: // Matrix * Matrix -> Matrix
				if(l.getLastDim() == r.getFirstDim()) return Dimensions.valueOf(l.getFirstDim(),r.getLastDim());
				break;
			default: // Tensor res
				//throw new ParseException("Sorry I don't know how to multiply a matrix by a tensor");
				
			}
			break;
		default: // Tensor res
			switch(rrank)
			{
			case 0: // Scaler res
				return l;
//			case 1: // Vector res
//				throw new ParseException("Sorry I don't know how to multiply a tensor by a vector");
//			case 2: // Matrix res
//				throw new ParseException("Sorry I don't know how to multiply a tensor by a matrix");
//			default: // Tensor res
//				throw new ParseException("Sorry I don't know how to multiply a tensor by a tensor");
			}
		}
		throw new ParseException("Dimensions for multiply do not match: "+l+" "+r);
	}

	public MatrixValueI calcValue(MatrixValueI res,MatrixValueI param1,MatrixValueI param2) throws ParseException
	{
		if(param1 instanceof Scaler)
		{	
			if(param2 instanceof Scaler)	
			{ // Scaler * Scaler -> Scaler
				res.setEle(0,super.mul(param1.getEle(0),param2.getEle(0)));
			}
			else if(param2 instanceof MVector)
			{
			 // Scaler * Vector -> Vector
				for(int i=0;i Matrix
			{
				Matrix r = (Matrix) param2;
				Matrix mres = (Matrix) res;
				for(int i=0;i Vector
			{
				for(int i=0;i Matrix
			{
				Matrix mat = (Matrix) res;
				for(int i=0;i Vector
			{
				MVector lhs = (MVector) param1;
				Matrix rhs = (Matrix) param2;
				if(lhs.getNumEles() != rhs.getNumRows()) throw new ParseException("Multiply Matrix , Vector: Miss match in sizes ("+lhs.getNumEles()+","+rhs.getNumRows()+")!");
				for(int i=0;i Matrix
			{
				Matrix l = (Matrix) param1;
				Matrix mres = (Matrix) res;
				for(int i=0;i Vector
			{	
				Matrix lhs = (Matrix) param1;
				MVector rhs = (MVector) param2;
				if(lhs.getNumCols() != rhs.getNumEles()) throw new ParseException("Mat * Vec: Miss match in sizes ("+lhs.getNumCols()+","+rhs.getNumEles()+") when trying to add vectors!");
				for(int i=0;i Matrix
			{
				Matrix lhs = (Matrix) param1;
				Matrix rhs = (Matrix) param2;
				Matrix mres = (Matrix) res;
				if(lhs.getNumCols() != rhs.getNumRows()) throw new ParseException("Multiply matrix,matrix: Miss match in number of dims ("+lhs.getNumCols()+","+rhs.getNumRows()+")!");
				int lnr = lhs.getNumRows();
				int lnc = lhs.getNumCols();
				int rnc = rhs.getNumCols();
				Object ldata[][] = lhs.getEles();
				Object rdata[][] = rhs.getEles();
				Object resdata[][] = mres.getEles();
				for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy