/** * Gets argument value. * * @return direct argument value for free argument, * otherwise returns calculated argument value * based on the argument expression. */ public double getArgumentValue() { if (argumentType == FREE_ARGUMENT) return argumentValue; else return argumentExpression.calculate(); } /**
/** * Calculates function f(x0) (given as expression) assigning Argument x = x0; * * * @param f the expression * @param x the argument * @param x0 the argument value * * @return f.calculate() * * @see Expression */ public static final double getFunctionValue(Expression f, Argument x, double x0) { x.setArgumentValue(x0); return f.calculate(); } /**
@Override protected void testScenario() { Expression e = new Expression(test.exprStr); for (int i = 0; i <= super.iterNum; i++) { if (mXparser.isCurrentCalculationCancelled()) break; e.calculate(); } } }
/** * Forward difference(1) operator (at current value of argument x) * * @param f the expression * @param x the argument name * * @return Forward difference(1) value calculated at the current value of argument x. * * @see Expression * @see Argument */ public static final double forwardDifference(Expression f, Argument x) { double xb = x.getArgumentValue(); if (Double.isNaN(xb)) return Double.NaN; double fv = f.calculate(); x.setArgumentValue(xb + 1); double delta = f.calculate() - fv; x.setArgumentValue(xb); return delta; } /**
/** * Forward difference(h) operator (at the current value of the argument x) * * @param f the expression * @param h the difference * @param x the argument name * * @return Forward difference(h) value calculated at at the current value of the argument x. * * @see Expression * @see Argument */ public static final double forwardDifference(Expression f, double h, Argument x) { double xb = x.getArgumentValue(); if (Double.isNaN(xb)) return Double.NaN; double fv = f.calculate(); x.setArgumentValue(xb + h); double delta = f.calculate() - fv; x.setArgumentValue(xb); return delta; } /**
/** * Backward difference(h) operator (at the current value of the argument x) * * @param f the expression * @param h the difference * @param x the argument name * * @return Backward difference(h) value calculated at at the current value of the argument x. * * @see Expression * @see Argument */ public static final double backwardDifference(Expression f, double h, Argument x) { double xb = x.getArgumentValue(); if (Double.isNaN(xb)) return Double.NaN; double fv = f.calculate(); x.setArgumentValue(xb - h); double delta = fv - f.calculate(); x.setArgumentValue(xb); return delta; } /**
/** * Backward difference(1) operator (at current value of argument x) * * @param f the expression * @param x the argument name * * @return Backward difference(1) value calculated at the current value of argument x. * * @see Expression * @see Argument */ public static final double backwardDifference(Expression f, Argument x) { double xb = x.getArgumentValue(); if (Double.isNaN(xb)) return Double.NaN; double fv = f.calculate(); x.setArgumentValue(xb - 1); double delta = fv - f.calculate(); x.setArgumentValue(xb); return delta; } /**
/** * Calculates function value * * @return Function value as double. */ public double calculate() { if (functionBodyType == BODY_RUNTIME) return functionExpression.calculate(); else if (isVariadic == false) return functionExtension.calculate(); else { List<Double> paramsList = functionExpression.UDFVariadicParamsAtRunTime; if (paramsList != null) { int n = paramsList.size(); double[] parameters = new double[n]; for (int i = 0; i < n; i++) parameters[i] = paramsList.get(i); return functionExtensionVariadic.calculate(parameters); } else return Double.NaN; } } /**
@Override protected void testScenario() { Expression e = new Expression(""); for (int i = 0; i <= super.iterNum; i++) { if (mXparser.isCurrentCalculationCancelled()) break; e = new Expression("sin(2+(3*4)^2)/10"); } e.calculate(); } }
@Override protected void testScenario() { Argument x = new Argument("x"); Expression e = new Expression(test.exprStr, x); for (int i = 0; i <= super.iterNum; i++) { if (mXparser.isCurrentCalculationCancelled()) break; x.setArgumentValue(i); e.calculate(); } } }
/** * Constructor for function definition in natural math language, * for instance providing on string "f(x,y) = sin(x) + cos(x)" * is enough to define function "f" with parameters "x and y" * and function body "sin(x) + cos(x)". * * @param constantDefinitionString Constant definition in the form * of one String, ie "c = 2" or "c = 2*sin(pi/3)" * @param elements Optional parameters (comma separated) such as Arguments, Constants, Functions */ public Constant(String constantDefinitionString, PrimitiveElement...elements) { super(Constant.TYPE_ID); description = ""; syntaxStatus = SYNTAX_ERROR_OR_STATUS_UNKNOWN; relatedExpressionsList = new ArrayList<Expression>(); if ( mXparser.regexMatch(constantDefinitionString, ParserSymbol.constUnitgDefStrRegExp) ) { HeadEqBody headEqBody = new HeadEqBody(constantDefinitionString); constantName = headEqBody.headTokens.get(0).tokenStr; Expression bodyExpression = new Expression(headEqBody.bodyStr, elements); constantValue = bodyExpression.calculate(); syntaxStatus = bodyExpression.getSyntaxStatus(); errorMessage = bodyExpression.getErrorMessage(); } else errorMessage = "[" + constantDefinitionString + "] " + "--> pattern not mathes: " + ParserSymbol.constUnitgDefStrRegExp; } /**
/** * IF function * * @param pos the token position */ private void IF_CONDITION(int pos) { /* * Get condition string * 1st parameter * The goal is to avoid calculation * of not needed part of IF function * Example: If(1=1, 2, sin(3) ) - here sin(3) does not * require to be calculated. */ List<FunctionParameter> ifParams = getFunctionParameters(pos, tokensList); FunctionParameter ifParam = ifParams.get(0); Expression ifExp = new Expression(ifParam.paramStr, ifParam.tokens, argumentsList, functionsList, constantsList, KEEP_ULP_ROUNDING_SETTINGS, UDFExpression, UDFVariadicParamsAtRunTime); if (verboseMode == true) ifExp.setVerboseMode(); ifSetRemove(pos, ifExp.calculate()); } /**
if (isVariadic) { if (functionBodyType == BODY_RUNTIME) return functionExpression.calculate(); else return functionExtensionVariadic.calculate(parameters); for (int p = 0; p < parameters.length; p++) setArgumentValue(p, parameters[p]); return functionExpression.calculate(); } else { for (int p = 0; p < parameters.length; p++)
try { e = new Expression(expr); double d = e.calculate(); ans = new BigDecimal(d, MathContext.DECIMAL64);
try { e = new Expression(expr); double d = e.calculate(); ans = new BigDecimal(d, MathContext.DECIMAL64);
/** * Backward diffrence operator * * @param pos the token position */ private void BACKWARD_DIFFERENCE(int pos) { List<FunctionParameter> params = getFunctionParameters(pos, tokensList); FunctionParameter funParam = params.get(0); FunctionParameter xParam = params.get(1); ArgumentParameter x = getParamArgument(xParam.paramStr); Expression funExp = new Expression(funParam.paramStr, funParam.tokens, argumentsList, functionsList, constantsList, DISABLE_ULP_ROUNDING, UDFExpression, UDFVariadicParamsAtRunTime); if (verboseMode == true) funExp.setVerboseMode(); double h = 1; if (params.size() == 3) { FunctionParameter hParam = params.get(2); Expression hExp = new Expression(hParam.paramStr, hParam.tokens, argumentsList, functionsList, constantsList, DISABLE_ULP_ROUNDING, UDFExpression, UDFVariadicParamsAtRunTime); if (verboseMode == true) hExp.setVerboseMode(); h = hExp.calculate(); } calcSetDecreaseRemove(pos, Calculus.backwardDifference(funExp, h, x.argument) ); clearParamArgument(x); } /**
/** * Forward difference operator * * @param pos the token position */ private void FORWARD_DIFFERENCE(int pos) { List<FunctionParameter> params = getFunctionParameters(pos, tokensList); FunctionParameter funParam = params.get(0); FunctionParameter xParam = params.get(1); ArgumentParameter x = getParamArgument(xParam.paramStr); Expression funExp = new Expression(funParam.paramStr, funParam.tokens, argumentsList, functionsList, constantsList, DISABLE_ULP_ROUNDING, UDFExpression, UDFVariadicParamsAtRunTime); if (verboseMode == true) funExp.setVerboseMode(); double h = 1; if (params.size() == 3) { FunctionParameter hParam = params.get(2); Expression hExp = new Expression(hParam.paramStr, hParam.tokens, argumentsList, functionsList, constantsList, DISABLE_ULP_ROUNDING, UDFExpression, UDFVariadicParamsAtRunTime); if (verboseMode == true) hExp.setVerboseMode(); h = hExp.calculate(); } calcSetDecreaseRemove(pos, Calculus.forwardDifference(funExp, h, x.argument) ); clearParamArgument(x); } /**
if (isVariadic) { if (functionBodyType == BODY_RUNTIME) return functionExpression.calculate(); else return functionExtensionVariadic.calculate(parameters); for (int p = 0; p < arguments.length; p++) setArgumentValue(p, arguments[p].getArgumentValue()); return functionExpression.calculate(); } else { for (int p = 0; p < arguments.length; p++)
@Override protected void testScenario() { Argument x = new Argument("x"); Argument y = new Argument("y"); Function f = new Function("f(x,y)=3*x+4*y"); Expression e = new Expression(test.exprStr, f, x, y); for (int i = 0; i <= super.iterNum; i++) { if (mXparser.isCurrentCalculationCancelled()) break; x.setArgumentValue(i); y.setArgumentValue(i); e.calculate(); } } }