lambdify taking vector input

445 views
Skip to first unread message

Andrew Straw

unread,
Jan 17, 2009, 2:09:00 PM1/17/09
to sy...@googlegroups.com
Hi, I'm trying to get lambdify to take a vector as input so that I can
have a function G(x) where x is a vector. (The ultimate aim is to
evaluate linearizations at certain locations where the linearized
function is defined as a sympy Matrix.) Attempting to modify
sympy/test_external/test_numpy.py to include the test case results in
the traceback below. Should I expect this to work? If so, what am I
doing wrong?

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

Ondrej Certik

unread,
Jan 17, 2009, 2:15:39 PM1/17/09
to sy...@googlegroups.com
On Sat, Jan 17, 2009 at 11:09 AM, Andrew Straw <ast...@caltech.edu> wrote:
>
> Hi, I'm trying to get lambdify to take a vector as input so that I can
> have a function G(x) where x is a vector. (The ultimate aim is to
> evaluate linearizations at certain locations where the linearized
> function is defined as a sympy Matrix.) Attempting to modify
> sympy/test_external/test_numpy.py to include the test case results in
> the traceback below. Should I expect this to work? If so, what am I
> doing wrong?
>
> 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]],

^^^ 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

Andrew Straw

unread,
Jan 17, 2009, 3:10:44 PM1/17/09
to sy...@googlegroups.com
Ondrej Certik wrote:
> On Sat, Jan 17, 2009 at 11:09 AM, Andrew Straw <ast...@caltech.edu> wrote:
>> Hi, I'm trying to get lambdify to take a vector as input so that I can
>> have a function G(x) where x is a vector. (The ultimate aim is to
>> evaluate linearizations at certain locations where the linearized
>> function is defined as a sympy Matrix.) Attempting to modify
>> sympy/test_external/test_numpy.py to include the test case results in
>> the traceback below. Should I expect this to work? If so, what am I
>> doing wrong?
>>
>> 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]],
>
> ^^^ it's because x[0] should be X[0], doesn't it?

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, ...

Andrew Straw

unread,
Jan 17, 2009, 7:37:13 PM1/17/09
to sy...@googlegroups.com
Andrew Straw wrote:
> Ondrej Certik wrote:
>> On Sat, Jan 17, 2009 at 11:09 AM, Andrew Straw <ast...@caltech.edu> wrote:
>>> Hi, I'm trying to get lambdify to take a vector as input so that I can
>>> have a function G(x) where x is a vector. (The ultimate aim is to

> 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/

Ondrej Certik

unread,
Jan 17, 2009, 10:03:20 PM1/17/09
to sy...@googlegroups.com
> 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.

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

Reply all
Reply to author
Forward
0 new messages