Class XJep

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    DJep

    public class XJep
    extends Jep
    An extended version of Jep adds various routines for working with trees. Has a NodeFactory, and OperatorTable, TreeUtils and Visitors DoNothingVisitor,
    Author:
    Rich Morris Created on 16-Nov-2003
    See Also:
    Serialized Form
    • Constructor Detail

      • XJep

        public XJep()
        Create a new XJep with all the function of Jep plus simplification and other features.
      • XJep

        public XJep​(JepComponent... components)
        Create a new XJep instance using specified components and default components if necessary.
        Parameters:
        components -
      • XJep

        public XJep​(ComponentSet components)
        Create a new XJep instance using specified components set and default components if necessary.
        Parameters:
        components -
    • Method Detail

      • setXComponentInternal

        protected void setXComponentInternal​(JepComponent comp)
      • substitute

        public Node substitute​(Node orig,
                               java.lang.String name,
                               java.lang.Object value)
                        throws ParseException
        Substitute all occurrences of a named variable with a constant value.
        Parameters:
        orig - expression to substitute into
        name - name of variable to substitute
        value - its replacement value
        Returns:
        expression after substitution
        Throws:
        ParseException
      • substitute

        public Node substitute​(Node orig,
                               java.lang.String name,
                               Node replacement)
                        throws ParseException
        Substitute all occurrences of a named variable with an expression.
        Parameters:
        orig - expression to substitute into
        name - name of variable to substitute
        replacement - expression to
        Returns:
        expression after substitution
        Throws:
        ParseException
      • substitute

        public Node substitute​(Node orig,
                               Node sub)
                        throws ParseException
        Substitute all occurrences of the variable on the left hand side of sub with the expression tree on the right hand side.
        Parameters:
        orig - expression to substitute into
        sub - expression of the form x=....
        Returns:
        expression after substitution
        Throws:
        ParseException
      • substitute

        public Node substitute​(Node orig,
                               java.lang.String[] names,
                               java.lang.Object[] values)
                        throws ParseException
        Substitute all occurrences of the variables with their values.
        Throws:
        ParseException
      • substitute

        public Node substitute​(Node orig,
                               Node[] subs)
                        throws ParseException
        Substitute a set of expressions into the original.
        Parameters:
        orig - expression to substitute into
        subs - array of expressions of the form x=....
        Returns:
        expression after substitution
        Throws:
        ParseException
      • substitute

        public Node substitute​(Node orig,
                               java.lang.String[] names,
                               Node[] replacements)
                        throws ParseException
        Substitute all occurrences of a set of named variable with a set of expression tree.
        Throws:
        ParseException
      • substituteConstantVariables

        public Node substituteConstantVariables​(Node orig)
                                         throws ParseException
        Replace all constant variables like pi by their values.
        Parameters:
        orig - the input
        Returns:
        a copy of the input with all constant variables substituted by their values.
        Throws:
        ParseException
      • replaceRHSVariablesByEquations

        public Node replaceRHSVariablesByEquations​(Node node)
                                            throws JepException
        Replace variables on the right had side of an assignment by their equations
        Parameters:
        node - an equation of the form g:=f^2-1
        Returns:
        a copy of the node
        Throws:
        JepException
      • replaceVariableByExpressions

        public Node replaceVariableByExpressions​(Node node)
                                          throws JepException
        Replace all symbolic variable by their equations
        Parameters:
        node - root of expression
        Returns:
        copy of node with variables replaced by their equations
        Throws:
        JepException
      • calcVarValue

        public java.lang.Object calcVarValue​(java.lang.String name,
                                             boolean force)
                                      throws EvaluationException
        Calculates the value for the variables equation and returns that value. If the variable does not have an equation just return its value. Recursively calculates the values of any variable with equations which appear.
        Parameters:
        name - name of variable
        force - whether to force re-evaluation if the variable has a valid value
        Returns:
        value of variable
        Throws:
        EvaluationException
      • getVarsInEquation

        public java.util.Set<Variable> getVarsInEquation​(Node n)
        Finds all the variables in an equation.
        Parameters:
        n - the top node of the expression
        Returns:
        v
      • getVarsInEquation

        public java.util.Set<Variable> getVarsInEquation​(Node n,
                                                         java.util.Set<Variable> vars)
        Finds all the variables in an equation.
        Parameters:
        n - the top node of the expression
        vars - the list of variables (new variables will be added on end)
        Returns:
        v
      • recursiveGetVarsInEquation

        public java.util.List<XVariable> recursiveGetVarsInEquation​(Node n,
                                                                    java.util.List<XVariable> vars)
                                                             throws ParseException
        Finds all the variables in an equation and if any of those variables are defined by equations find the variables in those equations as well. The result is an ordered sequence, evaluating each variable in turn will correctly allow the final equation to be evaluated.

        For example if the equation is a+b and a=c+d, b=c+e then the result will be the sequence (c,d,a,e,b) For this method to work the equations must be preprocessed using preprocess(com.singularsys.jep.parser.Node). From version 2.1 returns a sorted list rather than an ordered set.

        Parameters:
        n - top node
        vars - list of variables, new variables will be added on the end.
        Returns:
        v, the ordered sequence of variables
        Throws:
        ParseException - if equation is recursive i.e. x=y; y=x+1;
      • addRewriteRule

        public boolean addRewriteRule​(RewriteRuleI rule)
      • getTreeUtils

        public TreeUtils getTreeUtils()
        Returns the TreeUtilitities, used for examining properties of nodes.