/** * Parse the given <code>expression String</code> and return the resulting ASTNode * * @param expression * @return * @throws SyntaxError */ public static ASTNode parseNode(String expression, boolean relaxedSyntax) { DoubleEvaluator doubleEvaluator = new DoubleEvaluator(relaxedSyntax); return doubleEvaluator.parse(expression); }
/** * Reevaluate the <code>expression</code> (possibly after a new Variable assignment) * * @param Expression * @return * @throws SyntaxError */ public double evaluate() { if (fNode == null) { throw new SyntaxError(0, 0, 0, " ", "No parser input defined", 1); } return evaluateNode(fNode); }
/** * Get the variable names from the given expression. * * @param expression * @param result * a set which contains the variable names */ public static void getVariables(String expression, Set<String> result) { getVariables(expression, result, true); }
/** * Parse the given <code>expression String</code> and evaluate it to a * double value. * * @param expression the expression * @return the double * @throws SyntaxError the syntax error */ public double evaluate( String expression ) { Parser p = new Parser(); fNode = p.parse( expression ); if ( fNode instanceof FunctionNode ) { fNode = optimizeFunction( (FunctionNode) fNode ); } return evaluateNode( fNode ); }
public double evaluate(DoubleEvaluator engine, FunctionNode function) { if (function.size() != 3) { throw new ArithmeticException( "SetFunction#evaluate(DoubleEvaluator,FunctionNode) needs 2 arguments: " + function.toString()); } if (!(function.getNode(1) instanceof SymbolNode)) { throw new ArithmeticException( "SetFunction#evaluate(DoubleEvaluator,FunctionNode) symbol required on the left hand side: " + function.toString()); } String variableName = ((SymbolNode) function.getNode(1)).getString(); double result = engine.evaluateNode(function.getNode(2)); IDoubleValue dv = engine.getVariable(variableName); if (dv == null) { dv = new DoubleVariable(result); } else { dv.setValue(result); } engine.defineVariable(variableName, dv); return result; } }
if (obj instanceof IBooleanBoolean1Function) { return ((IBooleanBoolean1Function) obj) .evaluate(evaluateNodeLogical(functionNode.getNode(1))); if (obj instanceof IBooleanDouble2Function) { return ((IBooleanDouble2Function) obj).evaluate( evaluateNode(functionNode.getNode(1)), evaluateNode(functionNode.getNode(2))); } else if (obj instanceof IBooleanBoolean2Function) { return ((IBooleanBoolean2Function) obj).evaluate( evaluateNodeLogical(functionNode.getNode(1)), evaluateNodeLogical(functionNode.getNode(2)));
.getNode(i)).doubleValue())); } else if (functionNode.getNode(i) instanceof FunctionNode) { ASTNode optNode = optimizeFunction((FunctionNode) functionNode .getNode(i)); if (!(optNode instanceof DoubleNode)) { return new DoubleNode(evaluateFunction(functionNode)); } catch (Exception e) {
return ComplexEvaluator.toString(c); } else { DoubleEvaluator engine = new DoubleEvaluator(); double d = engine.evaluate(strEval); return Double.toString(d);
/** * Parse the given <code>expression String</code> and store the resulting * ASTNode in this DoubleEvaluator * * @param expression * @return * @throws SyntaxError */ public ASTNode parse(String expression) { Parser p = new Parser(); fNode = p.parse(expression); if (fNode instanceof FunctionNode) { fNode = optimizeFunction((FunctionNode) fNode); } return fNode; }
public boolean evaluateNodeLogical(final ASTNode node) { if (node instanceof FunctionNode) { return evaluateFunctionLogical((FunctionNode) node); } if (node instanceof SymbolNode) { BooleanVariable v = fBooleanVariables.get(node.toString()); if (v != null) { return v.getValue(); } Boolean boole = SYMBOL_BOOLEAN_MAP.get(node.toString()); if (boole != null) { return boole.booleanValue(); } } throw new ArithmeticException( "EvalDouble#evaluateNodeLogical(ASTNode) not possible for: " + node.toString()); }
return evaluateFunction((FunctionNode) node);
/** * Evaluate. * * @param engine the engine * @param function the function * @return the double */ @Override public double evaluate( DoubleEvaluator engine, FunctionNode function ) { if ( function.size() != 3 ) { throw new ArithmeticException( "SetFunction#evaluate(DoubleEvaluator,FunctionNode) needs 2 arguments: " + function.toString() ); } if ( !(function.getNode( 1 ) instanceof SymbolNode) ) { throw new ArithmeticException( "SetFunction#evaluate(DoubleEvaluator,FunctionNode) symbol required on the left hand side: " + function.toString() ); } String variableName = ((SymbolNode) function.getNode( 1 )).getString(); double result = engine.evaluateNode( function.getNode( 2 ) ); IDoubleValue dv = engine.getVariable( variableName ); if ( dv == null ) { dv = new DoubleVariable( result ); } else { dv.setValue( result ); } engine.defineVariable( variableName, dv ); return result; } }
Object obj = FUNCTION_BOOLEAN_MAP.get( symbol ); if ( obj instanceof IBooleanBoolean1Function ) { return ((IBooleanBoolean1Function) obj).evaluate( evaluateNodeLogical( functionNode.getNode( 1 ) ) ); Object obj = FUNCTION_BOOLEAN_MAP.get( symbol ); if ( obj instanceof IBooleanDouble2Function ) { return ((IBooleanDouble2Function) obj).evaluate( evaluateNode( functionNode.getNode( 1 ) ), evaluateNode( functionNode.getNode( 2 ) ) ); return ((IBooleanBoolean2Function) obj).evaluate( evaluateNodeLogical( functionNode.getNode( 1 ) ), evaluateNodeLogical( functionNode.getNode( 2 ) ) );
/** * Parse the given <code>expression String</code> and evaluate it to a double * value * * @param expression * @return * @throws SyntaxError */ public double evaluate(String expression) { Parser p = new Parser(); fNode = p.parse(expression); if (fNode instanceof FunctionNode) { fNode = optimizeFunction((FunctionNode) fNode); } return evaluateNode(fNode); }
functionNode.set(i, new DoubleNode(((NumberNode) functionNode.getNode(i)).doubleValue())); } else if (functionNode.getNode(i) instanceof FunctionNode) { ASTNode optNode = optimizeFunction((FunctionNode) functionNode.getNode(i)); if (!(optNode instanceof DoubleNode)) { doubleOnly = false; return new DoubleNode(evaluateFunction(functionNode)); } catch (Exception e) {
DoubleEvaluator engine = new DoubleEvaluator(); double d = engine.evaluate( strEval ); return Double.toString( d );
/** * Parse the given <code>expression String</code> and store the resulting * ASTNode in this DoubleEvaluator. * * @param expression the expression * @return the AST node * @throws SyntaxError the syntax error */ public ASTNode parse( String expression ) { Parser p = new Parser(); fNode = p.parse( expression ); if ( fNode instanceof FunctionNode ) { fNode = optimizeFunction( (FunctionNode) fNode ); } return fNode; }
/** * Evaluate node logical. * * @param node the node * @return a boolean. */ public boolean evaluateNodeLogical( final ASTNode node ) { if ( node instanceof FunctionNode ) { return evaluateFunctionLogical( (FunctionNode) node ); } if ( node instanceof SymbolNode ) { BooleanVariable v = fBooleanVariables.get( node.toString() ); if ( v != null ) { return v.getValue(); } Boolean boole = SYMBOL_BOOLEAN_MAP.get( node.toString() ); if ( boole != null ) { return boole.booleanValue(); } } throw new ArithmeticException( "EvalDouble#evaluateNodeLogical(ASTNode) not possible for: " + node.toString() ); }
return evaluateFunction( (FunctionNode) node );
@Override public double evaluate(DoubleEvaluator engine, FunctionNode function) { if (function.size() != 3) { throw new ArithmeticMathException( "SetFunction#evaluate(DoubleEvaluator,FunctionNode) needs 2 arguments: " + function.toString()); } if (!(function.getNode(1) instanceof SymbolNode)) { throw new ArithmeticMathException( "SetFunction#evaluate(DoubleEvaluator,FunctionNode) symbol required on the left hand side: " + function.toString()); } String variableName = ((SymbolNode) function.getNode(1)).getString(); double result = engine.evaluateNode(function.getNode(2)); IDoubleValue dv = engine.getVariable(variableName); if (dv == null) { dv = new DoubleVariable(result); } else { dv.setValue(result); } engine.defineVariable(variableName, dv); return result; } }