Simple operations on equations

37 views
Skip to first unread message

Rathmann

unread,
Sep 11, 2014, 11:07:56 PM9/11/14
to sy...@googlegroups.com
I have been playing with SymPy and IPython Notebook and thought it
would be useful to be able to explicitly perform some of the
operations that are in the toolset taught in a high-school algebra
class.

This would be things like 
  • Adding the same thing to both sides of an equation.
  • Creating a new equation by adding two other equations together.
  • Substituting one equation into another.

I realize that all of these are easy to synthesize from the existing
operations in SymPy, but didn't see them in any sort of a pre-packaged
form.

Are they there but I have just missed them?

If not, would they be a worthwhile addition to SymPy?

In case there is any ambiguity, I am speaking explicitly of the
Equality/Eq relations.  I know that SymPy and many other packages tend
to just use expressions with an implicit ==0 condition, but, in an
elementary context, explicit equations seem more familiar/easier to
understand.

The main use case would be when using a notebook to show the explicit
steps of a derivation.  (And especially at the level of high-school
algebra or science.)  In one of my physics notebooks I noticed myself
writing a lot of things like

    eq3_4 = eq3_3.subs(eq2_1.rhs, eq2_1.lhs)

and thinking that having a well-documented set of idioms could add clarity.


F. B.

unread,
Sep 12, 2014, 3:59:33 AM9/12/14
to sy...@googlegroups.com
This could be achieved by defining Eq's behaviour wrt addition, multiplication by expressions or other Eq objects.

F. B.

unread,
Sep 12, 2014, 4:05:15 AM9/12/14
to sy...@googlegroups.com
Some of Eq's problems:

In [1]: srepr(Eq(x, y)*2)
Out[1]: "Mul(Integer(2), Equality(Symbol('x'), Symbol('y')))"

In [2]: Eq(x, y)*2
Out[2]: 2⋅x = y

The formatted output is wrong, the tree expression corresponds to Mul(2, Eq(x, y)), not to Eq(Mul(2, x), y) as it looks like in the formatted output.

In any case, I would suggest to change the behavior to the one displayed in Out[2] even in the expression tree, both for Eq( ... ) * ... and for Eq( ... ) + ...

In [3]: Eq(x, y) + Eq(z, t)
Out[3]: x = y + z = t

In [4]: srepr(Eq(x, y)+Eq(z, t))
Out[4]: "Add(Equality(Symbol('x'), Symbol('y')), Equality(Symbol('z'), Symbol('t')))"

Again, maybe this should rather produce Eq(x+z, y+t)?

By the way, is there any way to swap the hand-sides of Equality? Otherwise it could be easily added something like:

>>> Eq(x, y).swap_hs()
y == x

F. B.

unread,
Sep 12, 2014, 7:34:23 AM9/12/14
to sy...@googlegroups.com

Rathmann

unread,
Sep 12, 2014, 3:56:35 PM9/12/14
to sy...@googlegroups.com
Cool, thanks!  I'll pull down your branch and play with it.

For something like this, I think the main issue is defing a good interface, and then how to document it.

One caution is that SymPy does already define operations on Equality, e.g., Eq(a,c)+c gives c+(a==b).
Where the latter makes sense if you think of the logical outcome of a test being coerced c-style into an int. 

(In my Python, True+True gives 2)

Aaron Meurer

unread,
Sep 12, 2014, 9:18:53 PM9/12/14
to sy...@googlegroups.com
On Fri, Sep 12, 2014 at 2:56 PM, Rathmann <rathm...@gmail.com> wrote:
> Cool, thanks! I'll pull down your branch and play with it.
>
> For something like this, I think the main issue is defing a good interface,
> and then how to document it.
>
> One caution is that SymPy does already define operations on Equality, e.g.,
> Eq(a,c)+c gives c+(a==b).
> Where the latter makes sense if you think of the logical outcome of a test
> being coerced c-style into an int.

SymPy booleans are not integers. This only works because Add doesn't
explicitly disallow Eq, but the expression is not semantically
meaningful.

Aaron Meurer

>
> (In my Python, True+True gives 2)
>
> On Friday, September 12, 2014 4:34:23 AM UTC-7, Francesco Bonazzi wrote:
>>
>> Here we go: https://github.com/sympy/sympy/pull/8023
>
> --
> 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 post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/346e8015-98f4-4bd9-aed6-23dadfc63110%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages