Variables in Sage with many indices and with negative indices.

273 Views Asked by At

I need to define some variables in Sage with 2 indices and the second index is negative. For example, Y[2, -6]. I know how to define variables with one index:

 rank=8
R=PolynomialRing(QQ, 'a' ,rank+1)    
a=R.gens()

Edit: I tried the following codes to define Y[i,s]:

m, n = 8, 8
Y = {(i,j): var("Y_{}{}".format(i,j),
latex_name="Y_{{{}{}}}".format(i,j))
for i in [0..m] for j in [(-2*n)..0]}

But it is said that "The name "Y_0-16" is not a valid Python identifier".

Is there some way to define Y[i,s] in Sage? Thank you very much.

1

There are 1 best solutions below

0
On BEST ANSWER

This may do what you want.

sage: m, n = 8, 8
sage: Y = {(i, j): var("Y_{}_{}".format(i, str(j).replace('-', 'm')),
....:                  latex_name="Y_{{{},{}}}".format(i, j))
....:      for i in (0 .. m) for j in (-2*n .. 0)}

Usage:

sage: a = Y[0, -16] * Y[2, -3]
sage: a
Y_0_m16*Y_2_m3
sage: latex(a)
{Y_{0,-16}} {Y_{2,-3}}

We replaced the "minus sign" - with an m in Python identifiers but the LaTeX name is as desired.

In a Jupyter sheet, %display latex will ensure LaTeX rendering.