Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2012-08-21 09:10:40 -0500

DSM gravatar image

Usually 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.

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.

Usually 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.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.