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

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

Go to download

JEP is a Java library for parsing and evaluating mathematical expressions. Use groupId org.fudaa to deploy it in maven central

The newest version!
/* @author rich
 * Created on 23-Dec-2004
 *
 * See LICENSE.txt for license information.
 */
package org.lsmp.djep.sjep;
import org.nfunk.jep.*;
/**
 * A mutable monomial representing a * x^i * y^j * ... * z^k.
 * There are no requirements that this is in a reduced form
 * so some powers can be zero.
 * 
 * @author Rich Morris
 * Created on 23-Dec-2004
 */
public class MutiableMonomial
{
	PolynomialCreator pc;
	PConstant coeff;
	int length;
	PNodeI terms[];
	PNodeI powers[];
	/**
	 * Note arrays parsed may be modified.
	 */
	public MutiableMonomial(PolynomialCreator pc,PConstant coeff,PNodeI nodes[],PNodeI pows[])
	{
		this.pc = pc;
		this.coeff = coeff;
		length = nodes.length;
		terms = nodes;
		powers = pows;
	}

	public void mul(PConstant c) throws ParseException
	{
		coeff = (PConstant) coeff.mul(c);
	}
	
	public void div(PConstant c) throws ParseException
	{
		coeff = (PConstant) coeff.div(c);
	}

	public void mul(PNodeI term,PNodeI power) throws ParseException
	{
		for(int i=0;i 0) {
		 		newTerms[pos] = term;
		 		newPowers[pos] = power;
		 		++pos;
		 		done = true;
			}
			newTerms[pos] = terms[i];
			newPowers[pos] = powers[i];
			++pos;
		}
		if(!done)
		{
			newTerms[pos] = term;
			newPowers[pos] = power;
			++pos;
		}
		length = length+1;
		terms = newTerms;
		powers = newPowers;
	}
	
	void power(PConstant c) throws ParseException
	{
		coeff = (PConstant) coeff.pow(c);
		for(int i=0;i 1
			else if(terms[i].isOne()) {} // 1^x --> 1
			else if(terms[i] instanceof PConstant && powers[i] instanceof PConstant)
			{
				coeff = (PConstant) coeff.mul(terms[i].pow(powers[i]));
			}
			else {
				newTerms[pos] = terms[i];
				newPowers[pos] = powers[i];
				++pos;
			}
		}
		length = newLen;
		terms = newTerms;
		powers = newPowers;
	}
	
	PNodeI toPNode() throws ParseException
	{
		reduce();
		if(length ==0) return coeff;
		if(coeff.isZero()) return pc.zeroConstant;

		return new Monomial(pc,coeff,terms,powers);
	}

	public String toString()
	{	
		StringBuffer sb = new StringBuffer();
		sb.append(coeff.toString());
		for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy