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, 27 Aug 2012 10:46:51 +0200How do I exec a function of N variables.https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/The python code:
f(x,y)=sin(x) + cos(y) Can be both differentiated and evaluated.
like this:
dfx=diff(f,x);
dfy=diff(f,y)
then evaluated like this:
f(pi/6,pi/9) -> cos(1/9*pi) + 1/2
dfx(pi/6,pi/9) -> 1/2*sqrt(3)
I'm wanting to do something like:
exec("f(t1,t2)=cos(t1)+sin(t2)")
so that I can write an @interact function to define the equations of N variables interactively. When I try the above exec I receive the error: "SyntaxError: can't assign to function call"
I can almost do it, but I'm getting into a horrible inelegant tangle of of eval statements in the process!
Is there a clean way to do it with sage?Tue, 21 Aug 2012 14:49:37 +0200https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/Answer by oldbrad for <p>The python code:
f(x,y)=sin(x) + cos(y) Can be both differentiated and evaluated.</p>
<p>like this:
dfx=diff(f,x);
dfy=diff(f,y)
then evaluated like this:
f(pi/6,pi/9) -> cos(1/9<em>pi) + 1/2
dfx(pi/6,pi/9) -> 1/2</em>sqrt(3) </p>
<p>I'm wanting to do something like:
exec("f(t1,t2)=cos(t1)+sin(t2)")
so that I can write an @interact function to define the equations of N variables interactively. When I try the above exec I receive the error: "SyntaxError: can't assign to function call"</p>
<p>I can almost do it, but I'm getting into a horrible inelegant tangle of of eval statements in the process!</p>
<p>Is there a clean way to do it with sage?</p>
https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?answer=13955#post-id-13955Is an embedded exec any more insecure than a live session in which anyone could just write one?Wed, 22 Aug 2012 15:56:11 +0200https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?answer=13955#post-id-13955Comment by DSM for <p>Is an embedded exec any more insecure than a live session in which anyone could just write one?</p>
https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19194#post-id-19194Nope, once you've given someone the ability to write arbitrary code, they can do anything in the box, exec or otherwise. Hence my preference for interacts which only have sliders and such..Wed, 22 Aug 2012 16:31:15 +0200https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19194#post-id-19194Answer by DSM for <p>The python code:
f(x,y)=sin(x) + cos(y) Can be both differentiated and evaluated.</p>
<p>like this:
dfx=diff(f,x);
dfy=diff(f,y)
then evaluated like this:
f(pi/6,pi/9) -> cos(1/9<em>pi) + 1/2
dfx(pi/6,pi/9) -> 1/2</em>sqrt(3) </p>
<p>I'm wanting to do something like:
exec("f(t1,t2)=cos(t1)+sin(t2)")
so that I can write an @interact function to define the equations of N variables interactively. When I try the above exec I receive the error: "SyntaxError: can't assign to function call"</p>
<p>I can almost do it, but I'm getting into a horrible inelegant tangle of of eval statements in the process!</p>
<p>Is there a clean way to do it with sage?</p>
https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?answer=13952#post-id-13952Usually needing to use `exec` is a sign something's wrong with a design, but I don't use @interacts much so maybe it's the right way here. It's definitely a little dangerous for remote use, though: if you give me access to this interact over the net I can probably delete all your files.
The problem is that you're using Sage-specific syntax handles by the Sage prepareser and `exec` doesn't know what to do with it. You can work around that in a few ways, say by calling the preparser specifically:
sage: reset()
sage: s = "f(t1, t2) = cos(t1) + sin(t2)"
sage: exec(s)
------------------------------------------------------------
File "<string>", line 1
SyntaxError: can't assign to function call (<string>, line 1)
sage: preparse(s)
'__tmp__=var("t1,t2"); f = symbolic_expression(cos(t1) + sin(t2)).function(t1,t2)'
sage: exec(preparse(s))
sage: f
(t1, t2) |--> sin(t2) + cos(t1)
or by using the alternate function syntax:
sage: exec("g = (cos(t1)+sin(t2)).function(t1, t2)")
sage: g
(t1, t2) |--> sin(t2) + cos(t1)
where you'd have to make sure the variables were defined somewhere.
Tue, 21 Aug 2012 16:10:40 +0200https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?answer=13952#post-id-13952Comment by kcrisman for <p>Usually needing to use <code>exec</code> is a sign something's wrong with a design, but I don't use @interacts much so maybe it's the right way here. It's definitely a little dangerous for remote use, though: if you give me access to this interact over the net I can probably delete all your files.</p>
<p>The problem is that you're using Sage-specific syntax handles by the Sage prepareser and <code>exec</code> doesn't know what to do with it. You can work around that in a few ways, say by calling the preparser specifically:</p>
<pre><code>sage: reset()
sage: s = "f(t1, t2) = cos(t1) + sin(t2)"
sage: exec(s)
------------------------------------------------------------
File "<string>", line 1
SyntaxError: can't assign to function call (<string>, line 1)
sage: preparse(s)
'__tmp__=var("t1,t2"); f = symbolic_expression(cos(t1) + sin(t2)).function(t1,t2)'
sage: exec(preparse(s))
sage: f
(t1, t2) |--> sin(t2) + cos(t1)
</code></pre>
<p>or by using the alternate function syntax:</p>
<pre><code>sage: exec("g = (cos(t1)+sin(t2)).function(t1, t2)")
sage: g
(t1, t2) |--> sin(t2) + cos(t1)
</code></pre>
<p>where you'd have to make sure the variables were defined somewhere.</p>
https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19182#post-id-19182If you like it, you can "accept" it so that DSM "gets credit" for this :)Fri, 24 Aug 2012 16:38:19 +0200https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19182#post-id-19182Comment by oldbrad for <p>Usually needing to use <code>exec</code> is a sign something's wrong with a design, but I don't use @interacts much so maybe it's the right way here. It's definitely a little dangerous for remote use, though: if you give me access to this interact over the net I can probably delete all your files.</p>
<p>The problem is that you're using Sage-specific syntax handles by the Sage prepareser and <code>exec</code> doesn't know what to do with it. You can work around that in a few ways, say by calling the preparser specifically:</p>
<pre><code>sage: reset()
sage: s = "f(t1, t2) = cos(t1) + sin(t2)"
sage: exec(s)
------------------------------------------------------------
File "<string>", line 1
SyntaxError: can't assign to function call (<string>, line 1)
sage: preparse(s)
'__tmp__=var("t1,t2"); f = symbolic_expression(cos(t1) + sin(t2)).function(t1,t2)'
sage: exec(preparse(s))
sage: f
(t1, t2) |--> sin(t2) + cos(t1)
</code></pre>
<p>or by using the alternate function syntax:</p>
<pre><code>sage: exec("g = (cos(t1)+sin(t2)).function(t1, t2)")
sage: g
(t1, t2) |--> sin(t2) + cos(t1)
</code></pre>
<p>where you'd have to make sure the variables were defined somewhere.</p>
https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19166#post-id-19166Can't quite see how to "accept". Is it a thumbs up or a tick or something else?Mon, 27 Aug 2012 10:44:12 +0200https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19166#post-id-19166Comment by kcrisman for <p>Usually needing to use <code>exec</code> is a sign something's wrong with a design, but I don't use @interacts much so maybe it's the right way here. It's definitely a little dangerous for remote use, though: if you give me access to this interact over the net I can probably delete all your files.</p>
<p>The problem is that you're using Sage-specific syntax handles by the Sage prepareser and <code>exec</code> doesn't know what to do with it. You can work around that in a few ways, say by calling the preparser specifically:</p>
<pre><code>sage: reset()
sage: s = "f(t1, t2) = cos(t1) + sin(t2)"
sage: exec(s)
------------------------------------------------------------
File "<string>", line 1
SyntaxError: can't assign to function call (<string>, line 1)
sage: preparse(s)
'__tmp__=var("t1,t2"); f = symbolic_expression(cos(t1) + sin(t2)).function(t1,t2)'
sage: exec(preparse(s))
sage: f
(t1, t2) |--> sin(t2) + cos(t1)
</code></pre>
<p>or by using the alternate function syntax:</p>
<pre><code>sage: exec("g = (cos(t1)+sin(t2)).function(t1, t2)")
sage: g
(t1, t2) |--> sin(t2) + cos(t1)
</code></pre>
<p>where you'd have to make sure the variables were defined somewhere.</p>
https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19164#post-id-19164I guess you figured it out :)Mon, 27 Aug 2012 10:46:51 +0200https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19164#post-id-19164Comment by oldbrad for <p>Usually needing to use <code>exec</code> is a sign something's wrong with a design, but I don't use @interacts much so maybe it's the right way here. It's definitely a little dangerous for remote use, though: if you give me access to this interact over the net I can probably delete all your files.</p>
<p>The problem is that you're using Sage-specific syntax handles by the Sage prepareser and <code>exec</code> doesn't know what to do with it. You can work around that in a few ways, say by calling the preparser specifically:</p>
<pre><code>sage: reset()
sage: s = "f(t1, t2) = cos(t1) + sin(t2)"
sage: exec(s)
------------------------------------------------------------
File "<string>", line 1
SyntaxError: can't assign to function call (<string>, line 1)
sage: preparse(s)
'__tmp__=var("t1,t2"); f = symbolic_expression(cos(t1) + sin(t2)).function(t1,t2)'
sage: exec(preparse(s))
sage: f
(t1, t2) |--> sin(t2) + cos(t1)
</code></pre>
<p>or by using the alternate function syntax:</p>
<pre><code>sage: exec("g = (cos(t1)+sin(t2)).function(t1, t2)")
sage: g
(t1, t2) |--> sin(t2) + cos(t1)
</code></pre>
<p>where you'd have to make sure the variables were defined somewhere.</p>
https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19195#post-id-19195Nice answer thanks.Wed, 22 Aug 2012 15:52:00 +0200https://ask.sagemath.org/question/9260/how-do-i-exec-a-function-of-n-variables/?comment=19195#post-id-19195