Simplification of equations

31 views
Skip to first unread message

Mikhael Myara

unread,
Jul 26, 2020, 1:39:32 PM7/26/20
to sympy

a simple code :

import sympy as sp
sp
.var('a b c',nonzero=True)
eq
= Eq(a*b,a*c)
display
(eq)
display(eq.simplify())

Both display exhibit : ab = ac

I would have liked sympy to simply remove 'a', which is possible because I mentioned 'a' is nonzero.

What's wrong ?

Best regards, Mike

Davide Sandona'

unread,
Jul 26, 2020, 2:13:11 PM7/26/20
to sy...@googlegroups.com
Hello Mike,
sadly, what you are trying to do is not easily possible! You are thinking of Eq as an equation, instead in Sympy it is an alias for the class Equality. They are conceptually different and the behaviour you are trying to represent is not possible: you can not apply the same mathematical operation to both sides simultaneously. If you happen to have objects of type Equality, it is better to convert them to an expression and perform a manipulation, for example:

eq.rewrite(sp.Add).collect(a) / a

Alternatively:

sp.Eq(*[arg / a for arg in eq.args])

If you absolutely need an object to represent an equation, take a look at the source code of this pull-request [1]. It's not perfect, but it is a starting point; with that class Equation, you can apply the same mathematical operation to both sides simultaneously.


Davide.


--
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/23535fa8-2842-4259-96aa-4862292c8ae1o%40googlegroups.com.

Mikhael Myara

unread,
Jul 26, 2020, 2:19:20 PM7/26/20
to sympy
Thanks a lot for your answer. I belived "Eq" was a reduction for "Equation" and not for "Equality".
I started to do something similar to what you mention, but your way to do it is smarter and I did and I will use your way.
Thanks a lot,
    Mike.

Question : As you answerd nicely my question I require to close  this request. How may I do it ?


Le dimanche 26 juillet 2020 20:13:11 UTC+2, Davide Sandona' a écrit :
Hello Mike,
sadly, what you are trying to do is not easily possible! You are thinking of Eq as an equation, instead in Sympy it is an alias for the class Equality. They are conceptually different and the behaviour you are trying to represent is not possible: you can not apply the same mathematical operation to both sides simultaneously. If you happen to have objects of type Equality, it is better to convert them to an expression and perform a manipulation, for example:

eq.rewrite(sp.Add).collect(a) / a

Alternatively:

sp.Eq(*[arg / a for arg in eq.args])

If you absolutely need an object to represent an equation, take a look at the source code of this pull-request [1]. It's not perfect, but it is a starting point; with that class Equation, you can apply the same mathematical operation to both sides simultaneously.


Davide.


Il giorno dom 26 lug 2020 alle ore 19:39 Mikhael Myara <mikhae...@umontpellier.fr> ha scritto:

a simple code :

import sympy as sp
sp
.var('a b c',nonzero=True)
eq
= Eq(a*b,a*c)
display
(eq)
display(eq.simplify())

Both display exhibit : ab = ac

I would have liked sympy to simply remove 'a', which is possible because I mentioned 'a' is nonzero.

What's wrong ?

Best regards, Mike

--
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 sy...@googlegroups.com.

gu...@uwosh.edu

unread,
Jul 26, 2020, 9:07:51 PM7/26/20
to sympy
Mike,

I'm the author of the referenced pull request (https://github.com/sympy/sympy/pull/19479) for the equation type. There is a version you can try without having to set up SymPy for development (see https://github.com/gutow/Algebra_with_Sympy, which I try to keep roughly equivalent to the developing SymPy update). As you have interest in this, I would appreciate you trying it and providing feedback about behavior in the pull request. I am hoping to work on this some more as soon as I get things lined up for my Fall classes. I will be trying the Algebra_with_Sympy code with one of my classes this Fall.

Regards,
Jonathan
Reply all
Reply to author
Forward
0 new messages