/** * {@inheritDoc} Creates a new ASTNode object. */ @Override public FunctionNode createFunction( final SymbolNode head, final ASTNode arg0, final ASTNode arg1 ) { return new FunctionNode( head, arg0, arg1 ); }
private ASTNode getDerivativeResult(ASTNode arg1Derived, FunctionNode fun) { if (!arg1Derived.equals(new DoubleNode(1.0))) { FunctionNode res = new FunctionNode(fASTFactory.createSymbol("Times")); res.add(arg1Derived); res.add(fun); return res; } return fun; }
/** * Internal helper using the mathclipse framework. * * @param aFunctionNode The node to be processed. * * @return The {@link Criteria} (tree) extracted from the provided node. * * @throws ParseException Thrown in case there were problems working with * the given node. */ private Criteria toCriteria( FunctionNode aFunctionNode ) throws ParseException { if ( aFunctionNode.size() == 3 ) { if ( (aFunctionNode.get( 0 ) instanceof SymbolNode) && (aFunctionNode.get( 1 ) instanceof SymbolNode) && (aFunctionNode.get( 2 ) instanceof SymbolNode) ) { return toCriteria( (SymbolNode) aFunctionNode.get( 0 ), (SymbolNode) aFunctionNode.get( 1 ), aFunctionNode.get( 2 ) ); } else if ( (aFunctionNode.get( 0 ) instanceof SymbolNode) && (aFunctionNode.get( 1 ) instanceof SymbolNode) && (aFunctionNode.get( 2 ) instanceof NumberNode) ) { return toCriteria( (SymbolNode) aFunctionNode.get( 0 ), (SymbolNode) aFunctionNode.get( 1 ), aFunctionNode.get( 2 ) ); } else if ( (aFunctionNode.get( 0 ) instanceof SymbolNode) && (aFunctionNode.get( 1 ) instanceof FunctionNode) && (aFunctionNode.get( 2 ) instanceof SymbolNode) ) { return toCriteria( (SymbolNode) aFunctionNode.get( 0 ), (FunctionNode) aFunctionNode.get( 1 ), (SymbolNode) aFunctionNode.get( 2 ) ); } else if ( (aFunctionNode.get( 0 ) instanceof SymbolNode) && (aFunctionNode.get( 1 ) instanceof FunctionNode) && (aFunctionNode.get( 2 ) instanceof FunctionNode) ) { return toCriteria( (SymbolNode) aFunctionNode.get( 0 ), (FunctionNode) aFunctionNode.get( 1 ), (FunctionNode) aFunctionNode.get( 2 ) ); } else if ( (aFunctionNode.get( 0 ) instanceof SymbolNode) && (aFunctionNode.get( 1 ) instanceof SymbolNode) && (aFunctionNode.get( 2 ) instanceof FunctionNode) ) { return toCriteria( (SymbolNode) aFunctionNode.get( 0 ), (SymbolNode) aFunctionNode.get( 1 ), (FunctionNode) aFunctionNode.get( 2 ) ); } } throw new ParseException( aFunctionNode.getString(), 0 ); }
public Complex evaluate(IEvaluator<Complex, ComplexVariable> 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(); Complex result = engine.evaluateNode(function.getNode(2)); ComplexVariable dv = engine.getVariable(variableName); if (dv == null) { dv = new ComplexVariable(result); } else { dv.setValue(result); } engine.defineVariable(variableName, dv); return result; } }
if (functionNode.size() > 0) { boolean complexOnly = true; ASTNode node; for (int i = 1; i < functionNode.size(); i++) { node = functionNode.getNode(i); if (node instanceof NumberNode) { functionNode.set(i, new ComplexNode(((NumberNode) functionNode.getNode(i)).doubleValue())); } else if (functionNode.getNode(i) instanceof FunctionNode) { ASTNode optNode = optimizeFunction((FunctionNode) functionNode.getNode(i)); if (!(optNode instanceof ComplexNode)) { complexOnly = false; functionNode.set(i, optNode); } else if (node instanceof SymbolNode) { Complex c = SYMBOL_MAP.get(node.toString()); if (c != null) { functionNode.set(i, new ComplexNode(c)); } else { complexOnly = false;
if (f.size() > 1 && f.getNode(0) instanceof SymbolNode) { SymbolNode head = (SymbolNode) f.getNode(0); if (f.size() == 2) { ASTNode arg1Derived = derivative(f.getNode(1), var); if (isSymbol(head, "Exp")) { FunctionNode fun = new FunctionNode(fASTFactory.createSymbol("Exp")); fun.add(f.getNode(1)); return getDerivativeResult(arg1Derived, fun); FunctionNode fun = new FunctionNode(fASTFactory.createSymbol("Times")); fun.add(new DoubleNode(-1.0)); fun.add(new FunctionNode(fASTFactory.createSymbol("Cos"), f.getNode(1))); return getDerivativeResult(arg1Derived, fun); FunctionNode fun = new FunctionNode(fASTFactory.createSymbol("Cos")); fun.add(f.getNode(1)); return getDerivativeResult(arg1Derived, fun); } else if (f.size() == 3 && isSymbol(head, "Power")) { if (f.get(2).isFree(var)) {// derive x^r ASTNode arg1Derived = derivative(f.getNode(1), var); new DoubleNode(-1.0), f.get(2)); FunctionNode fun = fASTFactory.createFunction(fASTFactory.createSymbol("Times"), f.get(2), fASTFactory.createFunction(fASTFactory.createSymbol("Power"), f.get(1), exponent)); return getDerivativeResult(arg1Derived, fun); if (f.get(1).isFree(var)) {// derive a^x
/** * construct the arguments for an expression * */ private void getArguments(final FunctionNode function) throws SyntaxError { do { function.add(parseExpression()); if (fToken != TT_COMMA) { break; } getNextToken(); if (fToken == TT_PRECEDENCE_CLOSE || fToken == TT_ARGUMENTS_CLOSE) { function.add(new SymbolNode("Null")); break; } } while (true); }
@Override public double evaluate(DoubleEvaluator doubleEngine, FunctionNode functionNode, double[] args) { ASTNode node = functionNode.getNode(0); if (node instanceof SymbolNode) { AST2Expr ast2Expr = new AST2Expr(); IExpr head = ast2Expr.convert(node); IASTAppendable fun = F.ast(head, args.length, false); fun.appendArgs(0, args.length, i -> F.num(args[i])); // for (int i = 0; i < args.length; i++) { // fun.append(F.num(args[i])); // } final IExpr result = F.evaln(fun); if (result.isReal()) { return ((ISignedNumber) result).doubleValue(); } } else if (node instanceof FunctionNode) { AST2Expr ast2Expr = new AST2Expr(); IExpr head = ast2Expr.convert(node); IASTAppendable fun = F.ast(head); for (int i = 0; i < args.length; i++) { fun.append(F.num(args[i])); } final IExpr result = F.evaln(fun); if (result.isReal()) { return ((ISignedNumber) result).doubleValue(); } } throw new MathException("CoreCallbackFunction#evaluate() not possible for: " + functionNode.toString()); }
/** * Internal helper using the mathclipse framework. * * @param aSymbolNodeA The node A to be processed. * @param aFunctionNodeB The node B to be processed. * @param aFunctionNodeC The node C to be processed. * * @return The {@link Criteria} (tree) extracted from the provided nodes. * * @throws ParseException Thrown in case there were problems working with * the given node. */ private Criteria toCriteria( SymbolNode aSymbolNodeA, FunctionNode aFunctionNodeB, FunctionNode aFunctionNodeC ) throws ParseException { if ( aSymbolNodeA.getString().equalsIgnoreCase( NODE ) ) { Criteria theCriteriaB = toCriteria( aFunctionNodeB ); Criteria theCriteriaC = toCriteria( aFunctionNodeC ); if ( theCriteriaB instanceof CriteriaNode ) { CriteriaNode theCriteriaNode = (CriteriaNode) theCriteriaB; theCriteriaNode.addChild( theCriteriaC ); return theCriteriaNode; } } throw new ParseException( "Unable to parse: \"" + aSymbolNodeA.getString() + "\", \"" + aFunctionNodeB.getString() + "\" and/or \"" + aFunctionNodeC.getString() + "\"", 0 ); }
/** {@inheritDoc} */ @Override public boolean isFree(final ASTNode node) { if (this.equals(node)) { return false; } for (int i = 0; i < fNodesList.size(); i++) { if (!fNodesList.get(i).isFree(node)) { return false; } } return true; }
public boolean evaluateFunctionLogical(final FunctionNode functionNode) { if (functionNode.size() > 0 && functionNode.getNode(0) instanceof SymbolNode) { String symbol = functionNode.getNode(0).toString(); if (functionNode.size() == 2) { Object obj = FUNCTION_BOOLEAN_MAP.get(symbol); if (obj instanceof IBooleanBoolean1Function) { return ((IBooleanBoolean1Function) obj).evaluate(evaluateNodeLogical(functionNode.getNode(1))); } } else if (functionNode.size() == 3) { Object obj = FUNCTION_BOOLEAN_MAP.get(symbol); if (obj instanceof IBooleanComplex2Function) { return ((IBooleanComplex2Function) 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))); } // } else { // Object obj = FUNCTION_BOOLEAN_MAP.get(symbol); // if (obj instanceof IBooleanDoubleFunction) { // return ((IBooleanDoubleFunction) obj).evaluate(this, functionNode); // } } } throw new ArithmeticException("EvalDouble#evaluateFunctionLogical(FunctionNode) not possible for: " + functionNode.toString()); }
if (functionNode.size() > 0) { boolean doubleOnly = true; ASTNode node; for (int i = 1; i < functionNode.size(); i++) { node = functionNode.getNode(i); if (node instanceof NumberNode) { 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; functionNode.set(i, optNode); } else if (node instanceof SymbolNode) { Double dbl = SYMBOL_DOUBLE_MAP.get(node.toString()); if (dbl != null) { functionNode.set(i, new DoubleNode(dbl.doubleValue())); } else { doubleOnly = false;
@Override public ASTNode createFunction(final INodeParserFactory factory, final ASTNode lhs, final ASTNode rhs) { if (fOperatorString.equals("@")) { FunctionNode fn = factory.createAST(lhs); fn.add(rhs); return fn; } FunctionNode fn = factory.createFunction(factory.createSymbol("Apply"), lhs, rhs); if (fOperatorString.equals("@@")) { return fn; } // case "@@@" fn.add(factory.createFunction(factory.createSymbol("List"), factory.createInteger(1))); return fn; } }
/** * Internal helper using the mathclipse framework. * * @param aSymbolNodeA The node A to be processed. * @param aSymbolNodeB The node B to be processed. * @param aFunctionNodeC The node C to be processed. * * @return The {@link Criteria} (tree) extracted from the provided nodes. * * @throws ParseException Thrown in case there were problems working with * the given node. */ private Criteria toCriteria( SymbolNode aSymbolNodeA, SymbolNode aSymbolNodeB, FunctionNode aFunctionNodeC ) throws ParseException { if ( aSymbolNodeA.getString().equalsIgnoreCase( NODE ) ) { Criteria theCriteria = toCriteria( aFunctionNodeC ); if ( aSymbolNodeB.getString().equalsIgnoreCase( NOT ) ) { return not( theCriteria ); } } throw new ParseException( "Unable to parse: \"" + aSymbolNodeA.getString() + "\", \"" + aSymbolNodeB.getString() + "\" and/or \"" + aFunctionNodeC.getString() + "\"", 0 ); }
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 (functionNode.size() > 0) { boolean doubleOnly = true; ASTNode node; for (int i = 1; i < functionNode.size(); i++) { node = functionNode.getNode(i); if (node instanceof NumberNode) { 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; functionNode.set(i, optNode); } else if (node instanceof SymbolNode) { Double dbl = SYMBOL_DOUBLE_MAP.get(node.toString()); if (dbl != null) { functionNode.set(i, new DoubleNode(dbl.doubleValue())); } else { doubleOnly = false;