Ask Your Question
1

Using symbolic expressions with numpy arrays

asked 2011-10-16 07:39:28 +0100

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2011-10-16 16:34:04 +0100

benjaminfjones gravatar image

updated 2011-10-16 16:34:42 +0100

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.

edit flag offensive delete link more

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 ( 2011-10-16 17:53:23 +0100 )edit

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 ( 2011-10-17 13:57:28 +0100 )edit

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 ( 2011-10-17 14:05:30 +0100 )edit

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: 2011-10-16 07:39:28 +0100

Seen: 4,623 times

Last updated: Oct 16 '11