ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 17 Oct 2011 14:05:30 +0200Using symbolic expressions with numpy arrayshttps://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/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.Sun, 16 Oct 2011 07:39:28 +0200https://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/Answer by benjaminfjones for <p>Assume I have some symbolic function like</p>
<pre><code>f(x,y,z)=x^2*sin(y)*z
</code></pre>
<p>Now I want to evaluate it numerically using numpy arrays, like</p>
<pre><code>import numpy as np
X=np.linspace(-1,1,10)
Y,Z=X,X
f(X,Y,Z)
</code></pre>
<p>That produces an error</p>
<pre><code>blabla...
NotImplementedError: Numpy arrays are not supported as arguments for
symbolic expressions
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/?answer=12769#post-id-12769You 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.Sun, 16 Oct 2011 16:34:04 +0200https://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/?answer=12769#post-id-12769Comment by jk for <p>You can try using the <code>fast_callable</code> 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) <code>sin(x)</code> where the input <code>x</code> is a float.</p>
<p>Try:</p>
<pre><code>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])
</code></pre>
<p>My first thought was to use <code>fast_float</code> instead, but that doesn't seem to work. Maybe someone more knowledgeable can point out why.</p>
https://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/?comment=21116#post-id-21116First, 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.Mon, 17 Oct 2011 13:57:28 +0200https://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/?comment=21116#post-id-21116Comment by kcrisman for <p>You can try using the <code>fast_callable</code> 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) <code>sin(x)</code> where the input <code>x</code> is a float.</p>
<p>Try:</p>
<pre><code>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])
</code></pre>
<p>My first thought was to use <code>fast_float</code> instead, but that doesn't seem to work. Maybe someone more knowledgeable can point out why.</p>
https://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/?comment=21115#post-id-21115See 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.Mon, 17 Oct 2011 14:05:30 +0200https://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/?comment=21115#post-id-21115Comment by Xaver for <p>You can try using the <code>fast_callable</code> 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) <code>sin(x)</code> where the input <code>x</code> is a float.</p>
<p>Try:</p>
<pre><code>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])
</code></pre>
<p>My first thought was to use <code>fast_float</code> instead, but that doesn't seem to work. Maybe someone more knowledgeable can point out why.</p>
https://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/?comment=21120#post-id-21120a) 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?Sun, 16 Oct 2011 17:53:23 +0200https://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/?comment=21120#post-id-21120