Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.nfunk.jep
Class JEP

    java.lang.Object
      org.nfunk.jep.JEP

Direct Known Subclasses:
  GroupJep, VectorJep, XJep

    public class JEP

extends java.lang.Object

The JEP class is the main interface with which the user should interact. It contains all necessary methods to parse and evaluate expressions.

The most important methods are parseExpression(String), for parsing the mathematical expression, and getValue() for obtaining the value of the expression.

Visit http://www.singularsys.com/jep for the newest version of JEP, and complete documentation.

Since:
  04/02/13 added Binom function
Author:
  Nathan Funk

Field Summary
`protected boolean` | `allowAssignment` Allow undeclared variables option
`protected boolean` | `allowUndeclared` Allow undeclared variables option
`protected java.util.Vector` | `errorList` Error List
`protected EvaluatorVisitor` | `ev` Evaluator
`protected FunctionTable` | `funTab` Function Table
`protected boolean` | `implicitMul` Implicit multiplication option
`protected NumberFactory` | `numberFactory` Number factory
`protected OperatorSet` | `opSet` OperatorSet
`protected Parser` | `parser` The parser object
`protected SymbolTable` | `symTab` Symbol Table

Constructor Summary
`` | `JEP()` Creates a new JEP instance with the default settings.
`` | `JEP(boolean traverse_in, boolean allowUndeclared_in, boolean implicitMul_in, NumberFactory numberFactory_in)` Creates a new JEP instance with custom settings.
`protected` | `JEP(JEP j)` This constructor copies the SymbolTable and other components of the arguments to the new instance.

Method Summary
`void` | `addComplex()` Call this function if you want to parse expressions which involve complex numbers.
`void` | `addConstant(java.lang.String name, java.lang.Object value)` Adds a constant.
`void` | `addFunction(java.lang.String functionName, PostfixMathCommandI function)` Adds a new function to the parser.
`void` | `addStandardConstants()` Adds the constants pi and e to the parser.
`void` | `addStandardFunctions()` Adds the standard functions to the parser.
`java.lang.Double` | `addVariable(java.lang.String name, double value)` Adds a new variable to the parser, or updates the value of an existing variable.
`Complex` | `addVariable(java.lang.String name, double re, double im)` Adds a new complex variable to the parser, or updates the value of an existing variable.
`void` | `addVariable(java.lang.String name, java.lang.Object object)` Adds a new variable to the parser as an object, or updates the value of an existing variable.
`java.lang.Object` | `evaluate(Node node)` Evaluate an expression.
`boolean` | `getAllowAssignment()` Whether assignment equation `y=x+1` equations are allowed.
`boolean` | `getAllowUndeclared()` Returns the value of the allowUndeclared option.
`Complex` | `getComplexValue()` Evaluates and returns the value of the expression as a complex number.
`java.lang.String` | `getErrorInfo()` Reports information on the errors that occurred during the most recent action.
`EvaluatorVisitor` | `getEvaluatorVisitor()` Returns the EvaluatorVisitor
`FunctionTable` | `getFunctionTable()` Returns the function table (the list of all functions that the parser recognizes).
`boolean` | `getImplicitMul()` Returns the value of the implicit multiplication option.
`NumberFactory` | `getNumberFactory()` Returns the number factory.
`OperatorSet` | `getOperatorSet()` Returns the operator set.
`Parser` | `getParser()` Returns the parse object.
`SymbolTable` | `getSymbolTable()` Returns the symbol table (the list of all variables that the parser recognizes).
`Node` | `getTopNode()` Returns the top node of the expression tree.
`boolean` | `getTraverse()` Returns the value of the traverse option.
`double` | `getValue()` Evaluates and returns the value of the expression as a double number.
`java.lang.Object` | `getValueAsObject()` Evaluates and returns the value of the expression as an object.
`Variable` | `getVar(java.lang.String name)` Gets the object representing the variable with a given name.
`java.lang.Object` | `getVarValue(java.lang.String name)` Returns the value of the variable with given name.
`boolean` | `hasError()` Returns true if an error occurred during the most recent action (parsing or evaluation).
`void` | `initFunTab()` Creates a new FunctionTable object as funTab.
`void` | `initSymTab()` Creates a new SymbolTable object as symTab.
`Node` | `parse(java.lang.String expression)` Parses an expression.
`Node` | `parseExpression(java.lang.String expression_in)` Parses the expression.
`java.lang.Object` | `removeFunction(java.lang.String name)` Removes a function from the parser.
`java.lang.Object` | `removeVariable(java.lang.String name)` Removes a variable from the parser.
`void` | `setAllowAssignment(boolean value)` Sets whether assignment equations like `y=x+1` are allowed.
`void` | `setAllowUndeclared(boolean value)` Sets the value for the undeclared variables option.
`void` | `setImplicitMul(boolean value)` Sets the value of the implicit multiplication option.
`void` | `setTraverse(boolean value)` Sets the value of the traverse option. setTraverse is useful for debugging purposes.
`void` | `setVarValue(java.lang.String name, java.lang.Object val)` Sets the value of a variable.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### allowUndeclared

    protected boolean allowUndeclared

  Allow undeclared variables option

### allowAssignment

    protected boolean allowAssignment

  Allow undeclared variables option

### implicitMul

    protected boolean implicitMul

  Implicit multiplication option

### symTab

    protected SymbolTable symTab

  Symbol Table

### funTab

    protected FunctionTable funTab

  Function Table

### errorList

    protected java.util.Vector errorList

  Error List

### parser

    protected Parser parser

  The parser object

### ev

    protected EvaluatorVisitor ev

  Evaluator

### numberFactory

    protected NumberFactory numberFactory

  Number factory

### opSet

    protected OperatorSet opSet

  OperatorSet

Constructor Detail

### JEP

    public JEP()

  Creates a new JEP instance with the default settings.

  Traverse = false
  Allow undeclared variables = false
  Implicit multiplication = false
  Number Factory = DoubleNumberFactory

### JEP

    public JEP(boolean traverse_in,
               boolean allowUndeclared_in,
               boolean implicitMul_in,
               NumberFactory numberFactory_in)

  Creates a new JEP instance with custom settings. If the numberFactory_in is null, the default number factory is used.

  Parameters:
    `traverse_in` - The traverse option.
    `allowUndeclared_in` - The "allow undeclared variables" option.
    `implicitMul_in` - The implicit multiplication option.
    `numberFactory_in` - The number factory to be used.

### JEP

    protected JEP(JEP j)

  This constructor copies the SymbolTable and other components of the arguments to the new instance. Subclasses can call this protected constructor and set the individual components themselves.

  Since:
    2.3.0 alpha

Method Detail

### initSymTab

    public void initSymTab()

  Creates a new SymbolTable object as symTab.

### initFunTab

    public void initFunTab()

  Creates a new FunctionTable object as funTab.

### addStandardFunctions

    public void addStandardFunctions()

  Adds the standard functions to the parser. If this function is not called before parsing an expression, functions such as sin() or cos() would produce an "Unrecognized function..." error. In most cases, this method should be called immediately after the JEP object is created.

  Since:
    2.3.0 alpha added if and exp functions, 2.3.0 beta 1 added str function

### addStandardConstants

    public void addStandardConstants()

  Adds the constants pi and e to the parser. As addStandardFunctions(), this method should be called immediately after the JEP object is created.

### addComplex

    public void addComplex()

  Call this function if you want to parse expressions which involve complex numbers. This method specifies "i" as the imaginary unit (0,1). Two functions re() and im() are also added for extracting the real or imaginary components of a complex number respectively.

  Since:
    2.3.0 alpha The functions cmod and arg are added to get the modulus and argument., 2.3.0 beta 1 The functions complex and polar to convert x,y and r,theta to Complex., Feb 05 added complex conjugate conj.

### addFunction

    public void addFunction(java.lang.String functionName,
                            PostfixMathCommandI function)

  Adds a new function to the parser. This must be done before parsing an expression so the parser is aware that the new function may be contained in the expression.

  Parameters:
    `functionName` - The name of the function
    `function` - The function object that is used for evaluating the function

### addConstant

    public void addConstant(java.lang.String name,
                            java.lang.Object value)

  Adds a constant. This is a variable whose value cannot be changed.

  Since:
    2.3.0 beta 1

### addVariable

    public java.lang.Double addVariable(java.lang.String name,
                                        double value)

  Adds a new variable to the parser, or updates the value of an existing variable. This must be done before parsing an expression so the parser is aware that the new variable may be contained in the expression.

  Parameters:
    `name` - Name of the variable to be added
    `value` - Initial value or new value for the variable
  Returns:
    Double object of the variable

### addVariable

    public Complex addVariable(java.lang.String name,
                               double re,
                               double im)

  Adds a new complex variable to the parser, or updates the value of an existing variable. This must be done before parsing an expression so the parser is aware that the new variable may be contained in the expression.

  Parameters:
    `name` - Name of the variable to be added
    `re` - Initial real value or new real value for the variable
    `im` - Initial imaginary value or new imaginary value for the variable
  Returns:
    Complex object of the variable

### addVariable

    public void addVariable(java.lang.String name,
                            java.lang.Object object)

  Adds a new variable to the parser as an object, or updates the value of an existing variable. This must be done before parsing an expression so the parser is aware that the new variable may be contained in the expression.

  Parameters:
    `name` - Name of the variable to be added
    `object` - Initial value or new value for the variable

### removeVariable

    public java.lang.Object removeVariable(java.lang.String name)

  Removes a variable from the parser. For example after calling addStandardConstants(), removeVariable("e") might be called to remove the euler constant from the set of variables.

  Returns:
    The value of the variable if it was added earlier. If the variable is not in the table of variables, `null` is returned.

### getVarValue

    public java.lang.Object getVarValue(java.lang.String name)

  Returns the value of the variable with given name.

  Parameters:
    `name` - name of the variable.
  Returns:
    the current value of the variable.
  Since:
    2.3.0 alpha

### setVarValue

    public void setVarValue(java.lang.String name,
                            java.lang.Object val)

  Sets the value of a variable. The variable must exist before hand.

  Parameters:
    `name` - name of the variable.
    `val` - the initial value of the variable.
  Throws:
    `java.lang.NullPointerException` - if the variable has not been previously created with `addVariable(String,Object)` first.
  Since:
    2.3.0 alpha, April 05 - throws an exception if variable unset.

### getVar

    public Variable getVar(java.lang.String name)

  Gets the object representing the variable with a given name.

  Parameters:
    `name` - the name of the variable to find.
  Returns:
    the Variable object or null if name not found.
  Since:
    2.3.0 alpha

### removeFunction

    public java.lang.Object removeFunction(java.lang.String name)

  Removes a function from the parser.

  Returns:
    If the function was added earlier, the function class instance is returned. If the function was not present, `null` is returned.

### setTraverse

    public void setTraverse(boolean value)

  Sets the value of the traverse option. setTraverse is useful for debugging purposes. When traverse is set to true, the parse-tree will be dumped to the standard output device.

  The default value is false.

  Parameters:
    `value` - The boolean traversal option.

### getTraverse

    public boolean getTraverse()

  Returns the value of the traverse option.

  Returns:
    True if the traverse option is enabled. False otherwise.

### setImplicitMul

    public void setImplicitMul(boolean value)

  Sets the value of the implicit multiplication option. If this option is set to true before parsing, implicit multiplication will be allowed. That means that an expression such as

      "1 2"

  is valid and is interpreted as

      "1*2"

  .

  The default value is false.

  Parameters:
    `value` - The boolean implicit multiplication option.

### getImplicitMul

    public boolean getImplicitMul()

  Returns the value of the implicit multiplication option.

  Returns:
    True if the implicit multiplication option is enabled. False otherwise.

### setAllowUndeclared

    public void setAllowUndeclared(boolean value)

  Sets the value for the undeclared variables option. If this option is set to true, expressions containing variables that were not previously added to JEP will not produce an "Unrecognized Symbol" error. The new variables will automatically be added while parsing, and initialized to 0.

  If this option is set to false, variables that were not previously added to JEP will produce an error while parsing.

  The default value is false.

  Parameters:
    `value` - The boolean option for allowing undeclared variables.

### getAllowUndeclared

    public boolean getAllowUndeclared()

  Returns the value of the allowUndeclared option.

  Returns:
    True if the allowUndeclared option is enabled. False otherwise.

### setAllowAssignment

    public void setAllowAssignment(boolean value)

  Sets whether assignment equations like `y=x+1` are allowed.

  Since:
    2.3.0 alpha

### getAllowAssignment

    public boolean getAllowAssignment()

  Whether assignment equation `y=x+1` equations are allowed.

  Since:
    2.3.0 alpha

### parseExpression

    public Node parseExpression(java.lang.String expression_in)

  Parses the expression. If there are errors in the expression, they are added to the `errorList` member. Errors can be obtained through `getErrorInfo()`.

  Parameters:
    `expression_in` - The input expression string
  Returns:
    the top node of the expression tree if the parse was successful, `null` otherwise

### parse

    public Node parse(java.lang.String expression)
               throws ParseException

  Parses an expression. Returns a object of type Node, does not catch errors. Does not set the topNode variable of the JEP instance. This method should generally be used with the `evaluate` method rather than getValueAsObject.

  Parameters:
    `expression` - represented as a string.
  Returns:
    The top node of a tree representing the parsed expression.
  Throws:
    `ParseException`
  Since:
    2.3.0 alpha, 2.3.0 beta - will raise exception if errorList non empty

### evaluate

    public java.lang.Object evaluate(Node node)
                              throws ParseException

  Evaluate an expression. This method evaluates the argument rather than the topNode of the JEP instance. It should be used in conjunction with `parse` rather than `parseExpression`.

  Parameters:
    `node` - the top node of the tree representing the expression.
  Returns:
    The value of the expression
  Throws:
    `ParseException` - if for some reason the expression could not be evaluated
    `java.lang.RuntimeException` - could potentially be thrown.
  Since:
    2.3.0 alpha

### getValue

    public double getValue()

  Evaluates and returns the value of the expression as a double number.

  Returns:
    The calculated value of the expression as a double number. If the type of the value does not implement the Number interface (e.g. Complex), NaN is returned. If an error occurs during evaluation, NaN is returned and hasError() will return true.
  See Also:
    `getComplexValue()`

### getComplexValue

    public Complex getComplexValue()

  Evaluates and returns the value of the expression as a complex number.

  Returns:
    The calculated value of the expression as a complex number if no errors occur. Returns null otherwise.

### getValueAsObject

    public java.lang.Object getValueAsObject()

  Evaluates and returns the value of the expression as an object. The EvaluatorVisitor member ev is used to do the evaluation procedure. This method is useful when the type of the value is unknown, or not important.

  Returns:
    The calculated value of the expression if no errors occur. Returns null otherwise.

### hasError

    public boolean hasError()

  Returns true if an error occurred during the most recent action (parsing or evaluation).

  Returns:
    Returns `true` if an error occurred during the most recent action (parsing or evaluation).

### getErrorInfo

    public java.lang.String getErrorInfo()

  Reports information on the errors that occurred during the most recent action.

  Returns:
    A string containing information on the errors, each separated by a newline character; null if no error has occurred

### getTopNode

    public Node getTopNode()

  Returns the top node of the expression tree. Because all nodes are pointed to either directly or indirectly, the entire expression tree can be accessed through this node. It may be used to manipulate the expression, and subsequently evaluate it manually.

  Returns:
    The top node of the expression tree

### getSymbolTable

    public SymbolTable getSymbolTable()

  Returns the symbol table (the list of all variables that the parser recognizes).

  Returns:
    The symbol table

### getFunctionTable

    public FunctionTable getFunctionTable()

  Returns the function table (the list of all functions that the parser recognizes).

  Returns:
    The function table

### getEvaluatorVisitor

    public EvaluatorVisitor getEvaluatorVisitor()

  Returns the EvaluatorVisitor

  Returns:
    the EvaluatorVisitor.

### getNumberFactory

    public NumberFactory getNumberFactory()

  Returns the number factory.

  Returns:
    the NumberFactory used by this JEP instance.

### getOperatorSet

    public OperatorSet getOperatorSet()

  Returns the operator set.

  Returns:
    the OperatorSet used by this JEP instance.
  Since:
    2.3.0 alpha

### getParser

    public Parser getParser()

  Returns the parse object.

  Returns:
    the Parse used by this JEP.
  Since:
    2.3.0 beta 1

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

http://www.singularsys.com/jep Copyright © 2007 Singular Systems