/** * Define a value for a given variable name. * * @param variableName * @param value */ public void defineVariable(String variableName, double value) { if (fRelaxedSyntax) { fVariableMap.put(variableName.toLowerCase(), new DoubleVariable(value)); } else { fVariableMap.put(variableName, new DoubleVariable(value)); } }
/** * Define a value for a given variable name. * * @param variableName * @param value */ public void defineVariable(String variableName) { if (fRelaxedSyntax) { fVariableMap.put(variableName.toLowerCase(), new DoubleVariable(0.0)); } else { fVariableMap.put(variableName, new DoubleVariable(0.0)); } }
public static boolean isValid(String fun, String[] var) { EvalDouble dEval = new EvalDouble(true); for (String v : var) { dEval.defineVariable(v, new DoubleVariable(0.0)); } try { dEval.parse(fun); return true; } catch (Exception e) { return false; } }
public static boolean[] isValid(String[] fun, String[] var) { EvalDouble dEval = new EvalDouble(true); for (String v : var) { dEval.defineVariable(v, new DoubleVariable(0.0)); } boolean[] b = new boolean[fun.length]; for (int i = 0; i < fun.length; i++) { try { dEval.parse(fun[i]); b[i] = true; } catch (Exception e) { b[i] = false; } } return b; }
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; } }
/** * 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; } }
@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; } }