return types of solve?

17 views
Skip to first unread message

Carsten Knoll

unread,
Jan 21, 2015, 4:49:28 PM1/21/15
to sy...@googlegroups.com
I try to include sympy in teaching material.

When doing this, the focus is not on programming nor implementation
details. The CAS should be as "transparent" as possible. The CAS-using
code should read like the math done in a textbook.

I think sympy is very close to this. However, there are some annoying
obstacles to transparency.

One of it is the return behavior of solve. There are at least three
cases (if a solution can be found):

* a dict
* a list of tuples
* a list of dicts

for me it is hard to predict, when which case will occur. The behavior
can be influenced by keyword args but this would require additional
(off-topic) explanation to the students..

So, I am thinking in implementing my own wrapper for solve to overcome
this. However, I from my point of view it would be a lot cleaner if the
default return type (in the case of a unique solution) is always one
dict. Because then the solution can immediatly passed to subs(...).

Question: Is there any chance to change the solve behavior like that? (I
would try to work on it.)


See an example of what I mean (copied from an IP notebook):


In [1]:

import sympy as sp

print sp.__version__

0.7.5

In [2]:

x1, x2 = xx = sp.Matrix( sp.symbols("x1:3") )

z1, z2 = zz = sp.Matrix( sp.symbols("z1:3") )

In [3]:

q = sp.Matrix([x1, -x2 + x1*(1-x1**2)])

q

Out[3]:

Matrix([
[ x1],
[x1*(-x1**2 + 1) - x2]])

Say z:=q(x). We want to calculate x=q^{−1}(z). We use 0=z−q(x) as
expression to solve.
In [4]:

sp.solve(q-zz, xx) # returns a list (len=1) of tuples (len=2) of
expressions

Out[4]:

[(z1, -z1**3 + z1 - z2)]

In [5]:

# This is what I finally want to do:

# q_inv = xx.subs(x_sol)



# but this requires some overhead

sp.solve(q-zz, xx, dict=True) # returns a list (len=1) of dicts

Out[5]:

[{x1: z1, x2: -z1**3 + z1 - z2}]

In [6]:

x_sol = sp.solve(q-zz, xx, dict=True)[0]

q_inv = xx.subs(x_sol)

q_inv # this is the result I want (but with less 'magic' overhead)

Out[6]:

Matrix([
[ z1],
[-z1**3 + z1 - z2]])

In [7]:

# on the other hand, solve can behave like 'expected' (by me):


sp.solve(q-zz, zz) # returns one dict (good)

Out[7]:

{z2: -x1**3 + x1 - x2, z1: x1}


Best regards,
Carsten.

Amit Saha

unread,
Jan 21, 2015, 4:56:14 PM1/21/15
to sy...@googlegroups.com
On Thu, Jan 22, 2015 at 7:49 AM, Carsten Knoll <Carste...@gmx.de> wrote:
> I try to include sympy in teaching material.
>
> When doing this, the focus is not on programming nor implementation
> details. The CAS should be as "transparent" as possible. The CAS-using
> code should read like the math done in a textbook.
>
> I think sympy is very close to this. However, there are some annoying
> obstacles to transparency.
>
> One of it is the return behavior of solve. There are at least three
> cases (if a solution can be found):
>
> * a dict
> * a list of tuples
> * a list of dicts
>
> for me it is hard to predict, when which case will occur. The behavior
> can be influenced by keyword args but this would require additional
> (off-topic) explanation to the students..

I often face the same problem when I am working on my book. What I do
is just stick to one approach for the chapter/book. So, for example,
for this case, I just pass 'dict=True' all the time. It requires me to
explain what a Python dictionary is, but I don't think you can
*really* avoid getting into some Python while teaching with SymPy.

Aaron Meurer

unread,
Jan 21, 2015, 6:32:36 PM1/21/15
to sy...@googlegroups.com
It's a pretty well known issue with solve. dict=True is a decent workaround. You can also take a look at the new solveset (I don't remember if this was merged before the 0.7.6 release), which tries to make the return type of solve always be a Set object (this also will allow solve to return infinitely many solutions).

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 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/54C01EE2.3040206%40gmx.de.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages