Ask Your Question
1

autodeclare variables

asked 2011-10-09 06:02:41 -0500

this post is marked as community wiki

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

Hello Everyone!

Is there a way to autodeclare variables?

For example:

I want to import an expression from another computer program. Now this expression is stored as a string, that means I have to evaluate it in sage.

sage_eval(expression)

It works fine, but if the expression contains variables, these have to be manually declared first as var('variables').

I want to do this automatically, e.g.: sage_eval(expression, autodeclarevariables=on)

Is there a way to do something like that?

edit retag flag offensive close merge delete

6 answers

Sort by ยป oldest newest most voted
1

answered 2012-05-28 14:13:45 -0500

this post is marked as community wiki

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

You can use symbolic_expression, which will automatically detect variables. For example,

sage: symbolic_expression('3 * y')
3*y

If you need the variables in the global namespace, you can use

map(var, symbolic_expression('3 * y').variables())
edit flag offensive delete link more

Comments

Awesome, thanks!!

nablaoperator gravatar imagenablaoperator ( 2012-06-25 03:29:45 -0500 )edit
0

answered 2012-05-28 21:10:26 -0500

this post is marked as community wiki

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

Eviatar Buch Thank you for your answer. I accept your answer.

edit flag offensive delete link more
0

answered 2011-10-10 00:10:41 -0500

this post is marked as community wiki

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

In the Sage notebook, you can do automatic_variables(True) and get variables automatically declared. See here for an example.

edit flag offensive delete link more

Comments

Unfortunately this doesn't work in the command line interface or in a standalone python program. Is there a similar command?

nablaoperator gravatar imagenablaoperator ( 2011-10-10 00:15:04 -0500 )edit

no, it only works in the notebook. You might be able to code up a way to do it based on the code for that.

Jason Grout gravatar imageJason Grout ( 2011-10-10 15:28:11 -0500 )edit
0

answered 2011-10-09 06:42:33 -0500

this post is marked as community wiki

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

Thank you for your answer.

I import it from the rather unknown program "FORM" from Jos Vermaseren (I think it is only popular among theoretical particle physicists).

I'm not sure if FORM can export its declarations.

edit flag offensive delete link more

Comments

1

Could you paste an expression for example? Perhaps it can be handled by maxima - see my edited answer.

parzan gravatar imageparzan ( 2011-10-09 06:49:08 -0500 )edit

I'm afraid Maxima cannot handle my expression. I tried your workaround, but it fails on my expression (it has for example sin(x). Anyhow want to make it as general as possible).

nablaoperator gravatar imagenablaoperator ( 2011-10-09 07:39:55 -0500 )edit

But there is sin also in the example I put there??

parzan gravatar imageparzan ( 2011-10-09 08:05:20 -0500 )edit

Ah you're right. That's strange, a simple expression worked, but sin(x) not. I'll check it out. Thank you very much for your help!

nablaoperator gravatar imagenablaoperator ( 2011-10-09 08:30:30 -0500 )edit
0

answered 2011-10-10 05:16:11 -0500

this post is marked as community wiki

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

There is also the init.sage file which you can place in your $HOME directory. You could have that with an autodeclaration, and it would run every time Sage was started.

edit flag offensive delete link more

Comments

That's nice! Thank you!

nablaoperator gravatar imagenablaoperator ( 2011-10-10 05:54:21 -0500 )edit
0

answered 2011-10-09 07:09:27 -0500

parzan gravatar image

From what program are you importing your expression? Many algebra systems have a function which lists the variables in an expression. You can use it to generate the necessary variables for you expression before evaluating it.

For example, in Maple:

a := 5*x-3*sin(y)+x*y^4+exp(z^2):
indets(a,name);
                                  {x, y, z}

You can give this output (without the braces) to sage's var and then evaluate the expression.

Also, if your expression is maxima compatible you can use maxima's command, which is listofvars, inside sage. Using sage_eval's cmds parameter you can order it to parse the expression in maxima and create the variables:

sage: a = "5*x-3*sin(y)+x*y^4+exp(z^2)"
sage: sage_eval(a,cmds="var(','.join(map(repr, maxima('listofvars(%s)'))))"%a)
x*y^4 + 5*x + e^(z^2) - 3*sin(y)

This is of course a workaround - autodeclaration would be awesome but is probably not very near (see also here).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-10-09 06:02:41 -0500

Seen: 260 times

Last updated: May 29 '12