Class PolynomialCreator
java.lang.Object
com.singularsys.extensions.polynomials.PolynomialCreator
- All Implemented Interfaces:
GenericVisitor<PNodeI,,Void, ParseException> JepComponent,Serializable
- Direct Known Subclasses:
DPolynomialCreator
public class PolynomialCreator
extends Object
implements GenericVisitor<PNodeI,Void,ParseException>, JepComponent
A system for symbolic simplification, expansion and comparison based
on conversion to a canonical polynomial representation.
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,[PVariable("x"),PVariable("y")],[1,1]),
Monomial(4.0,[PVariable("x"),Function("sin",PVariable("y"))],[1,1])
])
Since Jep 4.0/Extensions 2.1 respects the DirtyFunction interface
so functions like rand() are no simplified/expanded away.
It also uses an
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final FieldIprotected FunctionTableprotected Jepprotected NodeFactoryprotected NumberFactoryprotected OperatorTableIprotected TreeUtilsprotected VariableTable -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructor when used as a JepComponent.PolynomialCreator(FieldI field) Constructor used as a component with a specified field.protectedPolynomialCreator(FieldI f, TreeUtils tu, Comparator<Object> comp) Constructor when used as a standalone class, with default DoubleField.PolynomialCreator(Jep j, FieldI f) Constructor when used as a standalone class with specified field -
Method Summary
Modifier and TypeMethodDescriptionintCompares two nodes.createPoly(Node node) Converts an expression into the polynomial representation.booleanCompares two nodes.Expand a PNodeI multiplying out all bracketsExpands an expression.intexpandCompare(Node node1, Node node2) Expands and compares two expressions.booleanexpandEquals(Node node1, Node node2) Compares two nodes.getField()Gets a light-weight instance suitable for using in multiple threads.getPConstant(double var) getPConstant(int var) getPVariable(String string) getTu()Gets the comparator used to compare the values of the constants.voidInitialize the component.makeException(String message) Create an Exception of the correct type.voidsetComparator(Comparator<PNodeI> comparator) Simplifies an expression.PNodeI[]toCoefficientArray(PNodeI poly, String var) Finds the coefficients of each power of the variable, these can be polynomials.double[]toDoubleArray(PNodeI poly, String var) Extract the numerical coefficients of a polynomial.double[][]toDoubleArray(PNodeI poly, String var1, String var2) Extract the numerical coefficients of a polynomial in two variables.double[][][]toDoubleArray(PNodeI poly, String var1, String var2, String var3) Extract the numerical coefficients of a polynomial in three variables.Convert from a polynomial representation back to standard Jep format.PNodeI[]toPNodeArray(PNodeI poly, String var) Create a one-dimensional array of the coefficients of a polynomial.PNodeI[][]toPNodeArray(PNodeI poly, String var1, String var2) Create a two-dimensional rectangular array of the coefficients of a polynomial.PNodeI[][][]toPNodeArray(PNodeI poly, String var1, String var2, String var3) Create a two-dimensional rectangular array of the coefficients of a polynomial.toPNodeArrayRecursive(PNodeI poly, String... vars) Coefficients of a polynomial as a multidimensional array of PNodeI elements.visit(ASTConstant node, Void data) Visit a constant nodevisit(ASTFunNode node, Void data) Visit a function nodeVisit an operator nodevisit(ASTVarNode node, Void data) Visit a variable nodeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.singularsys.jep.GenericVisitor
makeException, visit, visitChildren
-
Field Details
-
zeroConstant
-
oneConstant
-
minusOneConstant
-
twoConstant
-
infConstant
-
nanConstant
-
jep
-
numf
-
tu
-
nf
-
vt
-
ft
-
ot
-
field
-
-
Constructor Details
-
PolynomialCreator
public PolynomialCreator()Constructor when used as a JepComponent. and using the defaultDoubleField. Theinit(Jep)method must be called. -
PolynomialCreator
Constructor used as a component with a specified field.- Parameters:
field- field for operations on constants Theinit(Jep)method must be called.
-
PolynomialCreator
Constructor when used as a standalone class, with default DoubleField.- Parameters:
j- Jep instance
-
PolynomialCreator
Constructor when used as a standalone class with specified field- Parameters:
j- Jep instancef- field for operations on constants
-
PolynomialCreator
-
-
Method Details
-
init
Description copied from interface:JepComponentInitialize the component. This method 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:
initin interfaceJepComponent- Parameters:
j- the current Jep instance
-
getLightWeightInstance
Description copied from interface:JepComponentGets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstancein interfaceJepComponent- Returns:
- either a new instance, null or 'this'.
-
createPoly
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 expression with these values is encountered.
-
toNode
Convert from a polynomial representation back to standard Jep format.- Parameters:
poly-- Returns:
- a Jep expression
- Throws:
ParseException- if expression cannot be converted.
-
simplify
Simplifies an expression.Since Jep 4.0/Extensions 2.1 respects the
DirtyFunctioninterface so functions likerand()are no simplified/expanded away.- Parameters:
node- top node to expression to be simplified.- Returns:
- a simplified expression
- Throws:
ParseException- if expression cannot be converted.
-
expand
Expands an expression. Will always expand brackets for multiplication and simple powers. For instance(1+x)^3 -> 1+3x+3x^2+x^3Since Jep 4.0/Extensions 2.1 respects the
DirtyFunctioninterface 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
Expand a PNodeI multiplying out all brackets- Parameters:
poly- input polynomial- Returns:
- the expanded polynomial
- Throws:
ParseException- if expression cannot be converted.
-
compare
Compares two nodes. Uses a total ordering of expressions. Does not expand the 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
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
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 expand the equations before comparison.- Parameters:
node1-node2-- Returns:
- true if two nodes represents same expression.
- Throws:
ParseException
-
expandEquals
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
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
-
toPNodeArrayRecursive
Coefficients of a polynomial as a multidimensional array of PNodeI elements. With one variable the coefficients can be found usingPNodeI[] coeffs = (PNodeI[]) pc.toPNodeArrayRecursive("a x^2+b x+c","x");For two variables usePNodeI[][] coeffs = (PNodeI[][]) pc.toPNodeArrayRecursive("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
-
toPNodeArray
Create a one-dimensional array of the coefficients of a polynomial. Individual coefficients may be complex involving other terms- Parameters:
poly- the polynomialvar- the variable- Returns:
- array of coefficients
- Throws:
ParseException- on error
-
toPNodeArray
Create a two-dimensional rectangular array of the coefficients of a polynomial. Individual coefficients may be complex involving other terms- Parameters:
poly- the polynomialvar1- first variablevar2- second variable- Returns:
- array of coefficients
- Throws:
ParseException- on error
-
toPNodeArray
public PNodeI[][][] toPNodeArray(PNodeI poly, String var1, String var2, String var3) throws ParseException Create a two-dimensional rectangular array of the coefficients of a polynomial. Individual coefficients may be complex involving other terms- Parameters:
poly- the polynomialvar1- first variablevar2- second variablevar3- third variable- Returns:
- array of coefficients
- Throws:
ParseException- on error
-
toDoubleArray
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
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, String var1, String var2, 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
Description copied from interface:GenericVisitorVisit a constant node- Specified by:
visitin interfaceGenericVisitor<PNodeI,Void, ParseException> - Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit a variable node- Specified by:
visitin interfaceGenericVisitor<PNodeI,Void, ParseException> - Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit a function node- Specified by:
visitin interfaceGenericVisitor<PNodeI,Void, ParseException> - Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit an operator node- Specified by:
visitin interfaceGenericVisitor<PNodeI,Void, ParseException> - Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
getNodeFactory
-
getOperatorTable
-
getPVariable
-
getPConstant
-
getPConstant
-
getComparator
-
setComparator
-
getValueComparator
Gets the comparator used to compare the values of the constants.- Returns:
- the valueComparator
- Since:
- Jep 4.1, extensions 2.2
-
getTu
-
getField
-
makeException
Description copied from interface:GenericVisitorCreate an Exception of the correct type. A typical implementation is@Override public JepException makeException(String message) { return new JepException(message); }- Specified by:
makeExceptionin interfaceGenericVisitor<PNodeI,Void, ParseException> - Parameters:
message- error message- Returns:
- an Exception
-