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/f093d483-aa2c-4823-93ae-b22ab7554491n%40googlegroups.com.