org.lsmp.djep.sjep.PolynomialCreator 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.lsmp.djep.xjep.*;
import org.nfunk.jep.*;
import org.nfunk.jep.type.*;
import org.nfunk.jep.function.*;
/**
* Main entry point for simplification routines.
*
*
Uses a complete reworking of the ways equations are represented.
A tree structure is built from Polynomials, Monomials, PVariable etc.
An equation like
1+2 x^2+3 x y+4 x sin(y)
is represented as
Polynomial([
Monomial(2.0,[PVariable(x)],[2])]),
Monomial(3.0,[x,y],[1,1]),
Monomial(4.0,[x,Function(sin,arg)],[1,1])
])
A total ordering of all expressions is used.
As the representation is constructed the total ordering of terms is maintained.
This helps ensure that polynomials are always in their simplest form
and also allows comparison of equations.
The following sequence illustrates current ordering.
This ordering may change without warning.
- -1 numbers sorted by values
- 0
- 1 numbers before monomials
- a^-2 powers in increasing order
- a^-1
- a
- a^2
- a^3
- a^x numeric powers before symbolic powers
- a b single variable monomials before multiple variables
- a^2 b
- b variables sorted alphabetically
- cos(a) monomials before functions
- sin(a) function names sorted alphabetically
- a+b functions before polynomials
- a+b+c
-
* @author Rich Morris
* Created on 14-Dec-2004
*/
public class PolynomialCreator extends DoNothingVisitor {
private XJep jep;
Object zero,one,minusOne,infinity,nan,two;
PConstant zeroConstant,oneConstant,minusOneConstant,infConstant,nanConstant,twoConstant;
Monomial zeroMonomial,unitMonomial,infMonomial,nanMonomial;
Polynomial zeroPolynomial,unitPolynomial,infPolynomial,nanPolynomial;
NumberFactory numf;
OperatorSet os;
NodeFactory nf;
//boolean expand=false;
private PolynomialCreator() {}
public PolynomialCreator(XJep j)
{
jep = j;
numf = j.getNumberFactory();
os = j.getOperatorSet();
nf = j.getNodeFactory();
zero = j.getNumberFactory().getZero();
one = j.getNumberFactory().getOne();
minusOne = j.getNumberFactory().getMinusOne();
two = j.getNumberFactory().getTwo();
try {
infinity = div(one,zero);
nan = div(zero,zero);
} catch(ParseException e) {
infinity = new Double(Double.POSITIVE_INFINITY);
nan = new Double(Double.NaN);
}
zeroConstant = new PConstant(this,zero);
oneConstant = new PConstant(this,one);
twoConstant = new PConstant(this,two);
minusOneConstant = new PConstant(this,minusOne);
infConstant = new PConstant(this,infinity);
nanConstant = new PConstant(this,nan);
}
/**
* Converts an expression into the polynomial representation.
* @param node top node of expression
* @return top node of polynomial form of expression
* @throws ParseException if expression cannot be converted.
*/
public PNodeI createPoly(Node node) throws ParseException
{
return (PNodeI) node.jjtAccept(this,null);
}
/**
* Simplifies an expression.
*
* @param node top node to expression to be simplified.
* @return a simplified expression
* @throws ParseException
*/
public Node simplify(Node node) throws ParseException
{
PNodeI poly = createPoly(node);
return poly.toNode();
}
/**
* Expands an expression.
* Will always expand brackets for multiplication and simple powers.
* For instance
* (1+x)^3 -> 1+3x+3x^2+x^3
*
* @param node top node to expression to be simplified.
* @return a simplified expression
* @throws ParseException
*/
public Node expand(Node node) throws ParseException
{
PNodeI poly = createPoly(node);
PNodeI expand = poly.expand();
return expand.toNode();
}
/**
* Compares two nodes.
* Uses a total ordering of expressions.
* Expands equations before comparison.
*
* @param node1
* @param node2
* @return -1 if node1<node2, 0 if node1==node2, +1 if node1>node2
* @throws ParseException
*/
public int compare(Node node1,Node node2) throws ParseException
{
PNodeI poly1 = createPoly(node1);
PNodeI exp1 = poly1.expand();
PNodeI poly2 = createPoly(node2);
PNodeI exp2 = poly2.expand();
return exp1.compareTo(exp2);
}
/**
* Compares two nodes.
* Uses a total ordering of expressions.
* May give some false negatives is simplification cannot reduce
* two equal expressions to the same canonical form.
* Expands equations before comparison.
*
* @param node1
* @param node2
* @return true if two nodes represents same expression.
* @throws ParseException
*/
public boolean equals(Node node1,Node node2) throws ParseException
{
PNodeI poly1 = createPoly(node1);
PNodeI exp1 = poly1.expand();
PNodeI poly2 = createPoly(node2);
PNodeI exp2 = poly2.expand();
return exp1.equals(exp2);
}
public Object visit(ASTConstant node, Object data) throws ParseException {
return new PConstant(this,node.getValue());
}
public Object visit(ASTVarNode node, Object data) throws ParseException {
return new PVariable(this,(XVariable) node.getVar());
}
public Object visit(ASTFunNode node, Object data) throws ParseException {
int nChild = node.jjtGetNumChildren();
PNodeI args[] = new PNodeI[nChild];
for(int i=0;i