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, 25 Jun 2012 10:29:45 +0200autodeclare variableshttps://ask.sagemath.org/question/8363/autodeclare-variables/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?
Sun, 09 Oct 2011 13:02:41 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/Answer by kcrisman for <p>Hello Everyone!</p>
<p>Is there a way to autodeclare variables?</p>
<p>For example:</p>
<p>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.</p>
<p>sage_eval(expression)</p>
<p>It works fine, but if the expression contains variables, these have to be manually declared first as var('variables').</p>
<p>I want to do this automatically, e.g.: sage_eval(expression, autodeclarevariables=on)</p>
<p>Is there a way to do something like that?</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=12744#post-id-12744There 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.Mon, 10 Oct 2011 12:16:11 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=12744#post-id-12744Comment by nablaoperator for <p>There is also the <code>init.sage</code> file which you can place in your <code>$HOME</code> directory. You could have that with an autodeclaration, and it would run every time Sage was started.</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21144#post-id-21144That's nice! Thank you!Mon, 10 Oct 2011 12:54:21 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21144#post-id-21144Answer by nablaoperator for <p>Hello Everyone!</p>
<p>Is there a way to autodeclare variables?</p>
<p>For example:</p>
<p>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.</p>
<p>sage_eval(expression)</p>
<p>It works fine, but if the expression contains variables, these have to be manually declared first as var('variables').</p>
<p>I want to do this automatically, e.g.: sage_eval(expression, autodeclarevariables=on)</p>
<p>Is there a way to do something like that?</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=12733#post-id-12733Thank 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.Sun, 09 Oct 2011 13:42:33 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=12733#post-id-12733Comment by parzan for <p>Thank you for your answer.</p>
<p>I import it from the rather unknown program "FORM" from Jos Vermaseren (I think it is only popular among theoretical particle physicists).</p>
<p>I'm not sure if FORM can export its declarations.</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21160#post-id-21160But there is sin also in the example I put there??Sun, 09 Oct 2011 15:05:20 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21160#post-id-21160Comment by nablaoperator for <p>Thank you for your answer.</p>
<p>I import it from the rather unknown program "FORM" from Jos Vermaseren (I think it is only popular among theoretical particle physicists).</p>
<p>I'm not sure if FORM can export its declarations.</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21161#post-id-21161I'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). Sun, 09 Oct 2011 14:39:55 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21161#post-id-21161Comment by parzan for <p>Thank you for your answer.</p>
<p>I import it from the rather unknown program "FORM" from Jos Vermaseren (I think it is only popular among theoretical particle physicists).</p>
<p>I'm not sure if FORM can export its declarations.</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21162#post-id-21162Could you paste an expression for example? Perhaps it can be handled by maxima - see my edited answer.Sun, 09 Oct 2011 13:49:08 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21162#post-id-21162Comment by nablaoperator for <p>Thank you for your answer.</p>
<p>I import it from the rather unknown program "FORM" from Jos Vermaseren (I think it is only popular among theoretical particle physicists).</p>
<p>I'm not sure if FORM can export its declarations.</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21159#post-id-21159Ah 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!Sun, 09 Oct 2011 15:30:30 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21159#post-id-21159Answer by Jason Grout for <p>Hello Everyone!</p>
<p>Is there a way to autodeclare variables?</p>
<p>For example:</p>
<p>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.</p>
<p>sage_eval(expression)</p>
<p>It works fine, but if the expression contains variables, these have to be manually declared first as var('variables').</p>
<p>I want to do this automatically, e.g.: sage_eval(expression, autodeclarevariables=on)</p>
<p>Is there a way to do something like that?</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=12742#post-id-12742In the Sage notebook, you can do `automatic_variables(True)` and get variables automatically declared. See [here](http://sagenb.org/home/pub/3311/) for an example.Mon, 10 Oct 2011 07:10:41 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=12742#post-id-12742Comment by Jason Grout for <p>In the Sage notebook, you can do <code>automatic_variables(True)</code> and get variables automatically declared. See <a href="http://sagenb.org/home/pub/3311/">here</a> for an example.</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21142#post-id-21142no, it only works in the notebook. You might be able to code up a way to do it based on the code for that.Mon, 10 Oct 2011 22:28:11 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21142#post-id-21142Comment by nablaoperator for <p>In the Sage notebook, you can do <code>automatic_variables(True)</code> and get variables automatically declared. See <a href="http://sagenb.org/home/pub/3311/">here</a> for an example.</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21148#post-id-21148Unfortunately this doesn't work in the command line interface or in a standalone python program. Is there a similar command?Mon, 10 Oct 2011 07:15:04 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=21148#post-id-21148Answer by CIQALA for <p>Hello Everyone!</p>
<p>Is there a way to autodeclare variables?</p>
<p>For example:</p>
<p>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.</p>
<p>sage_eval(expression)</p>
<p>It works fine, but if the expression contains variables, these have to be manually declared first as var('variables').</p>
<p>I want to do this automatically, e.g.: sage_eval(expression, autodeclarevariables=on)</p>
<p>Is there a way to do something like that?</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=13627#post-id-13627Eviatar Buch Thank you for your answer. I accept your answer.
Tue, 29 May 2012 04:10:26 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=13627#post-id-13627Answer by parzan for <p>Hello Everyone!</p>
<p>Is there a way to autodeclare variables?</p>
<p>For example:</p>
<p>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.</p>
<p>sage_eval(expression)</p>
<p>It works fine, but if the expression contains variables, these have to be manually declared first as var('variables').</p>
<p>I want to do this automatically, e.g.: sage_eval(expression, autodeclarevariables=on)</p>
<p>Is there a way to do something like that?</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=12734#post-id-12734From 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](http://ask.sagemath.org/question/611/implicitly-defining-a-sequence-of-variables)).
Sun, 09 Oct 2011 14:09:27 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=12734#post-id-12734Answer by Eviatar Bach for <p>Hello Everyone!</p>
<p>Is there a way to autodeclare variables?</p>
<p>For example:</p>
<p>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.</p>
<p>sage_eval(expression)</p>
<p>It works fine, but if the expression contains variables, these have to be manually declared first as var('variables').</p>
<p>I want to do this automatically, e.g.: sage_eval(expression, autodeclarevariables=on)</p>
<p>Is there a way to do something like that?</p>
https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=13626#post-id-13626You 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())Mon, 28 May 2012 21:13:45 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?answer=13626#post-id-13626Comment by nablaoperator for <p>You can use <code>symbolic_expression</code>, which will automatically detect variables. For example,</p>
<pre><code>sage: symbolic_expression('3 * y')
3*y
</code></pre>
<p>If you need the variables in the global namespace, you can use</p>
<pre><code>map(var, symbolic_expression('3 * y').variables())
</code></pre>
https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=19515#post-id-19515Awesome, thanks!!Mon, 25 Jun 2012 10:29:45 +0200https://ask.sagemath.org/question/8363/autodeclare-variables/?comment=19515#post-id-19515