Ask Your Question

How to take the index of a variable in sagemath?

asked 2022-11-03 18:39:29 +0100

lijr07 gravatar image

updated 2022-11-03 19:22:50 +0100

Max Alekseyev gravatar image

I used the method in the post to generate a matrix with entries which have indices.

def symbolic_matrix(root,m,n):
    for i in range(m):
        for j in range(n):
    return Matrix(SR,m,n,mlist)


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.

edit retag flag offensive close merge delete


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

John Palmieri gravatar imageJohn Palmieri ( 2022-11-03 23:11:47 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2022-11-04 12:16:47 +0100

rburing gravatar image

It'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]
sage: M[1,3] is y[1,3]
edit flag offensive delete link more

answered 2022-11-03 19:32:09 +0100

Max Alekseyev gravatar image

updated 2022-11-03 19:46:55 +0100

One 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 = {}
    for i in range(m):
        for j in range(n):
            v = SR(root+'_'+str(i)+'_'+str(j))
            var2ind[v] = [i,j]
    return Matrix(SR,m,n,mlist), var2ind

r1, v2i = symbolic_matrix('y',k,n)

edit flag offensive delete link more

answered 2022-11-03 19:22:57 +0100

updated 2022-11-03 19:23:26 +0100

I 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:]].

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-11-03 18:39:29 +0100

Seen: 119 times

Last updated: Nov 04 '22