Hi,

On 1 November 2011 09:36, a.lwtzky <[email protected]> wrote:

> Dear everyone,
>
> I was wondering if there is a function in sympy that converts a
> sympy.Matrix to a list of lists of python standard types. For example
> if you have
> >>> m = matrices.Matrix([[2,0],[0,2]])
>
> it would be nice to have a function <f> that returns:
> >>> res = <f>(m)
> [[2,0],[0,2]]
> >>> type(res)
> list
> >>> type(res[0][0])
> int # or float or whatever seems appropriate.
>
> as an alternative: return a 2D numpy array of integers/floats... But
> this brings probably unnecessary dependencies to numpy. And if the
> user really wants to have a numpy.array, he/she could just use
> np.asarray(res).
>

You can create an array from a matrix and convert a matrix to a list of
lists, e.g.:

In [1]: import numpy as np

In [2]: a = Matrix([[1, 2], [3, 4]])

In [3]: a
Out[3]:
⎡1  2⎤
⎢    ⎥
⎣3  4⎦

In [4]: a.tolist()
Out[4]: [[1, 2], [3, 4]]

In [5]: np.array(a)
Out[5]:
 [[1 2]
 [3 4]]

This is for git version of SymPy, but should work for older versions too.


>
> I spent a couple of hours in order to find a (simple) solution for
> this.
> A similar idea was presented here:
>
> http://weekinpse.wordpress.com/2010/01/06/how-to-convert-a-sympy-matrix-to-numpy-array/
> This subject has already been discussed in sympy IRC channel with
> ronan (thanks again).
>
> -> Motivation - Use case
> I would like to use numpy and sympy in the same project. Use sympy to
> solve a ODE system symbolically, get its jacobian, the jacobian's
> eigenvectors at a critical point and so on. Then use this information
> to plot it (with matplotlib) together with other functions, further
> investigate it's properties (for example integrate it numerically with
> numpy - plot the trajectories) and so on.
>
> -> suggestions
> It doesn't seem to be too bad implementing something like this. The
> solution of hdahlol can be found at the link (see above).
> ronan thought about something like:
> def <f>(m):
>    arr = np.asarray(map(int, m.mat)) # or float...
>    arr.shape = m.shape
>    return arr
>
> But both of us agreed that using m.mat is pretty ugly at this point.
> And it does explicitly take use of numpy. Of course there is a way to
> copy value by value - but this might result in terribly slow code
> without benefit.
>
> In case a value can't be converted to standard types (for example a
> variable x) the function could just throw an exception or leave the
> sympy.object in the list and let the user care about this case.
>
> I would really appreciate help in this question.
>
> Thanks, Andy
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>
>
Mateusz

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

Reply via email to