Processing math: 100%
Ask Your Question
1

Using symbolic expressions with numpy arrays

asked 13 years ago

Xaver gravatar image

Assume I have some symbolic function like

f(x,y,z)=x^2*sin(y)*z

Now I want to evaluate it numerically using numpy arrays, like

import numpy as np
X=np.linspace(-1,1,10)
Y,Z=X,X
f(X,Y,Z)

That produces an error

blabla...
NotImplementedError: Numpy arrays are not supported as arguments for
symbolic expressions

Can I convert a symbolic function into a python functions, so that I can us numpy with it? I fear there surely is a documented way to do to this somewhere - but I cannot find it.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 13 years ago

benjaminfjones gravatar image

updated 13 years ago

You can try using the fast_callable function which converts a symbolic function to a python (or Cython? or C?) function that takes advantage of the fast routines for compute (for example) sin(x) where the input x is a float.

Try:

sage: x,y,z = var('x,y,z')
sage: import numpy as np
sage: X=np.linspace(-1,1,10)
sage: Y,Z=X,X
sage: f(x,y,z)=x^2*sin(y)*z
sage: ff = fast_callable(f, vars=[x,y,z])
sage: ff(1,2,3)
3*sin(2)
sage: ff(X,Y,Z)
array([  8.41470985e-01,   3.30154145e-01,   9.04347369e-02,
         1.21183221e-02,   1.52102371e-04,   1.52102371e-04,
         1.21183221e-02,   9.04347369e-02,   3.30154145e-01,
         8.41470985e-01])

My first thought was to use fast_float instead, but that doesn't seem to work. Maybe someone more knowledgeable can point out why.

Preview: (hide)
link

Comments

a) thanks! b) from your solution I went to the docs and I have to admit that the reason for not finding this answer was that I neither do understand what an ExpressionTree(Builder) is in the first place, nor why it seems to relate to the variables of the symbolic expression. Would you mind to give some more explanation or point me to a place in the doc where I could get the necessary background?

Xaver gravatar imageXaver ( 13 years ago )

First, I agree with @Xaver that some more end-user accessible documentation would be very helpful. Google only found [this](http://www.sagemath.org/doc/reference/sage/symbolic/expression_conversions.html), which does not appear to be a good starting point. Second, I'm not sure, whether this is the solution to OP's question. f(x,y,z) is a function of three independent variables, so the general numeric result should be a NxNxN matrix.

jk gravatar imagejk ( 13 years ago )

See also http://trac.sagemath.org/sage_trac/ticket/5572 where fast_callable is (sort of) being worked on. This is another case of not fixing doc because there is an upgrade int the works... for years ;-) which is no offense to Jason or the others working on it, because it's a huge bit of work.

kcrisman gravatar imagekcrisman ( 13 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 13 years ago

Seen: 4,755 times

Last updated: Oct 16 '11