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 Details

  • Constructor Details

    • PolynomialCreator

      public PolynomialCreator()
      Constructor when used as a JepComponent. and using the default DoubleField. The init(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 The init(Jep) method must be called.
    • PolynomialCreator

      public PolynomialCreator(Jep j)
      Constructor when used as a standalone class, with default DoubleField.
      Parameters:
      j - Jep instance
    • PolynomialCreator

      public PolynomialCreator(Jep j, FieldI f)
      Constructor when used as a standalone class with specified field
      Parameters:
      j - Jep instance
      f - field for operations on constants
    • PolynomialCreator

      protected PolynomialCreator(FieldI f, TreeUtils tu, Comparator<Object> comp)
  • Method Details

    • init

      public void init(Jep j)
      Description copied from interface: JepComponent
      Initialize 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:
      init in interface JepComponent
      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 interface JepComponent
      Returns:
      either a 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 expression 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 like rand() 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

      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 like rand() 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 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 node
      node2 - 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 - lhs
      node2 - 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 expand the 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, String var) throws ParseException
      Finds the coefficients of each power of the variable, these can be polynomials.
      Parameters:
      poly - The input polynomial
      var - the name of the variable
      Returns:
      an array with coefficients, [constant,var,var^2, ...]
      Throws:
      ParseException
    • toPNodeArrayRecursive

      public Object toPNodeArrayRecursive(PNodeI poly, String... vars) throws ParseException
      Coefficients of a polynomial as a multidimensional array of PNodeI elements. With one variable the coefficients can be found using PNodeI[] coeffs = (PNodeI[]) pc.toPNodeArrayRecursive("a x^2+b x+c","x"); For two variables use PNodeI[][] 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 of
      vars - 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

      public PNodeI[] toPNodeArray(PNodeI poly, String var) throws ParseException
      Create a one-dimensional array of the coefficients of a polynomial. Individual coefficients may be complex involving other terms
      Parameters:
      poly - the polynomial
      var - the variable
      Returns:
      array of coefficients
      Throws:
      ParseException - on error
    • toPNodeArray

      public PNodeI[][] toPNodeArray(PNodeI poly, String var1, String var2) 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 polynomial
      var1 - first variable
      var2 - 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 polynomial
      var1 - first variable
      var2 - second variable
      var3 - third variable
      Returns:
      array of coefficients
      Throws:
      ParseException - on error
    • toDoubleArray

      public double[] toDoubleArray(PNodeI poly, 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, String var1, 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 variable
      var2 - 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 variable
      var2 - name of second variable
      var3 - 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 PNodeI visit(ASTConstant node, Void data) throws ParseException
      Description copied from interface: GenericVisitor
      Visit a constant node
      Specified by:
      visit in interface GenericVisitor<PNodeI,Void,ParseException>
      Parameters:
      node - current node
      data - context data
      Returns:
      the result of visiting the node
      Throws:
      ParseException
    • visit

      public PNodeI visit(ASTVarNode node, Void data) throws ParseException
      Description copied from interface: GenericVisitor
      Visit a variable node
      Specified by:
      visit in interface GenericVisitor<PNodeI,Void,ParseException>
      Parameters:
      node - current node
      data - context data
      Returns:
      the result of visiting the node
      Throws:
      ParseException
    • visit

      public PNodeI visit(ASTFunNode node, Void data) throws ParseException
      Description copied from interface: GenericVisitor
      Visit a function node
      Specified by:
      visit in interface GenericVisitor<PNodeI,Void,ParseException>
      Parameters:
      node - current node
      data - context data
      Returns:
      the result of visiting the node
      Throws:
      ParseException
    • visit

      public PNodeI visit(ASTOpNode node, Void data) throws ParseException
      Description copied from interface: GenericVisitor
      Visit an operator node
      Specified by:
      visit in interface GenericVisitor<PNodeI,Void,ParseException>
      Parameters:
      node - current node
      data - context data
      Returns:
      the result of visiting the node
      Throws:
      ParseException
    • getNodeFactory

      public NodeFactory getNodeFactory()
    • getOperatorTable

      public OperatorTableI getOperatorTable()
    • getPVariable

      public PVariable getPVariable(String string)
    • getPConstant

      public PConstant getPConstant(int var)
    • getPConstant

      public PNodeI getPConstant(double var)
    • getComparator

      public Comparator<PNodeI> getComparator()
    • setComparator

      public void setComparator(Comparator<PNodeI> comparator)
    • getValueComparator

      public Comparator<Object> getValueComparator()
      Gets the comparator used to compare the values of the constants.
      Returns:
      the valueComparator
      Since:
      Jep 4.1, extensions 2.2
    • getTu

      public TreeUtils getTu()
    • getField

      public FieldI getField()
    • makeException

      public ParseException makeException(String message)
      Description copied from interface: GenericVisitor
      Create an Exception of the correct type. A typical implementation is
      @Override
      public JepException makeException(String message) {
       return new JepException(message);
      }
      
      Specified by:
      makeException in interface GenericVisitor<PNodeI,Void,ParseException>
      Parameters:
      message - error message
      Returns:
      an Exception