Class 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 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.

    • -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 like rand() are no simplifed/expanded away.

    See Also:
    Serialized Form
    • Constructor Detail

      • PolynomialCreator

        public PolynomialCreator()
        Constructor when used as a JepComponent. 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 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 instance
        f - field for operations on constants
      • PolynomialCreator

        protected PolynomialCreator​(FieldI f,
                                    TreeUtils tu)
    • 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 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 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 like rand() 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 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 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 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 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 polynomial
        var - 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 using PNodeI[] coeffs = (PNodeI[]) pc.toPNodeArray("a x^2+b x+c","x"); For two variables use PNodeI[][] 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 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
      • 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 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,
                                          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 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