sympifying iterables to Matrix?

13 views
Skip to first unread message

krastano...@gmail.com

unread,
May 12, 2012, 1:17:03 PM5/12/12
to sy...@googlegroups.com
If we do this it would be possible to write stuff like `eye(2)*[x, y]`
and it will work.

In many places we have `args = sympify(args)` where args is a list or
a tuple. This will work in most cases (as Matrix has the necessary
interfaces), but it will be slower. It should be changed to
`map(sympify, args)`.

Or it can at least be done with a keyword argument `iter_to_matrix`?

Vinzent Steinberg

unread,
May 16, 2012, 3:01:08 PM5/16/12
to sy...@googlegroups.com
Am Samstag, 12. Mai 2012 19:17:03 UTC+2 schrieb Stefan Krastanov:
If we do this it would be possible to write stuff like `eye(2)*[x, y]`
and it will work.

This could be implemented in Matrix.__mul__ and __rmul__. However, I'm not really sure it's a good idea.

In numpy we have this behavior:

>>> from numpy import *
>>> a = matrix([[1,2],[3,4]])
>>> a*[1,-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 330, in __mul__
    return N.dot(self, asmatrix(other))
ValueError: objects are not aligned
>>> [1,-1]*a
matrix([[-2, -2]])
>>> a = array([[1,2],[3,4]])
>>> [1,-1]*a
array([[ 1, -2],
       [ 3, -4]])
>>> a*[1,-1]
array([[ 1, -2],
       [ 3, -4]])

So it treats [1, -1] as a row vector.

Vinzent

krastano...@gmail.com

unread,
May 16, 2012, 4:44:59 PM5/16/12
to sy...@googlegroups.com
On 16 May 2012 21:01, Vinzent Steinberg
However, Matrix([1, -1]) returns a column vector and it would be nice
to imitate the constructor.
Reply all
Reply to author
Forward
0 new messages