org.lsmp.djep.sjep.MutiablePolynomial Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jep Show documentation
Show all versions of jep Show documentation
JEP is a Java library for parsing and evaluating mathematical expressions.
/* @author rich
* Created on 23-Dec-2004
*
* See LICENSE.txt for license information.
*/
package org.lsmp.djep.sjep;
import org.nfunk.jep.*;
/**
* A mutable polynomial representing a + b + c.
* 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 MutiablePolynomial
{
PolynomialCreator pc;
PNodeI terms[];
/**
* Note arrays parsed may be modified.
*/
public MutiablePolynomial(PolynomialCreator pc,PNodeI nodes[])
{
this.pc = pc;
terms = nodes;
}
public void add(PNodeI term) throws ParseException
{
if(term instanceof PConstant)
for(int i=0;i 0) {
newTerms[pos] = term;
++pos;
done = true;
}
newTerms[pos] = terms[i];
++pos;
}
if(!done)
{
newTerms[pos] = term;
++pos;
}
terms = newTerms;
}
/**
* Multiplies this by a polynomial and expands the results.
* (1+x)*(1+x) --> 1+2*x+x^2
* @param p the polynomial to multiply by
* @throws ParseException
*/
void expandMul(Polynomial p) throws ParseException
{
PNodeI newTerms[][] = new PNodeI[terms.length][p.terms.length];
for(int i=0;i 1+2*x+x^2
* @param node
* @throws ParseException
*/
void expandMul(PNodeI node) throws ParseException
{
if(node instanceof Polynomial) {
expandMul((Polynomial) node);
return;
}
PNodeI newTerms[] = new PNodeI[terms.length];
for(int i=0;i 1
else if(terms[i] instanceof PConstant) {}
else {
newTerms[pos] = terms[i];
++pos;
}
}
terms = newTerms;
}
PNodeI toPNode() throws ParseException
{
reduce();
if(terms.length ==0) return pc.zeroConstant;
if(terms.length == 1) return terms[0];
return new Polynomial(pc,terms);
}
public String toString() {
StringBuffer sb = new StringBuffer();
for(int i=0;i0) sb.append('+');
sb.append(terms[i].toString());
}
return sb.toString();
}
}