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 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.
Hi Mateusz,
True, but the dtype of this numpy array is object, which is unsuitable
e.g. for numerical ODE solver.
Try:
In [1] : a = Matrix([[1, 2], [3, 4]])
In [2]: type(a.tolist()[0][0])
Out[13]: <class 'sympy.core.numbers.One'>
same with the numpy array:
array([[1, 2],
[3, 4]], dtype=object)
I didn't find an issue for it. It should be very easy to fix. We
just need to define __long__ on Number, which is similar to __int__
except it casts the result to a long first.
>
> Thank you very much for your help so far. I don't know if this is in
> the scope of sympy, but it would be nice to have a nice interface to
> numpy at this point.
This is within the scope to some degree. This is why we have
functions like lambdify() for example.
Aaron Meurer
On Tue, Nov 1, 2011 at 1:27 PM, a.lwtzky <a.lw...@googlemail.com> wrote:I didn't find an issue for it. It should be very easy to fix. We
> Hi Mateusz,
>
> the second solution is exactly what I was looking for.
> Actually, I tried that as well but ran in the same type error.
> Unfortunately I only tried it with integers, so I gave up hope that it
> works like this.
> Anyway, what's still confusing me is:
>>>> a = Matrix([[1., 2.], [3., 4.]])
>>>> b = np.array(a, dtype=float)
> ---------------------------------------------------------------------------
> TypeError Traceback (most recent call
> last)
>
> /home/andy/<ipython console> in <module>()
>
> TypeError: __array__() takes exactly 1 argument (2 given)
>
> I expected it to do exactly the same as array()&astype(), but that's
> obviously wrong. It would be awesome, if it works like that.
>
> Is the bug with the Integers in sympy known? Or should I file a bug
> report for this?
just need to define __long__ on Number, which is similar to __int__
except it casts the result to a long first.
Well, it's happened several times now that some simple thing like this
doesn't work because we don't have some __method__ from
http://docs.python.org/reference/datamodel.html implemented in Basic,
Expr, or whatever relevant subclass. So I created
http://code.google.com/p/sympy/issues/detail?id=2817 for this.
I marked it for Code-In, but if you want to fix it, Andy, that would
also be great.
Aaron Meurer