Ask Your Question

double indexed variables in a table header

asked 2022-10-20 17:14:02 +0200

Cyrille gravatar image

I want to construct a table with a header like

$[\alpha_{0,0}, \alpha_{0,1},...\alpha_{1,0},\alpha_{1,1},...]$

but I do not want to enter the indices by hand. How can I do ?

Also I find the show result of the following command a little bit strange

α = var("α_", n=11,latex_name='\alpha_')
αα = list(α)

Can some one explain

edit retag flag offensive close merge delete


I think you should use '\\alpha' for the latex_name instead. Two slashes at the start because a backslash is a special character and needs to be "escaped". No trailing _ because Sage seems to insert that automatically in LaTeX names for indexed variables like this.

John Palmieri gravatar imageJohn Palmieri ( 2022-10-20 17:23:10 +0200 )edit

Your example gives one index. Can you give us an example with two indexes ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-10-20 18:38:23 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2022-10-22 18:13:21 +0200

slelievre gravatar image

Doubly indexed variables are often useful.

No easy construct exists like for simply indexed ones though.

That said, list comprehension makes it relatively easy.

Here is one way to obtain the requested indexing.

sage: α = [var(f"α_{i}_{j}", latex_name=fr"\alpha_{{{i},{j}}}")
....:      for i in range(3) for j in range(3)]
edit flag offensive delete link more

answered 2022-10-22 11:06:39 +0200

Emmanuel Charpentier gravatar image

Not sure of what your "double indexed variable means. One possible interpretation is a two-dimensionnal array of symbolic variables, flattened as needed :

sage: DIVar=[[var("alpha_%d_%d"%(u,v), latex_name="\\alpha_{%d,%d}"%(u, v)) for v in range(3)] for u in range(2)]

which is $$ \left[\left[{\alpha_{0,0}}, {\alpha_{0,1}}, {\alpha_{0,2}}\right], \left[{\alpha_{1,0}}, {\alpha_{1,1}}, {\alpha_{1,2}}\right]\right] $$

and can be used as:

sage: table([[f(u) for u in flatten(DIVar)] for f in (sin, cos, tan)], header_row=flatten(DIVar), header_column=[function("f")]+[sin, cos, tan])

  f   | alpha_0_0        alpha_0_1        alpha_0_2        alpha_1_0        alpha_1_1        alpha_1_2
  sin | sin(alpha_0_0)   sin(alpha_0_1)   sin(alpha_0_2)   sin(alpha_1_0)   sin(alpha_1_1)   sin(alpha_1_2)
  cos | cos(alpha_0_0)   cos(alpha_0_1)   cos(alpha_0_2)   cos(alpha_1_0)   cos(alpha_1_1)   cos(alpha_1_2)
  tan | tan(alpha_0_0)   tan(alpha_0_1)   tan(alpha_0_2)   tan(alpha_1_0)   tan(alpha_1_1)   tan(alpha_1_2)

[ No LaTeX output here : the generated latex code uses tabular, not understood by's Markdown. I know that there is a way to create an HTML version accepted here, but, for dear life, I'm unable to remember it... ].


edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-10-20 17:14:02 +0200

Seen: 122 times

Last updated: Oct 22 '22