I've done some more testing. It looks like some simplification of the expression is not happening, but I haven't been able to understand when and why it is done or not done. Here is a test program created by copying the expression values from the debugger into the test program. Everything works as expected, but it looks like the amount of simplification varies.
import sympy
from sympy import symbols, expand, factor, factorial, diff
def createSymbols():
global m, m0
global i, j, k
m = symbols('m')
m0 = symbols('m0')
i, j, k = symbols('i, j, k')
print('end of createSymbols')
createSymbols()
jIn = -2
iIn = -2
exp = (-1*(-2)*(m0**2 + 2*(-2)*m0 - 2*(-2)*m0 + (-2)**2 - 2*(-2))/((-2)**1*(m0**2 + 2*(-2)**2)**1))/factorial(0)
print(exp)
exp = expand(exp)
print(exp)
exp = factor(exp)
print(exp)
exp = -i*(i*j - 2*i*m + j**2 + 2*j*m + m**2)/(j*(2*j**2 + m**2))
print(exp)
if exp != 0:
exp = exp.subs(j, jIn).subs(i, iIn).subs(m, m0)
print(exp)
exp = expand(exp)
print(exp)
exp = factor(exp)
print(exp)
The output is:
end of createSymbols
(2*m0**2 + 16)/(-2*m0**2 - 16)
2*m0**2/(-2*m0**2 - 16) + 16/(-2*m0**2 - 16)
-1
-i*(i*j - 2*i*m + j**2 + 2*j*m + m**2)/(j*(2*j**2 + m**2))
-1
-1
-1
The thread 'MainThread' (0x1) has exited with code 0 (0x0).
Here is a snippet from the code that is having a problem:
exp = Eji()
print('after Eji', exp) #!!
exp = diff(exp, m, kIn)/factorial(kIn)
print('after diff', exp) #!!
if exp != 0:
exp = exp.subs(j, jIn).subs(i, iIn).subs(m, m0)
#exp = factor(expand(exp)) #!!
print('after subs', exp) #!!
#exp = simplify(exp)
#print('after simplify', exp) #!!
exp = expand(exp)
print('after expand', exp) #!!
The output is:
end of createSymbols
after Eji -i*(i*j - 2*i*m + j**2 + 2*j*m + m**2)/(j*(2*j**2 + m**2))
after diff (-i*(i*j - 2*i*m + j**2 + 2*j*m + m**2)/(j*(2*j**2 + m**2)))/factorial(0)
after subs (-1*(-2)*(m0**2 + 2*(-2)*m0 - 2*(-2)*m0 + (-2)**2 - 2*(-2))/((-2)**1*(m0**2 + 2*(-2)**2)**1))/factorial(0)
after expand (((((m0**2 + (((m0**(1/1)/1**1)/1**1)/1**1)/1**1 + (((m0**(1/1)/1**1)/1**1)/1**1)/1**1 - 2/1**1/1**1 + (-2)**2)**(1/1)/1**1)/1**1)/1**1)/((((m0**(2/1**1)/1**1)/1**1)/1**1)/1**1 + (0/1**1)/1**1 + ((((((-2)**2/1**1)/1**1)/1**1)/1**1)/1**1)/1**1))/factorial(0)
Backend TkAgg is interactive backend. Turning interactive mode on.
invalid input: 1*1
Stack trace:
> File "C:\Users\thoma\OneDrive\data-Tom\Tom\Research\HillSeries3\HillSeries3.py", line 198, in E
> exp = factor(exp)
> File "C:\Users\thoma\OneDrive\data-Tom\Tom\Research\HillSeries3\HillSeries3.py", line 1087, in sub2
> exp = exp + E(jInd, iInd, kInd)*(m-m0)**kInd
> File "C:\Users\thoma\OneDrive\data-Tom\Tom\Research\HillSeries3\HillSeries3.py", line 1632, in <module> (Current frame)
> sub2()
Stack trace:
>
>During handling of the above exception, another exception occurred:
>
>
>During handling of the above exception, another exception occurred:
>
>
>During handling of the above exception, another exception occurred:
>
>
>During handling of the above exception, another exception occurred:
>
>
>During handling of the above exception, another exception occurred:
>
>
>During handling of the above exception, another exception occurred:
>
>
>During handling of the above exception, another exception occurred:
>
>
>During handling of the above exception, another exception occurred:
>
>
>During handling of the above exception, another exception occurred:
>
> File "C:\Users\thoma\OneDrive\data-Tom\Tom\Research\HillSeries3\HillSeries3.py", line 194, in E
> exp = simplify(exp)
> File "C:\Users\thoma\OneDrive\data-Tom\Tom\Research\HillSeries3\HillSeries3.py", line 1087, in sub2
> exp = exp + E(jInd, iInd, kInd)*(m-m0)**kInd
> File "C:\Users\thoma\OneDrive\data-Tom\Tom\Research\HillSeries3\HillSeries3.py", line 1632, in <module> (Current frame)
> sub2()
Loaded 'sympy.core.assumptions'
Loaded 'sympy.core.numbers'
Loaded 'sympy.core.cache'
Loaded 'sympy.core.mul'
Loaded 'sympy.core.operations'
Loaded 'sympy.simplify.radsimp'
Loaded 'sympy.core.expr'
Loaded 'sympy.core.add'
Loaded 'sympy.core.power'
Loaded 'sympy.simplify.simplify'
Loaded '__main__'
Loaded 'runpy'
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 462, in getit
return self._assumptions[fact]
KeyError: 'zero'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 462, in getit
return self._assumptions[fact]
KeyError: 'zero'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 462, in getit
return self._assumptions[fact]
KeyError: 'zero'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 462, in getit
return self._assumptions[fact]
KeyError: 'zero'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 462, in getit
return self._assumptions[fact]
KeyError: 'extended_real'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 462, in getit
return self._assumptions[fact]
KeyError: 'zero'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 462, in getit
return self._assumptions[fact]
KeyError: 'extended_real'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 462, in getit
return self._assumptions[fact]
KeyError: 'finite'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 462, in getit
return self._assumptions[fact]
KeyError: 'integer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy/..\debugpy\server\cli.py", line 444, in main
run()
File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy/..\debugpy\server\cli.py", line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 268, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\thoma\OneDrive\data-Tom\Tom\Research\HillSeries3\HillSeries3.py", line 1632, in <module>
sub2()
File "C:\Users\thoma\OneDrive\data-Tom\Tom\Research\HillSeries3\HillSeries3.py", line 1087, in sub2
exp = exp + E(jInd, iInd, kInd)*(m-m0)**kInd
File "C:\Users\thoma\OneDrive\data-Tom\Tom\Research\HillSeries3\HillSeries3.py", line 194, in E
exp = simplify(exp)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\simplify\simplify.py", line 587, in simplify
if isinstance(expr, Expr) and expr.is_zero:
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 466, in getit
return _ask(fact, self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 509, in _ask
a = evaluate(obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\mul.py", line 1321, in _eval_is_zero
z = a.is_zero
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 466, in getit
return _ask(fact, self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 509, in _ask
a = evaluate(obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\mul.py", line 1321, in _eval_is_zero
z = a.is_zero
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 466, in getit
return _ask(fact, self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 509, in _ask
a = evaluate(obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\power.py", line 589, in _eval_is_zero
if self.base.is_zero:
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 466, in getit
return _ask(fact, self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 509, in _ask
a = evaluate(obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\add.py", line 691, in _eval_is_zero
if a.is_extended_real:
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 466, in getit
return _ask(fact, self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 521, in _ask
_ask(pk, obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 521, in _ask
_ask(pk, obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 521, in _ask
_ask(pk, obj)
[Previous line repeated 11 more times]
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 509, in _ask
a = evaluate(obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\power.py", line 1399, in _eval_is_algebraic
if self.base.is_zero or _is_one(self.base):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\power.py", line 1394, in _is_one
return (expr - 1).is_zero
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 466, in getit
return _ask(fact, self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 509, in _ask
a = evaluate(obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\add.py", line 700, in _eval_is_zero
elif (S.ImaginaryUnit*a).is_extended_real:
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 466, in getit
return _ask(fact, self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 521, in _ask
_ask(pk, obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 521, in _ask
_ask(pk, obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 509, in _ask
a = evaluate(obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\expr.py", line 850, in _eval_is_positive
finite = self.is_finite
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 466, in getit
return _ask(fact, self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 521, in _ask
_ask(pk, obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 509, in _ask
a = evaluate(obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\mul.py", line 1578, in _eval_is_odd
is_integer = self.is_integer
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 466, in getit
return _ask(fact, self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 521, in _ask
_ask(pk, obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 521, in _ask
_ask(pk, obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 521, in _ask
_ask(pk, obj)
[Previous line repeated 2 more times]
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\assumptions.py", line 509, in _ask
a = evaluate(obj)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\mul.py", line 1615, in _eval_is_even
n, d = fraction(self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\simplify\radsimp.py", line 1113, in fraction
return Mul(*numer, evaluate=not exact), Mul(*denom, evaluate=not exact)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\cache.py", line 72, in wrapper
retval = cfunc(*args, **kwargs)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\operations.py", line 85, in __new__
c_part, nc_part, order_symbols = cls.flatten(args)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\mul.py", line 630, in flatten
neg1e = Rational(p, q)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\cache.py", line 72, in wrapper
retval = cfunc(*args, **kwargs)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\numbers.py", line 1635, in __new__
p = Rational(p)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\cache.py", line 72, in wrapper
retval = cfunc(*args, **kwargs)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\sympy\core\numbers.py", line 1629, in __new__
The thread 'MainThread' (0x1) has exited with code 0 (0x0).
raise TypeError('invalid input: %s' % p)
TypeError: invalid input: Mod(1*(2*0) + 1*(1*1), (1*1)*(1*2))
The program 'python.exe' has exited with code 4294967295 (0xffffffff).