def test_lambdify_matrix_vec_input():
x,y,z=sympy.symbols('xyz')
X=sympy.Matrix([x,y,z]) # our input vector
M=sympy.Matrix([[X[0]**2, x[0]*X[1], X[0]*X[2]],
[X[1]*X[0], X[1]**2, X[1]*X[2]],
[X[2]*X[0], X[2]*X[1], X[2]**2]])
f = lambdify(X, M, "numpy")
Xh = array([1.0, 2.0, 3.0])
expected = matrix([[Xh[0]**2, Xh[0]*Xh[1], Xh[0]*Xh[2]],
[Xh[1]*Xh[0], Xh[1]**2, Xh[1]*Xh[2]],
[Xh[2]*Xh[0], Xh[2]*Xh[1], Xh[2]**2]])
actual = f(Xh)
assert numpy.allclose(actual,expected)
Traceback (most recent call last):
File
"/home/astraw/PY/lib/python2.5/site-packages/nose-0.10.4-py2.5.egg/nose/case.py",
line 182, in runTest
self.test(*self.arg)
File
"/home/astraw/other-peoples-src/sympy/sympy.git/sympy/test_external/test_numpy.py",
line 225, in test_lambdify_matrix_vec_input
M=sympy.Matrix([[X[0]**2, x[0]*X[1], X[0]*X[2]],
TypeError: 'Symbol' object is unindexable
^^^ it's because x[0] should be X[0], doesn't it?
Otherwise I usually play with the idea in isympy, e.g.:
In [1]: X = Matrix([x,y,z])
In [2]: M = Matrix([[X[0]**2, x[0]*X[1], X[0]*X[2]],
...: [X[1]*X[0], X[1]**2, X[1]*X[2]],
...: [X[2]*X[0], X[2]*X[1], X[2]**2]])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/ondra/repos/sympy/<ipython console> in <module>()
TypeError: 'Symbol' object is unindexable
Then it reveals the problem immediatelly. However, when you enter it correctly:
In [10]: M
Out[10]:
⎡ 2 ⎤
⎢x x⋅y x⋅z⎥
⎢ ⎥
⎢ 2 ⎥
⎢x⋅y y y⋅z⎥
⎢ ⎥
⎢ 2 ⎥
⎣x⋅z y⋅z z ⎦
In [11]: f = lambdify(X, M, "numpy")
------------------------------------------------------------
File "<string>", line 1
lambda [x]
^
SyntaxError: invalid syntax
it still doesn't work, so I think our lambdify cannot handle this yet.
It think it'd be a useful to have this feature though.
Ondrej
Ahh, yes, silly mistake...
[snip]
> Then it reveals the problem immediatelly. However, when you enter it correctly:
>
>
> In [10]: M
> Out[10]:
> ⎡ 2 ⎤
> ⎢x x⋅y x⋅z⎥
> ⎢ ⎥
> ⎢ 2 ⎥
> ⎢x⋅y y y⋅z⎥
> ⎢ ⎥
> ⎢ 2 ⎥
> ⎣x⋅z y⋅z z ⎦
>
> In [11]: f = lambdify(X, M, "numpy")
> ------------------------------------------------------------
> File "<string>", line 1
> lambda [x]
> ^
> SyntaxError: invalid syntax
>
>
> it still doesn't work, so I think our lambdify cannot handle this yet.
> It think it'd be a useful to have this feature though.
Digging a bit into sympy looks like this is beyond my immediate
abilities -- Python will of course evaluate X[0] in the Matrix
definition above to x (for the case above). So, I think there'd need to
be a way to tell sympy "element zero of vector X" without having it
dereferenced. Thus, rather than __getattr__ or the equivalent, something
like X.get_component(0), which would return some kind of deferred
reference to component zero of X.
x,y,z = symbols('xyz')
X=Matrix([x,y,z])
M=Matrix([[X.get_component(0)**2, ...
Or, perhaps a "DeferredMatrix" that implements this automatically?
X=DeferredMatrix()
M=Matrix([[X[0]**2, ...
> Or, perhaps a "DeferredMatrix" that implements this automatically?
>
> X=DeferredMatrix()
> M=Matrix([[X[0]**2, ...
I sent a patch to sympy-patches which implements this (well, it's called
"DeferredVector", actually). I'm interested in feedback on the patch in
the hopes that it might get into sympy.
Strangely, although I sent if a couple hours ago my patch doesn't (yet)
show up on the sympy-patches archive, but perhaps that's because I
wasn't subscribed. If it doesn't show up in a day or two, I'll re-send.
-Andrew
--
Dr. Andrew D. Straw
California Institute of Technology
http://www.its.caltech.edu/~astraw/
I think you need to join the group (due to spam). If it's just one
patch, attach it to some issue, that should work. I'll have a look at
it.
Thanks,
Ondrej