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 On Wed, Jan 21, 2015 at 3:49 PM, Carsten Knoll <[email protected]> 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.. > > 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. > > -- > 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 post to this group, send email to [email protected]. > 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. > -- 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 post to this group, send email to [email protected]. 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/CAKgW%3D6%2BReQR9yzmWrWj0BNXLEQpDbz_KtNMCeO1kVNuBqO2%3DQQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
