sympy exception invalid input mod

194 views
Skip to first unread message

Thomas Ligon

unread,
Apr 20, 2022, 4:23:44 AM4/20/22
to sympy
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".

Chris Smith

unread,
Apr 20, 2022, 9:11:33 AM4/20/22
to sympy
>   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

Thomas Ligon

unread,
Apr 20, 2022, 10:05:19 AM4/20/22
to sympy
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'

Thomas Ligon

unread,
Apr 22, 2022, 1:27:29 PM4/22/22
to sympy
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).

Aaron Meurer

unread,
Apr 22, 2022, 2:49:26 PM4/22/22
to sy...@googlegroups.com
It's hard to say for sure without seeing the full traceback. The
Mod(1*(2*0) + 1*(1*1), (1*1)*(1*2)) looks like it might be an
unevaluated expression. If that's the case, those are known to cause
issues because they can break the expectations of various functions.

Aaron Meurer
> --
> 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 sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/c427ce48-8d94-48df-a89c-fea3686b26a3n%40googlegroups.com.

Thomas Ligon

unread,
Apr 25, 2022, 5:42:51 AM4/25/22
to sympy
Thanks! I thought I had copied too much traceback information, but we can handle that later if necessary.
Your remark about unevaluated expressions looks like a very good place for me to look, so I read the documentation and added a line of test code:
print('unevaluated part of exp', UnevaluatedExpr(exp))
Now the output looks like this:
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)
unevaluated part of 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)
How can I avoid this?`
One more remark: I use this when I return a value that should not be expanded, for example:
    with evaluate(False):
        exp3 = (m-m0)**(Rational(2,3))*exp3

but the evaluate(False) should not be in effect here, and if it is, I would need to turn if off.

Thomas Ligon

unread,
Apr 25, 2022, 4:32:56 PM4/25/22
to sympy
I'm still learning...
   if exp != 0:
        exp = exp.subs(j, jIn, evaluate=True).subs(i, iIn, evaluate=True).subs(m, m0, evaluate=True)

and
    exp = simplify(exp, evaluate=True)
didn't have any effect. This one
    exp = exp.doit()
    print('after doit', exp)

ran for 90 minutes and I terminated it.

Thomas Ligon

unread,
Apr 26, 2022, 4:43:20 AM4/26/22
to sympy
I have a solution and, as I thought earlier on, this is a side effect, because the "state" called "evaluate" is a kind of global variable that can cause side effects.
What I mean by side effect is when two functions, A and B, both use a global variable x. When A changes x, it affects how B works, and that can easily be undesired.
My mistake was in this code:
            for kInd in range(0, 6):
                with evaluate(False):

                    exp = exp +  E(jInd, iInd, kInd)*(m-m0)**kInd

What I wanted was to calculate a partial sum (powers of m-m0) and I didn't want these powers to be expanded. The problem is that  evaluate(False) also affects the inner working of E, so I changed it to this:
            for kInd in range(0, 6):
                exp1 = E(jInd, iInd, kInd)
                with evaluate(False):
                    exp = exp + exp1*(m-m0)**kInd

That solves the problem.
Now, while we are on the subject of mistakes I made, here are some more. I saw something that looked like a side effect, and I wanted to set the "global variable" evaluate, but I didn't find that in the documentation, so I tried a few things, like
        exp = exp.subs(j, jIn, evaluate=True).subs(i, iIn, evaluate=True).subs(m, m0, evaluate=True)
This is wrong, because evaluate is not an attribute, but a function.
        exp = exp.subs(j, jIn, evaluate(True)).subs(i, iIn, evaluate(True)).subs(m, m0, evaluate(True))
This is wrong, because subs does not expect a third parameter.
        exp = exp.subs(j, jIn).evaluate(True).subs(i, iIn).evaluate(True).subs(m, m0).evaluate(True)
This is wrong, because the Mul object does not have a method called evaluate.
        with evaluate(True):

            exp = exp.subs(j, jIn).subs(i, iIn).subs(m, m0)

This is correct, and helped me find the solution, but it is unnecessary.

I also tried writing evaluate(False) in my test program, without the with, but that didn't provoke the error.

In summary, what I needed to learn was that evaluate(False) will have an effect on nested functions.

Thanks again to everyone who responded to my request.

David Bailey

unread,
Apr 26, 2022, 6:39:11 AM4/26/22
to sy...@googlegroups.com
On 25/04/2022 21:32, Thomas Ligon wrote:
I'm still learning...
   if exp != 0:
        exp = exp.subs(j, jIn, evaluate=True).subs(i, iIn, evaluate=True).subs(m, m0, evaluate=True)

and
    exp = simplify(exp, evaluate=True)
didn't have any effect. This one
    exp = exp.doit()
    print('after doit', exp)

ran for 90 minutes and I terminated it.

Thomas,

I take it you are not posting your complete code, and I just wonder if the exp() function figures in your complete code, so that using exp as a variable is causing a problem. I'd avoid using the name of a SymPy function as a variable name, just to play safe.

David

Reply all
Reply to author
Forward
0 new messages