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.Fri, 04 Nov 2022 12:16:47 +0100How to take the index of a variable in sagemath?https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/I used the method in the [post](https://ask.sagemath.org/question/7715/symbolic-linear-algebra/) to generate a matrix with entries which have indices.
def symbolic_matrix(root,m,n):
mlist=[]
for i in range(m):
for j in range(n):
mlist.append(root+'_'+str(i)+'_'+str(j))
return Matrix(SR,m,n,mlist)
k,n=4,8
r1=symbolic_matrix('y',k,n)
r1
Now I would like to get the index of the entries of the matrix r1. For example, the index of y_1_2 is [1,2]. How to do this in sagemath? I tried something like y.index(y_1_2) but it doesn't work. Thank you very much.Thu, 03 Nov 2022 18:39:29 +0100https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/Comment by John Palmieri for <p>I used the method in the <a href="https://ask.sagemath.org/question/7715/symbolic-linear-algebra/">post</a> to generate a matrix with entries which have indices. </p>
<pre><code>def symbolic_matrix(root,m,n):
mlist=[]
for i in range(m):
for j in range(n):
mlist.append(root+'_'+str(i)+'_'+str(j))
return Matrix(SR,m,n,mlist)
k,n=4,8
r1=symbolic_matrix('y',k,n)
r1
</code></pre>
<p>Now I would like to get the index of the entries of the matrix r1. For example, the index of y_1_2 is [1,2]. How to do this in sagemath? I tried something like y.index(y_1_2) but it doesn't work. Thank you very much.</p>
https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/?comment=64759#post-id-64759If you are willing to work with a polynomial ring instead of the symbolic ring, then you can construct those with generators given as an array: `PolynomialRing(QQ, 3, 5, var_array='y')`. There is no obvious way to access the indices of those generators, unfortunately.Thu, 03 Nov 2022 23:11:47 +0100https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/?comment=64759#post-id-64759Answer by rburing for <p>I used the method in the <a href="https://ask.sagemath.org/question/7715/symbolic-linear-algebra/">post</a> to generate a matrix with entries which have indices. </p>
<pre><code>def symbolic_matrix(root,m,n):
mlist=[]
for i in range(m):
for j in range(n):
mlist.append(root+'_'+str(i)+'_'+str(j))
return Matrix(SR,m,n,mlist)
k,n=4,8
r1=symbolic_matrix('y',k,n)
r1
</code></pre>
<p>Now I would like to get the index of the entries of the matrix r1. For example, the index of y_1_2 is [1,2]. How to do this in sagemath? I tried something like y.index(y_1_2) but it doesn't work. Thank you very much.</p>
https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/?answer=64763#post-id-64763It's better to set things up more conveniently from the beginning. Symbolically:
indices = [(i,j) for i in range(3) for j in range(5)]
variables = [var('y_{}_{}'.format(i,j)) for (i,j) in indices]
y = dict(zip(indices, variables))
M = matrix(SR, 3, 5, variables)
Or with polynomials:
indices = [(i,j) for i in range(3) for j in range(5)]
R = PolynomialRing(QQ, ['y_{}_{}'.format(i,j) for (i,j) in indices])
y = dict(zip(indices, R.gens()))
M = matrix(R, 3, 5, R.gens())
In both cases:
sage: y[2,0]
y_2_0
sage: M[1,3] is y[1,3]
TrueFri, 04 Nov 2022 12:16:47 +0100https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/?answer=64763#post-id-64763Answer by Max Alekseyev for <p>I used the method in the <a href="https://ask.sagemath.org/question/7715/symbolic-linear-algebra/">post</a> to generate a matrix with entries which have indices. </p>
<pre><code>def symbolic_matrix(root,m,n):
mlist=[]
for i in range(m):
for j in range(n):
mlist.append(root+'_'+str(i)+'_'+str(j))
return Matrix(SR,m,n,mlist)
k,n=4,8
r1=symbolic_matrix('y',k,n)
r1
</code></pre>
<p>Now I would like to get the index of the entries of the matrix r1. For example, the index of y_1_2 is [1,2]. How to do this in sagemath? I tried something like y.index(y_1_2) but it doesn't work. Thank you very much.</p>
https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/?answer=64758#post-id-64758One possible solution is to construct/return not only a matrix but also a mapping (dict) of variables to their indices - like:
def symbolic_matrix(root,m,n):
var2ind = {}
mlist=[]
for i in range(m):
for j in range(n):
v = SR(root+'_'+str(i)+'_'+str(j))
var2ind[v] = [i,j]
mlist.append(v)
return Matrix(SR,m,n,mlist), var2ind
k,n=4,8
r1, v2i = symbolic_matrix('y',k,n)
v2i[r1[2,3]]Thu, 03 Nov 2022 19:32:09 +0100https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/?answer=64758#post-id-64758Answer by John Palmieri for <p>I used the method in the <a href="https://ask.sagemath.org/question/7715/symbolic-linear-algebra/">post</a> to generate a matrix with entries which have indices. </p>
<pre><code>def symbolic_matrix(root,m,n):
mlist=[]
for i in range(m):
for j in range(n):
mlist.append(root+'_'+str(i)+'_'+str(j))
return Matrix(SR,m,n,mlist)
k,n=4,8
r1=symbolic_matrix('y',k,n)
r1
</code></pre>
<p>Now I would like to get the index of the entries of the matrix r1. For example, the index of y_1_2 is [1,2]. How to do this in sagemath? I tried something like y.index(y_1_2) but it doesn't work. Thank you very much.</p>
https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/?answer=64757#post-id-64757I think the easiest thing to do, maybe the only thing to do, is to parse the name of the variable as a string. Try `str(r1[0,0]).split('_')`, for example. If you let `a = r1[0,0]`, then `str(a).split('_')[1:]` will give you the two indices as strings, so you could do `[int(s) for s in str(a).split('_')[1:]]`.Thu, 03 Nov 2022 19:22:57 +0100https://ask.sagemath.org/question/64755/how-to-take-the-index-of-a-variable-in-sagemath/?answer=64757#post-id-64757