Thank you so much for your comments! They were really helpful but also
leave me with some more questions. For example, say you're given an
expression
>>> expr = A*x**2 + B*x + C
and you are also given arrays of values
>>> A = np.array([...])
>>> B = np.array([...])
>>> C = np.array([...])
but no values for x. Thus x is unknown and you should just get an
array of polynomials in x. Is there any way to handle this with
lambdify? I run into problems if I only try to halfway evaluate an
expression.
Here's an example
In [9]: from numpy import newaxis as NAX
In [10]: A,B,C,x = sp.symbols('ABCx')
In [11]: AA,BB,CC = np.linspace(1,10,5),np.linspace(1,10,5),np.linspace
(1,10,5)
In [12]: expr = A*x**2 + B*x + C
In [13]: eval_expr = sp.lambdify((A,B,C),expr,modules="numpy")
In [14]: np_eval = eval_expr(AA[:,NAX,NAX],BB[NAX,:,NAX],CC
[NAX,NAX,:])
---------------------------------------------------------------------------
NameError Traceback (most recent call
last)
C:\Documents and Settings\wflynn\My Documents\Python\<ipython console>
in <module>()
C:\Documents and Settings\wflynn\My Documents\Python\<string> in
<lambda>(A, B,
C)
NameError: global name 'x' is not defined
In [9]: from numpy import newaxis as NAX
In [10]: A,B,C,x = sp.symbols('ABCx')
In [11]: AA,BB,CC = np.linspace(1,10,5),np.linspace(1,10,5),np.linspace
(1,10,5)
In [12]: expr = A*x**2 + B*x + C
...
In [17]: eval_expr2 = sp.lambdify((A,B,C,x),expr,modules="numpy")
In [18]: np_eval = eval_expr2(AA[:,NAX,NAX],BB[NAX,:,NAX],CC
[NAX,NAX,:],sp.Symbol('x'))
In [22]: np_eval[0][0]
Out[22]:
array([1.0 + x + x**2, 3.25 + x + x**2, 5.5 + x + x**2, 7.75 + x +
x**2,
10.0 + x + x**2], dtype=object)
Thank you so much for your help guys!
Bill