First time here? Check out the FAQ!

Ask Your Question
1

autodeclare variables

asked 13 years ago

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?

Preview: (hide)

6 Answers

Sort by » oldest newest most voted
1

answered 12 years ago

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())
Preview: (hide)
link

Comments

Awesome, thanks!!

nablaoperator gravatar imagenablaoperator ( 12 years ago )
0

answered 13 years ago

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.

Preview: (hide)
link

Comments

That's nice! Thank you!

nablaoperator gravatar imagenablaoperator ( 13 years ago )
0

answered 13 years ago

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.

Preview: (hide)
link

Comments

1

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

parzan gravatar imageparzan ( 13 years ago )

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 ( 13 years ago )

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

parzan gravatar imageparzan ( 13 years ago )

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 ( 13 years ago )
0

answered 13 years ago

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.

Preview: (hide)
link

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 ( 13 years ago )

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 ( 13 years ago )
0

answered 12 years ago

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.

Preview: (hide)
link
0

answered 13 years ago

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

Preview: (hide)
link

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: 13 years ago

Seen: 1,587 times

Last updated: May 29 '12