/** * 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. * * <p> * * @since 2.3.0 alpha The functions cmod and arg are added to get the modulus and argument. * @since 2.3.0 beta 1 The functions complex and polar to convert x,y and r,theta to Complex. * @since Feb 05 added complex conjugate conj. */ public void addComplex() { // add constants to Symbol Table symTab.addConstant("i", new Complex(0, 1)); funTab.put("re", new Real()); funTab.put("im", new Imaginary()); funTab.put("arg", new Arg()); funTab.put("cmod", new Abs()); funTab.put("complex", new ComplexPFMC()); funTab.put("polar", new Polar()); funTab.put("conj", new Conjugate()); }
/** * Visit a variable node. The value of the variable is obtained from the symbol table (symTab) and * pushed onto the stack. */ public Object visit(ASTVarNode node, Object data) throws ParseException { // try to get the variable object Variable var = symTab.getVar(node.getVarName()); if (var == null) { String message = "Could not evaluate " + node.getVarName() + ": "; throw new ParseException(message + "the variable was not found in the symbol table"); } // get the variable value // Object temp = var.getValue(); Object temp = var.getValue(); if (trapNullValues && temp == null) { String message = "Could not evaluate " + node.getVarName() + ": "; throw new ParseException(message + "variable not set"); } // all is fine // push the value on the stack stack.push(temp); return data; }
/** * Gets the object representing the variable with a given name. * * @param name the name of the variable to find. * @return the Variable object or null if name not found. * @since 2.3.0 alpha */ public Variable getVar(String name) { return symTab.getVar(name); }
/** * Sets the value of a variable. The variable must exist beforehand. * * @param name name of the variable. * @param val the initial value of the variable. * @throws NullPointerException if the variable has not been previously created with {@link * #addVariable(String,Object)} first. * @since 2.3.0 alpha * @since April 05 - throws an exception if variable unset. */ public void setVarValue(String name, Object val) { symTab.setVarValue(name, val); }
/** * Returns the value of the variable with given name. * * @param name name of the variable. * @return the current value of the variable. * @since 2.3.0 alpha */ public Object getVarValue(String name) { return symTab.getVar(name).getValue(); }
/** * 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. * * @return The value of the variable if it was added earlier. If the variable is not in the table * of variables, <code>null</code> is returned. */ public Object removeVariable(String name) { return symTab.remove(name); }
/** * 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. * * @param name Name of the variable to be added * @param object Initial value or new value for the variable */ public void addVariable(String name, Object object) { symTab.makeVarIfNeeded(name, object); }
/** * 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. * * @param name Name of the variable to be added * @param re Initial real value or new real value for the variable * @param im Initial imaginary value or new imaginary value for the variable * @return Complex object of the variable */ public Complex addVariable(String name, double re, double im) { Complex object = new Complex(re, im); symTab.makeVarIfNeeded(name, object); return object; }
/** * 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. * * @param name Name of the variable to be added * @param value Initial value or new value for the variable * @return Double object of the variable */ public Double addVariable(String name, double value) { Double object = new Double(value); symTab.makeVarIfNeeded(name, object); return object; }
/** * Adds a constant. This is a variable whose value cannot be changed. * * @since 2.3.0 beta 1 */ public void addConstant(String name, Object value) { symTab.addConstant(name, value); }
/** * Adds the constants pi and e to the parser. As addStandardFunctions(), this method should be * called immediately after the JEP object is created. */ public void addStandardConstants() { // add constants to Symbol Table symTab.addConstant("pi", new Double(Math.PI)); symTab.addConstant("e", new Double(Math.E)); }