org.lsmp.djep.sjep.Monomial 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. Use groupId org.fudaa to deploy it in maven central
The newest version!
/* @author rich
* Created on 14-Dec-2004
*/
package org.lsmp.djep.sjep;
import org.nfunk.jep.*;
/**
* Represents an imutable monomial a x^i * y^j * ... * z^k, a constant.
*
* @author Rich Morris
* Created on 14-Dec-2004
*/
public class Monomial extends AbstractPNode {
PConstant coeff;
PNodeI vars[];
PNodeI powers[];
/**
*
*/
Monomial(PolynomialCreator pc,PConstant coeff,PNodeI vars[],PNodeI powers[]) {
super(pc);
if(vars.length != powers.length)
throw new IllegalArgumentException("Monomial.valueOf length of variables and powers must be equal. they are "+vars.length+" "+powers.length);
this.coeff = coeff;
this.vars = vars;
this.powers = powers;
}
Monomial(PolynomialCreator pc,PConstant coeff,PNodeI var) {
super(pc);
this.coeff = coeff;
this.vars = new PNodeI[]{var};
this.powers = new PNodeI[]{pc.oneConstant};
}
Monomial(PolynomialCreator pc,PConstant coeff,PNodeI var,PNodeI power) {
super(pc);
this.coeff = coeff;
this.vars = new PNodeI[]{var};
this.powers = new PNodeI[]{power};
}
PNodeI valueOf(PConstant coefficient,PNodeI terms[],PNodeI pows[])
{
if(coefficient.isZero()) return pc.zeroConstant;
if(terms.length ==0) return coefficient;
return new Monomial(pc,coefficient,terms,pows);
}
MutiableMonomial toMutiableMonomial()
{
PNodeI newTerms[] = new PNodeI[vars.length];
PNodeI newPows[] = new PNodeI[vars.length];
for(int i=0;i=mon.vars.length) return 1;
int res = vars[i].compareTo(mon.vars[i]);
if(res!=0) return res;
res = powers[i].compareTo(mon.powers[i]);
if(res!=0) return res;
}
if(vars.length > mon.vars.length) return 1;
if(vars.length < mon.vars.length) return -1;
return coeff.compareTo(mon.coeff);
}
// compare with first term
int res = vars[0].compareTo(node);
if(res==0)
res = powers[0].compareTo(pc.oneConstant);
return res;
}
private boolean negativePower(PNodeI pow) {
return( pow instanceof PConstant
&& ((PConstant) pow).isNegative());
}
private void printPower(StringBuffer sb,PNodeI pow)
{
if(pow.isOne()) return;
if(pow instanceof PConstant
|| pow instanceof PVariable
|| pow instanceof PFunction)
{
sb.append('^');
sb.append(pow.toString());
}
else
{
sb.append("^(");
sb.append(pow.toString());
sb.append(")");
}
}
public String toString()
{
StringBuffer sb = new StringBuffer();
boolean flag = false;
if( coeff.isMinusOne())
sb.append('-');
else if( coeff.isOne()) { }
else {
sb.append(coeff.toString());
flag = true;
}
// first print positive and complicated powers
int numNeg = 0;
for(int i=0;i0)
{
if(!flag) sb.append('1');
if(numNeg > 1) sb.append("/(");
else sb.append("/");
flag = false;
for(int i=0;i 1) sb.append(")");
}
return sb.toString();
}
public Node toNode() throws ParseException
{
int nCoeff = coeff.isOne() ? 0 : 1;
int numDivisors = 0;
for(int i=0;i0)
args[pos++]=coeff.toNode();
for(int i=0;i0)
{
PNodeI res = vars[i].expand();
for(int j=1;j<=intpow;++j)
mp.expandMul(res);
}
else
mp.expandMul(new Monomial(pc,pc.oneConstant,vars[i].expand(),powers[i]));
}
}
else
mp.expandMul(new Monomial(pc,pc.oneConstant,vars[i].expand(),powers[i]));
}
return mp.toPNode();
}
}