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()
Loaded 'sympy.core.numbers'
Loaded 'sympy.core.cache'
Loaded 'sympy.core.decorators'
Loaded 'sympy.core.exprtools'
Loaded 'sympy.polys.rationaltools'
Loaded 'sympy.polys.polytools'
Loaded '__main__'
Loaded 'runpy'
Then I tried adding a simplify statement, yielding the following snippet:
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) #!!
exp = factor(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)
Backend TkAgg is interactive backend. Turning interactive mode on.
invalid input: Mod(1*(2*0) + 1*(1*1), (1*1)*(1*2))
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).
On Wednesday, April 20, 2022 at 4:05:19 PM UTC+2 Thomas Ligon wrote:
> Yes, that is exactly what I thought, and that there might be some kind of
> unintended side effect that I don't see.
> Here is another example I just got:
> print(exp) #!!
> exp = expand(exp)
> print(exp) #!!
> exp = factor(exp)
> produces:
> (-1*(-2)*(m_0**2 + 2*(-2)*m_0 - 2*(-2)*m_0 + (-2)**2 -
> 2*(-2))/((-2)**1*(m_0**2 + 2*(-2)**2)**1))/factorial(0)
> (((((m_0**2 + (((m_0**(1/1)/1**1)/1**1)/1**1)/1**1 +
> (((m_0**(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)/((((m_0**(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)
> invalid input: 1*1
> This tells me that the result of expand is kind of scary, and the problem
> is in factor (which I probably don't need), but why is the print
> statement producing a very long expression, while the exception just sees a
> 1*1?
> In the output, after the stack trace, I get this information (which might
> be useful to an expert):
> Loaded 'sympy.core.numbers'
> Loaded 'sympy.core.cache'
> Loaded 'sympy.core.decorators'
> Loaded 'sympy.core.exprtools'
> Loaded 'sympy.polys.rationaltools'
> Loaded 'sympy.polys.polytools'
> On Wednesday, April 20, 2022 at 3:11:33 PM UTC+2 [email protected] wrote:
>
>> > I can't see why something that should work the same every time
>> suddenly fails with an exception
>>
>> Since you don't pass variables to the function, it's return value will
>> depend on the value of those variables in the calling context
>> ```
>> def f():
>> return i
>>
>> >>> for i in range(2): f()
>> ...
>> 0
>> 1
>> ```
>>
>> /c
>> On Wednesday, April 20, 2022 at 3:23:44 AM UTC-5 [email protected]
>> wrote:
>>
>>> I am getting an exception where I don't see why it happens.
>>>
>>> The exception is:
>>> invalid input: Mod(1*(2*0) + 1*(1*1), (1*1)*(1*2))
>>>
>>> The statement where it is being thrown is:
>>> print(P3().subs(i, 0))
>>> exp = factor(expand(P3().subs(i, 0)))
>>>
>>> P3() should always return the same thing:
>>> def P3():
>>> exp = 2*i**2 + 4*i*j + 8*i*m - j**2 +2*m*j + 9*m**2
>>> return exp
>>>
>>> The print statement is just for debugging purposes. It shows me multiple
>>> times
>>> -j**2 + 2*j*m + 9*m**2
>>> then
>>> 9*m**2 + j*(2*m) - j**2 + (8*0)*m + (4*0)*j + 2*0**2
>>> invalid input: Mod(1*(2*0) + 1*(1*1), (1*1)*(1*2))
>>>
>>> So, I can't see why something that should work the same every time
>>> suddenly fails with an exception. The statement P3().subs(i, 0) should
>>> always return the same thing.
>>>
>>> A note on my coding style: P3 could easily be a global constant, but the
>>> function paradigm fits very well with the mathematics that I am
>>> reproducing. The most complex case I have runs recursively, which is very
>>> valuable. Also, I am using "exp" as a local variable that means
>>> "expression".
>>>
>>>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/e8887ba1-bdf0-4f52-8087-203aa3c4e480n%40googlegroups.com.