private boolean containsMacroCall(Expression e) { final LinkedList<MacroCall> calls = new LinkedList<MacroCall>(); e.accept(new DFGTraversalVisitor() { @Override public void visit(MacroCall node) { calls.add(node); } }); return !calls.isEmpty(); }
if (old == left) { left = newExpression; } else if (left.isComposite()) { left.replaceExpression(old, newExpression); } else if (right.isComposite()) { right.replaceExpression(old, newExpression);
@Override public void visit(AssignmentNode node) { node.getValue().accept(dfgReplacer); if (dfgReplacer.result != null) { node.setValue(dfgReplacer.result); dfgReplacer.result = null; } if (replaceMap.containsKey(node.getVariable().getName())) node.setVariable((Variable) replaceMap.get(node.getVariable().getName()).copy()); }
/** * Performs constant propagtion on an expression and returns the result expression * @param expression the expression for constant propagtion * @return the result expression, where constant variables are constant */ private Expression performConstantPropagationOnExpression(Expression expression) { dfgVisitorUsedVariables.getVariables().clear(); expression.accept(dfgVisitorUsedVariables); for (VariableComponent varComp : dfgVisitorUsedVariables.getVariables()) { if (mapConstants.containsKey(varComp)) { // replace variable with constant if (expression == varComp.getReferredExpression()) { expression = mapConstants.get(varComp); } else { expression.replaceExpression(varComp.getReferredExpression(), mapConstants.get(varComp)); } setGraphModified(); } } // do a constant folding on the value expression.accept(constantFolding); if (constantFolding.isGraphModified()) { setGraphModified(); } return constantFolding.getResultExpr(); }
@Override public void replaceExpression(Expression old, Expression newExpression) { if (old == operand) { operand = newExpression; } else { operand.replaceExpression(old, newExpression); } }
public static String bracketComposite(Expression expression) { if (expression.isComposite()) { return "(" + expression + ")"; } else { return expression.toString(); } }
@Override public void visit(Multiplication node) { node.getLeft().accept(this); Expression dLeft = result; node.getRight().accept(this); Expression dRight = result; if (dLeft == zero) { if (dRight == zero) { result = zero; } else { result = new Multiplication(node.getLeft().copy(), dRight); } } else { //dLeft != zero !! if (dRight == zero) { result = new Multiplication(dLeft, node.getRight().copy()); } else { result = new Addition( new Multiplication(dLeft, node.getRight().copy()), new Multiplication(node.getLeft().copy(), dRight) ); } } }
@Override public void replaceExpression(Expression old, Expression newExpression) { if (variable == old && newExpression instanceof Variable) { variable = (Variable) newExpression; } if (value == old) { value = newExpression; } else { // recursively try to replace expression value.replaceExpression(old, newExpression); } }
private String bladeToString(Expression blade) { if (!blade.isComposite()) return blade.toString(); OuterProduct outerProduct = (OuterProduct) blade; return bladeToString(outerProduct.getLeft())+"^"+bladeToString(outerProduct.getRight()); }
@Override public void visit(Division node) { node.getLeft().accept(this); Expression dLeft = result; node.getRight().accept(this); Expression dRight = result; new Division( new Multiplication( node.getLeft().copy(), dRight ), new Multiplication( node.getRight().copy(), node.getRight().copy() new Multiplication( dLeft, node.getRight().copy() ), new Multiplication( node.getRight().copy(), node.getRight().copy() result = new Division( new Subtraction( new Multiplication(dLeft, node.getRight().copy()), new Multiplication(node.getLeft().copy(), dRight) ), new Multiplication(node.getRight().copy(), node.getRight().copy()
/** * Creates an expression node that negates the given expression. * * @param value The expression that should be negated. A deep copy will be created. * @return A Negation object that negates the given expression. */ public static Negation negate(Expression value) { return new Negation(value.copy()); }
@Override public void replaceExpression(Expression old, Expression newExpression) { if (expression == old) { expression = newExpression; } else { expression.replaceExpression(old, newExpression); } }
/** * Creates an exponentation node that squares the given expression. * * @param base The expression that should be squared. A deep copy will be created. * @return An exponentation object that exponentiates the given base with a float constant of 2.0. */ public static Multiplication square(Expression base) { return new Multiplication(base, base.copy()); }
@Override public void replaceExpression(Expression old, Expression newExpression) { for (int i = 0; i < args.size(); i++) { Expression arg = args.get(i); if (arg == old) { args.set(i, newExpression); } else { arg.replaceExpression(old, newExpression); } } }
/** * Returns the direct summands of a given expression * @param expresssion The expression * @return The direct summands */ public static Sum getSignedSummands(Expression expresssion) { SignedSummandsGetter getter = new SignedSummandsGetter(); expresssion.accept(getter); return getter.summands; }
@Override public void visit(Exponentiation node) { node.getLeft().accept(this); Expression dLeft = result; node.getRight().accept(this); Expression dRight = result; node.getRight().copy(), new Exponentiation( node.getLeft().copy(), new Subtraction(node.getRight().copy(), new FloatConstant(1)) node.getRight().accept(this); Expression dv = result; result = new Multiplication( new Multiplication(dv, new MathFunctionCall(node.getLeft().copy(), MathFunction.LOG)), node.copy() ); } else { node.getRight().accept(this); Expression dv = result; result = new Multiplication(new Addition( new Multiplication(dv, new MathFunctionCall(node.getLeft().copy(), MathFunction.LOG)), new Division(new Multiplication(node.getRight().copy(), dLeft), node.getLeft().copy()) ), node.copy());