Ask Your Question

define/generate new variables automatically

asked 2013-11-21 23:44:22 -0500

gundamlh gravatar image

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 I can use this function with two/three for-loops to construct one symbolic matrix/tensor.

Thanks in advance!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-11-21 23:58:30 -0500

Luca gravatar image

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

edit flag offensive delete link more


wow, clever! Thanks! Does this "technique" appear quite often in codes in Numpy?

gundamlh gravatar imagegundamlh ( 2013-11-22 02:43:35 -0500 )edit

I know! What I should learn is NumPy.., and ask questions in stackoverflow .

gundamlh gravatar imagegundamlh ( 2013-11-22 04:34:53 -0500 )edit

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

Luca gravatar imageLuca ( 2013-11-23 02:33:49 -0500 )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


Asked: 2013-11-21 23:44:22 -0500

Seen: 420 times

Last updated: Nov 21 '13