[sympy] solving expression step by step

2,170 views
Skip to first unread message

klan...@gmail.com

unread,
Apr 18, 2010, 4:02:33 AM4/18/10
to sympy
Hi,

I'm looking for library which can be used as helper when solving maths
equations.

I would like to let student enter expression like:

2*3+4+5

This expression should be rendered with changes:
out: 2*3+4+5

Now student can apply some transformation:

1. Add transformation
out: 2*3+9

2.Multi transformation
out: 6+9

3. Add transformation
out: 15

Can sympy be used in this context.
Some examples how to do this would be great :-)

Thank you
Krzysztof

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.

Keir Mierle

unread,
Apr 18, 2010, 2:43:55 PM4/18/10
to sy...@googlegroups.com
+1

I would like something like this. Sometimes to get intuition about what's happening in an equation, you have to rearrange it in a particular way; but this is tedious for non-trivial equations.

Keir

smichr

unread,
Apr 19, 2010, 3:09:08 AM4/19/10
to sympy
Do you want the computer to make the decision about which Add to do
next? If they choose the wrong op, do you just want to output the same
expression? e.g.

>> eq
2*3+4+5
>> eq.mul()
6+4+5
>> eq.add()
10+5
>> eq.add()
15

vs

>> eq
2*3+4+5
>> eq.add()
2*3+9
>> eq.add()
2*3+9
>> eq.mul()
6+9
>> eq.add()
15

Interestingly, this is somewhat related to what I am trying to do with
rebuilding expressions. One way to do this is to selectively update
parts of an unevaluate expression, but in trying to do that I ran the
problem that things like Mul(2,3,evaluate=False) still evaluate to 6.
So one might have to use a little trickery to get this to work by
using symbols for numbers:

>>> Add(*[Mul(*[Symbol('2'),Symbol('3')]),Symbol('4'),Symbol('5')])
4 + 5 + 2*3
>>> srepr(_)
"Add(Symbol('4'), Symbol('5'), Mul(Symbol('2'), Symbol('3')))"
>>>

For the simple 4-ops, one can allow any operation as long as it
involves only two numbers/symbols. For more complicated things
(list .distrubute()) you would have other criteria.

I think it's doable.

klan...@gmail.com

unread,
Apr 19, 2010, 3:33:49 AM4/19/10
to sympy
It is enough if computer will select what to add. like

>>eq
2+3+4+5*6
>>eq.add()
9+5*6

>>eq
2*x+3*x+5*6
>>eq.add()
5*x+5*6

All I need is control over each operation (transformation).

Then I should be able to code function which will show full solution
for given math problem with explanation for each step
Like:

Use case 1

>>eq
(2*x)**2+(3*x)**2
>>show_simplify( eq )

(1) (2*x)**2+(3*x)**2

// apply pattern: (a*b)^n = a^n*b^n
(2) 4*x**2+9*x**2

// apply pattern: a*b +c*b = (a+c)*b
(2) 13*x**2


Use case 2:
Instead of printing solution I can show expression to student and give
her options:
* use pattern (a*b)^n = a^n*b^n
* use pattern a*b +c*b = (a+c)*b

To simplify work with application I don't want to ask student to
select which part of expression should be affected by this operation.
But maybe I'll change it in the future.

Mateusz Paprocki

unread,
Apr 19, 2010, 12:45:44 PM4/19/10
to sy...@googlegroups.com
Hi,

evaluate=False thing is evil (at least in the current form). On
hold branch in my github repo:

http://github.com/mattpap/sympy-polys/commits/hold

I implemented (almost) unevaluated expressions from scratch, e.g:

In [1]: hold('1 + 1 + 1*sin(x + x) + 1*2*3')
Out[1]: 1 + 1 + 1⋅sin(x + x) + 1⋅2⋅3

In [2]: type(_)
Out[2]: <class 'sympy.core.holdclasses.HoldAdd'>

In [3]: _1.unhold()
Out[3]: 8 + sin(2⋅x)

In [4]: _1.unhold(Add)
Out[4]: 2 + 1⋅sin(2⋅x) + 1⋅2⋅3

In [5]: _1.unhold(Mul)
Out[5]: 1 + 1 + sin(x + x) + 6

In [6]: _1.unhold_first()
Out[6]: 2 + 1⋅sin(x + x) + 1⋅2⋅3

(almost means that HoldAdd and HoldMul are flattened automatically).

In hold() I use AST API so currently only >= Python 2.6 interpreters
are supported. Also there are some issues with negation and printing.

To make all this useful for step-by-step rewriting unhold_first()
will have to be improved to unhold not the very first object in the
expression tree (as it does currently), but unhold all Hold* instances
on the first level with Hold* in the expression tree.

> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

--
Mateusz

signature.asc

Aaron S. Meurer

unread,
Apr 19, 2010, 12:54:02 PM4/19/10
to sy...@googlegroups.com
There is a program for the TI-89 called Symbolic Math Guide (you have to download it from the computer software update tool) that does this. It works by you selecting part of an expression and it applies a transformation on it. At each step, it does minimal simplification, requiring the user to specify it. It can also do more advanced things like symbolic integration, where the user can for example specify to try integration by parts, and the he will be asked to choose what u and dv should be (or it can fill them in automatically).

But my point is that there is an easy way to select parts of an expression in this program. The up and down arrows move up and down the expression tree (e.g., if x*y + 2 is selected, down will select just x*y), and left and right move left and right as you would expect. How would you do something like this in SymPy? It's very intuitive on the 89 where a little box surrounds the selected expression. Actually, an easy way to do this would be useful in normal use of SymPy as well.

I agree with Mateusz that non-evaluation is evil right now. We should get rid of it and completely refactor it somehow. The way it is right now, I think it is impossible to do what you want, and if you try to implement it, you will find that evaluate=False is very annoying.

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages