Class PolynomialCreator
- java.lang.Object
-
- com.singularsys.extensions.polynomials.PolynomialCreator
-
- All Implemented Interfaces:
JepComponent
,ParserVisitor
,java.io.Serializable
public class PolynomialCreator extends java.lang.Object implements ParserVisitor, JepComponent
A system for symbolic simplification, expansion and comparision based on conversion to a canonical polynomial representation. A tree structure is built from Polynomials, Monomials, PVariable etc. An equation like1+2 x^2+3 x y+4 x sin(y)
is represented asPolynomial([ 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.
- -1 // numbers sorted by values
- 0
- 1 // numbers before monomials
- a^-2 // powers in increasing order, parsed as a^(-2)
- a^-1
- -2 a // monomials with lower coefficient first
- -a
- a
- 2 a
- a^2
- a^3
- a^x // numeric powers before symbolic powers
- b // variables sorted alphabetically
- b c // first term in a monomial tested first
- c
- x-1 // polynomials terms tested in order with higher degree ones first
- x
- x+1
- x+2
- 2 x+1
- x^2+x+1 // polynomials with lower degree come first
- x^2+x+2
- x^2+2 x+1
- x^2 y
- x==1 // polynomials before operators
- x>1 // operators sorted alphabetically
- x>y
- y>x
- cos(a) // operators before functions
- sin(a) //function names sorted alphabetically
- sin(b) // function arguments compared
Since Jep 4.0/Extensions 2.1 respects the
DirtyFunction
interface so functions likerand()
are no simplifed/expanded away.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected FieldI
field
protected FunctionTable
ft
PConstant
infConstant
protected Jep
jep
PConstant
minusOneConstant
PConstant
nanConstant
protected NodeFactory
nf
protected NumberFactory
numf
PConstant
oneConstant
protected OperatorTableI
ot
protected TreeUtils
tu
PConstant
twoConstant
protected VariableTable
vt
PConstant
zeroConstant
-
Constructor Summary
Constructors Modifier Constructor Description PolynomialCreator()
Constructor when used as a JepComponent.PolynomialCreator(FieldI field)
Constructor used as a component with a specified field.protected
PolynomialCreator(FieldI f, TreeUtils tu)
PolynomialCreator(Jep j)
Constructor when used as a stand alone class, with default DoubleField.PolynomialCreator(Jep j, FieldI f)
Constructor when used as a stand alone class with specified field
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compare(Node node1, Node node2)
Compares two nodes.PNodeI
createPoly(Node node)
Converts an expression into the polynomial representation.boolean
equals(Node node1, Node node2)
Compares two nodes.PNodeI
expand(PNodeI poly)
Expand a PNodeI multiplying out all bracketsNode
expand(Node node)
Expands an expression.int
expandCompare(Node node1, Node node2)
Expands and compares two expressions.boolean
expandEquals(Node node1, Node node2)
Compares two nodes.JepComponent
getLightWeightInstance()
Gets a light-weight instance suitable for using in multiple threads.NodeFactory
getNodeFactory()
OperatorTableI
getOperatorTable()
void
init(Jep j)
Initialize the component.Node
simplify(Node node)
Simplifies an expression.PNodeI[]
toCoefficientArray(PNodeI poly, java.lang.String var)
Finds the coefficients of each power of the variable, these can be polynomials.double[]
toDoubleArray(PNodeI poly, java.lang.String var)
Extract the numerical coefficients of a polynomial.double[][]
toDoubleArray(PNodeI poly, java.lang.String var1, java.lang.String var2)
Extract the numerical coefficients of a polynomial in two variables.double[][][]
toDoubleArray(PNodeI poly, java.lang.String var1, java.lang.String var2, java.lang.String var3)
Extract the numerical coefficients of a polynomial in three variables.Node
toNode(PNodeI poly)
Convert from a polynomial representation back to standard Jep format.java.lang.Object
toPNodeArray(PNodeI poly, java.lang.String... vars)
Coefficients of a polynomial as a multi-dimensional array of PNodeI elements.java.lang.Object
visit(ASTConstant node, java.lang.Object data)
java.lang.Object
visit(ASTFunNode node, java.lang.Object data)
java.lang.Object
visit(ASTOpNode node, java.lang.Object data)
java.lang.Object
visit(ASTVarNode node, java.lang.Object data)
-
-
-
Field Detail
-
zeroConstant
public PConstant zeroConstant
-
oneConstant
public PConstant oneConstant
-
minusOneConstant
public PConstant minusOneConstant
-
twoConstant
public PConstant twoConstant
-
infConstant
public PConstant infConstant
-
nanConstant
public PConstant nanConstant
-
jep
protected Jep jep
-
numf
protected NumberFactory numf
-
tu
protected TreeUtils tu
-
nf
protected NodeFactory nf
-
vt
protected VariableTable vt
-
ft
protected FunctionTable ft
-
ot
protected OperatorTableI ot
-
field
protected FieldI field
-
-
Constructor Detail
-
PolynomialCreator
public PolynomialCreator()
Constructor when used as a JepComponent. Theinit(Jep)
method must be called.
-
PolynomialCreator
public PolynomialCreator(FieldI field)
Constructor used as a component with a specified field.- Parameters:
field
- field for operations on constants Theinit(Jep)
method must be called.
-
PolynomialCreator
public PolynomialCreator(Jep j)
Constructor when used as a stand alone class, with default DoubleField.- Parameters:
j
- Jep instance
-
PolynomialCreator
public PolynomialCreator(Jep j, FieldI f)
Constructor when used as a stand alone class with specified field- Parameters:
j
- Jep instancef
- field for operations on constants
-
-
Method Detail
-
init
public void init(Jep j)
Description copied from interface:JepComponent
Initialize the component. This methods is called whenever a component is added to Jep. Hence it allows components to keep track of the other components they may rely on.- Specified by:
init
in interfaceJepComponent
- Parameters:
j
- the current Jep instance
-
getLightWeightInstance
public JepComponent getLightWeightInstance()
Description copied from interface:JepComponent
Gets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstance
in interfaceJepComponent
- Returns:
- either an new instance, null or 'this'.
-
createPoly
public PNodeI createPoly(Node node) throws ParseException
Converts an expression into the polynomial representation.- Parameters:
node
- top node of expression- Returns:
- top node of polynomial form of expression
- Throws:
ParseException
- if expression cannot be converted. This can happen if the underlying field cannot represent Infinity or NaN and a constant exprssion with these values is encountered.
-
toNode
public Node toNode(PNodeI poly) throws ParseException
Convert from a polynomial representation back to standard Jep format.- Parameters:
poly
-- Returns:
- a Jep expression
- Throws:
ParseException
- if expression cannot be converted.
-
simplify
public Node simplify(Node node) throws ParseException
Simplifies an expression.Since Jep 4.0/Extensions 2.1 respects the
DirtyFunction
interface so functions likerand()
are no simplifed/expanded away.- Parameters:
node
- top node to expression to be simplified.- Returns:
- a simplified expression
- Throws:
ParseException
- if expression cannot be converted.
-
expand
public Node expand(Node node) throws ParseException
Expands an expression. Will always expand brackets for multiplication and simple powers. For instance(1+x)^3 -> 1+3x+3x^2+x^3
Since Jep 4.0/Extensions 2.1 respects the
DirtyFunction
interface so functions likerand()
are no expanded away.- Parameters:
node
- top node to expression to be simplified.- Returns:
- a simplified expression
- Throws:
ParseException
- if expression cannot be converted.
-
expand
public PNodeI expand(PNodeI poly) throws ParseException
Expand a PNodeI multiplying out all brackets- Parameters:
poly
- input polynomial- Returns:
- the expanded polynomial
- Throws:
ParseException
- if expression cannot be converted.
-
compare
public int compare(Node node1, Node node2) throws ParseException
Compares two nodes. Uses a total ordering of expressions. Does not expands equations before comparison. The ordering follows this sequence- -1 // numbers sorted by values
- 0
- 1 // numbers before monomials
- a^-2 // powers in increasing order, parsed as a^(-2)
- a^-1
- -2 a // monomials with lower coefficient first
- -a
- a
- 2 a
- a^2
- a^3
- a^x // numeric powers before symbolic powers
- b // variables sorted alphabetically
- b c // first term in a monomial tested first
- c
- x-1 // polynomials terms tested in order with higher degree ones first
- x
- x+1
- x+2
- 2 x+1
- x^2+x+1 // polynomials with lower degree come first
- x^2+x+2
- x^2+2 x+1
- x^2 y
- cos(a) // polynomials before functions
- sin(a) //function names sorted alphabetically
- sin(b) // function arguments compared
- Parameters:
node1
- lhs nodenode2
- rhs node- Returns:
- -1 if node1<node2, 0 if node1==node2, +1 if node1>node2
- Throws:
ParseException
-
expandCompare
public int expandCompare(Node node1, Node node2) throws ParseException
Expands and compares two expressions.- Parameters:
node1
- lhsnode2
- rhs- Returns:
- -1 if node1<node2, 0 if node1==node2, +1 if node1>node2 using the total order
- Throws:
ParseException
-
equals
public boolean equals(Node node1, Node node2) throws ParseException
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. Does not expands equations before comparison.- Parameters:
node1
-node2
-- Returns:
- true if two nodes represents same expression.
- Throws:
ParseException
-
expandEquals
public boolean expandEquals(Node node1, Node node2) throws ParseException
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.- Parameters:
node1
-node2
-- Returns:
- true if two nodes represents same expression.
- Throws:
ParseException
-
toCoefficientArray
public PNodeI[] toCoefficientArray(PNodeI poly, java.lang.String var) throws ParseException
Finds the coefficients of each power of the variable, these can be polynomials.- Parameters:
poly
- The input polynomialvar
- the name of the variable- Returns:
- an array with coefficients, [constant,var,var^2, ...]
- Throws:
ParseException
-
toPNodeArray
public java.lang.Object toPNodeArray(PNodeI poly, java.lang.String... vars) throws ParseException
Coefficients of a polynomial as a multi-dimensional array of PNodeI elements. With one variable the coefficients can be found usingPNodeI[] coeffs = (PNodeI[]) pc.toPNodeArray("a x^2+b x+c","x");
For two variables usePNodeI[][] coeffs = (PNodeI[][]) pc.toPNodeArray("a x^2+b x y","x","y");
Note the returned array is not guaranteed to have the same number of elements in each row or column.- Parameters:
poly
- expression to find coefficients ofvars
- names of variables to find coefficients of- Returns:
- multi-dimensional array either PNodeI[] or PNodeI[][] etc depending on number of variables
- Throws:
ParseException
- if poly does not represent a polynomial
-
toDoubleArray
public double[] toDoubleArray(PNodeI poly, java.lang.String var) throws ParseException
Extract the numerical coefficients of a polynomial.- Parameters:
poly
- input polynomial. A polynomial in a single variables.var
- polynomial variable- Returns:
- array of coefficients with constant term first
- Throws:
ParseException
-
toDoubleArray
public double[][] toDoubleArray(PNodeI poly, java.lang.String var1, java.lang.String var2) throws ParseException
Extract the numerical coefficients of a polynomial in two variables.- Parameters:
poly
- input polynomial. A polynomial in two variables.var1
- name of first variablevar2
- name of second variable- Returns:
- two dimensional array of coefficients, element [i][j] is coefficient of x^i y^j.
- Throws:
ParseException
-
toDoubleArray
public double[][][] toDoubleArray(PNodeI poly, java.lang.String var1, java.lang.String var2, java.lang.String var3) throws ParseException
Extract the numerical coefficients of a polynomial in three variables.- Parameters:
poly
- input polynomial. A polynomial in three variables.var1
- name of first variablevar2
- name of second variablevar3
- name of third variable- Returns:
- three dimensional array of coefficients, element [i][j][k] is coefficient of x^i y^j z^k.
- Throws:
ParseException
-
visit
public java.lang.Object visit(ASTConstant node, java.lang.Object data) throws ParseException
- Specified by:
visit
in interfaceParserVisitor
- Throws:
ParseException
-
visit
public java.lang.Object visit(ASTVarNode node, java.lang.Object data) throws ParseException
- Specified by:
visit
in interfaceParserVisitor
- Throws:
ParseException
-
visit
public java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws JepException
- Specified by:
visit
in interfaceParserVisitor
- Throws:
JepException
-
visit
public java.lang.Object visit(ASTOpNode node, java.lang.Object data) throws JepException
- Specified by:
visit
in interfaceParserVisitor
- Throws:
JepException
-
getNodeFactory
public NodeFactory getNodeFactory()
-
getOperatorTable
public OperatorTableI getOperatorTable()
-
-