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

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

Go to download

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

The newest version!
/* @author rich
 * Created on 13-Feb-2005
 *
 * See LICENSE.txt for license information.
 */
package org.lsmp.djep.vectorJep.function;

import java.util.Stack;

import org.lsmp.djep.vectorJep.Dimensions;
import org.lsmp.djep.vectorJep.values.*;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;

/**
 * Extracts diagonal from a square matrix.
 * getDiag([[1,0,0],[0,2,0],[0,0,3]]) -> [1,2,3]
 * 
 * @author Rich Morris
 * Created on 13-Feb-2005
 */
public class GetDiagonal extends PostfixMathCommand implements UnaryOperatorI
{
	public GetDiagonal()
	{
		super();
		this.numberOfParameters = 1;
	}

	public Dimensions calcDim(Dimensions ldim)
	{
		return Dimensions.valueOf(ldim.getFirstDim());
	}

	public MatrixValueI calcValue(MatrixValueI res, MatrixValueI lhs)
		throws ParseException
	{
		MVector vec = (MVector) res;
		Matrix mat = (Matrix) lhs;
		if( vec.getNumEles() != mat.getNumRows()
		 ||	vec.getNumEles() != mat.getNumCols())
		 	throw new ParseException("getDiag requires a square matrix");
		for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy