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

org.lsmp.djep.sjep.Polynomial Maven / Gradle / Ivy

Go to download

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

The newest version!
/* @author rich
 * Created on 14-Dec-2004
 */
package org.lsmp.djep.sjep;
import org.nfunk.jep.*;
/**
 * Represents a polynomial.
 * i.e. a sum of terms which are typically 
 * {@link Monomial}s, but can be any {@link AbstractPNode}. The order of the terms is specified by the total ordering.
 * 
 * @author Rich Morris
 * Created on 14-Dec-2004
 */
public class Polynomial extends AbstractPNode {

	PNodeI terms[];

	/**
	 * 
	 */
	Polynomial(PolynomialCreator pc,PNodeI terms[]) {
		super(pc);
		this.terms = terms;
	}

	MutiablePolynomial toMutiablePolynomial()
	{
		PNodeI newTerms[] = new PNodeI[terms.length];
		for(int i=0;i -1
	this > arg ---> 1
	*/

	public int compareTo(PNodeI node)
	{
		if(node instanceof Polynomial)
			return this.compareTo((Polynomial) node);
		int res = terms[0].compareTo(node);
		if(res != 0) return res;
		if(terms.length == 1) return 0;
		return 1;
	}

	public int compareTo(Polynomial p)
	{
		for(int i=0;i= p.terms.length) return 1;
			int res = terms[i].compareTo(p.terms[i]);
			if(res != 0) return res;
		}
		if(terms.length < p.terms.length) return -1;
		return 0;
	}
	
	private boolean isNegative(PNodeI node)
	{
		if(node instanceof PConstant)
			return ((PConstant) node).isNegative();
		if(node instanceof Monomial)
			return ((Monomial) node).negativeCoefficient();
		return false;
	}
	public String toString() {
		if(terms.length==0)
			return "0";
		StringBuffer sb = new StringBuffer();
		for(int i=0;i0 && !isNegative(terms[i])) 
				sb.append('+');
			sb.append(terms[i].toString());
		}
		return sb.toString();
	}
	
	public Node toNode() throws ParseException {
		if(terms.length==0)
			return pc.nf.buildConstantNode(pc.zero);
		Node args[] = new Node[terms.length];
		for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy