From 668a00b049fd1fcdcc90c24bbc9dd296723e6f24 Mon Sep 17 00:00:00 2001 From: Ondrej Certik Date: Fri, 15 Aug 2008 22:51:22 +0200 Subject: [PATCH] Add a special case to Mul.canonize that speed things up. Signed-off-by: Ondrej Certik --- sympy_py.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/sympy_py.py b/sympy_py.py index af5606c..2d00aa1 100644 --- a/sympy_py.py +++ b/sympy_py.py @@ -272,6 +272,23 @@ class Mul(Basic): d = HashTable() else: d = {} + if len(args) == 2 and args[0].type == MUL and args[1].type == INTEGER: + a, b = args + assert a.type == MUL + assert b.type == INTEGER + if b.i == 1: + return a + if b.i == 0: + return b + if a.args[0].type == INTEGER: + if a.args[0].i == 1: + args = (b,) + a.args[1:] + else: + args = (b*a.args[0],) + a.args[1:] + else: + args = (b,)+a.args + return Mul(args, False) + num = Integer(1) for a in args: if a.type == INTEGER: @@ -407,9 +424,9 @@ class Pow(Basic): base, exp = args if base.type == INTEGER: if base.i == 0: - return Integer(0) + return base if base.i == 1: - return Integer(1) + return base if exp.type == INTEGER: if exp.i == 0: return Integer(1) -- 2.11.4.GIT