How to substitute var by arrays?

36 views
Skip to first unread message

Ronghai Wu

unread,
Sep 21, 2015, 2:14:33 PM9/21/15
to sympy
Dear Sympy developers and users,

Could anyone help me with the following problem:
substitute a, b, c by arrays in expression f = a*b*c. For example, I get "ValueError: The truth value of an array with more than one element is ambiguous" if running:
import sympy
import numpy
sympy.var('a,b,c')
f = a*b*c
array1 = numpy.random.randint(1,3,3)
array2 = numpy.random.randint(1,3,3)
array3 = numpy.random.randint(1,3,3)
f.subs([[a, array1], [b, array2], [c, array3]])

Best Regards
Ronghai Wu

Aaron Meurer

unread,
Sep 21, 2015, 2:16:20 PM9/21/15
to sy...@googlegroups.com
subs should only be used to replace sympy expressions with sympy
expressions. To evaluate a sympy expression at a numpy array, you
should lambdify() it first.

In [10]: f0 = lambdify((a, b, c), f)

In [12]: f0(array1, array2, array3)
Out[12]: array([2, 1, 1])

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/89d85fa0-8151-40da-ab0c-6d6f6d34a66e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ronghai Wu

unread,
Sep 22, 2015, 4:34:52 AM9/22/15
to sympy
Hi Aaron Meurer,

Thanks, it works!

Ronghai

在 2015年9月21日星期一 UTC+2下午8:16:20,Aaron Meurer写道:
Reply all
Reply to author
Forward
0 new messages