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.Sat, 23 Nov 2013 09:33:49 +0100define/generate new variables automaticallyhttps://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/Dear all,
I'd like to use symbolic boxes in SAGE to check the proof of a theorem.
sage: var('a11, a12, a13, a21, a22, a23, a31, a32, a33');
sage: A = matrix(SR, 3, 3, [a11, a12, a13, a21, a22, a23, a31, a32, a33])
It is really time-consuming..
[genvarname@MATLAB](http://www.mathworks.de/de/help/matlab/ref/genvarname.html)
I can use this function with two/three for-loops to construct one symbolic matrix/tensor.
Thanks in advance!
Fri, 22 Nov 2013 06:44:22 +0100https://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/Answer by Luca for <p>Dear all,</p>
<p>I'd like to use symbolic boxes in SAGE to check the proof of a theorem. </p>
<pre><code>sage: var('a11, a12, a13, a21, a22, a23, a31, a32, a33');
sage: A = matrix(SR, 3, 3, [a11, a12, a13, a21, a22, a23, a31, a32, a33])
</code></pre>
<p>It is really time-consuming..</p>
<p><a href="http://www.mathworks.de/de/help/matlab/ref/genvarname.html">genvarname@MATLAB</a>
I can use this function with two/three for-loops to construct one symbolic matrix/tensor.</p>
<p>Thanks in advance!</p>
https://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/?answer=15716#post-id-15716Not exactly the same as `genvarname`, but there you go:
var(['a%d%d' % tuple(p) for p in Zmod(3)^2])
Of course, there's tons of other ways of obtaining a similar result.Fri, 22 Nov 2013 06:58:30 +0100https://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/?answer=15716#post-id-15716Comment by gundamlh for <p>Not exactly the same as <code>genvarname</code>, but there you go:</p>
<pre><code>var(['a%d%d' % tuple(p) for p in Zmod(3)^2])
</code></pre>
<p>Of course, there's tons of other ways of obtaining a similar result.</p>
https://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/?comment=16661#post-id-16661wow, clever! Thanks! Does this "technique" appear quite often in codes in Numpy?Fri, 22 Nov 2013 09:43:35 +0100https://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/?comment=16661#post-id-16661Comment by gundamlh for <p>Not exactly the same as <code>genvarname</code>, but there you go:</p>
<pre><code>var(['a%d%d' % tuple(p) for p in Zmod(3)^2])
</code></pre>
<p>Of course, there's tons of other ways of obtaining a similar result.</p>
https://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/?comment=16660#post-id-16660I know! What I should learn is NumPy.. http://mathesaurus.sourceforge.net/matlab-numpy.html, and ask questions in stackoverflow .Fri, 22 Nov 2013 11:34:53 +0100https://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/?comment=16660#post-id-16660Comment by Luca for <p>Not exactly the same as <code>genvarname</code>, but there you go:</p>
<pre><code>var(['a%d%d' % tuple(p) for p in Zmod(3)^2])
</code></pre>
<p>Of course, there's tons of other ways of obtaining a similar result.</p>
https://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/?comment=16648#post-id-16648`Zmod` is a Sage object, so this technique is specific to Sage (`var` is specific to Sage, anyway). I guess in pure Python you'd write something like
import itertools
['a%d%d' % p for p in itertools.product(range(3), range(3))]
You can ask Sage related questions in stackoverflow too. Many of us watch the "sage" tag.Sat, 23 Nov 2013 09:33:49 +0100https://ask.sagemath.org/question/10765/definegenerate-new-variables-automatically/?comment=16648#post-id-16648