Ask Your Question
0

How do I exec a function of N variables.

asked 2012-08-21 14:49:37 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

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/9pi) + 1/2 dfx(pi/6,pi/9) -> 1/2sqrt(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?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2012-08-21 16:10:40 +0100

DSM gravatar image

updated 2012-08-21 16:11:43 +0100

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

edit flag offensive delete link more

Comments

Nice answer thanks.

oldbrad gravatar imageoldbrad ( 2012-08-22 15:52:00 +0100 )edit

If you like it, you can "accept" it so that DSM "gets credit" for this :)

kcrisman gravatar imagekcrisman ( 2012-08-24 16:38:19 +0100 )edit

Can't quite see how to "accept". Is it a thumbs up or a tick or something else?

oldbrad gravatar imageoldbrad ( 2012-08-27 10:44:12 +0100 )edit

I guess you figured it out :)

kcrisman gravatar imagekcrisman ( 2012-08-27 10:46:51 +0100 )edit
0

answered 2012-08-22 15:56:11 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Is an embedded exec any more insecure than a live session in which anyone could just write one?

edit flag offensive delete link more

Comments

Nope, 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..

DSM gravatar imageDSM ( 2012-08-22 16:31:15 +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: 2012-08-21 14:49:37 +0100

Seen: 952 times

Last updated: Aug 22 '12