From 366701c094c13b172f75740aaf1b97fcf754ddec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Mon, 5 Nov 2018 11:27:33 +0100 Subject: [PATCH] =?UTF-8?q?Apply=20x+(-y)=20=E2=87=92=20x-y=20simplificati?= =?UTF-8?q?on=20even=20when=20x=20and=20y=20are=20permuted?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Incidentally, this fixes an issue in DataTree::operator=(), where this would cause to the copy to have a different number of nodes than the original. --- src/DataTree.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/DataTree.cc b/src/DataTree.cc index 2b713a81..ae2231e6 100644 --- a/src/DataTree.cc +++ b/src/DataTree.cc @@ -172,11 +172,6 @@ DataTree::AddPlus(expr_t iArg1, expr_t iArg2) { if (iArg1 != Zero && iArg2 != Zero) { - // Simplify x+(-y) in x-y - auto *uarg2 = dynamic_cast<UnaryOpNode *>(iArg2); - if (uarg2 != nullptr && uarg2->get_op_code() == UnaryOpcode::uminus) - return AddMinus(iArg1, uarg2->get_arg()); - // To treat commutativity of "+" // Nodes iArg1 and iArg2 are sorted by index if (iArg1->idx > iArg2->idx) @@ -185,6 +180,12 @@ DataTree::AddPlus(expr_t iArg1, expr_t iArg2) iArg1 = iArg2; iArg2 = tmp; } + + // Simplify x+(-y) in x-y + auto *uarg2 = dynamic_cast<UnaryOpNode *>(iArg2); + if (uarg2 != nullptr && uarg2->get_op_code() == UnaryOpcode::uminus) + return AddMinus(iArg1, uarg2->get_arg()); + return AddBinaryOp(iArg1, BinaryOpcode::plus, iArg2); } else if (iArg1 != Zero) -- GitLab