Ask Your Question

# List indexed by (signed) integers?

I've been doing some work in SAGE with a linear operator on a polynomial algebra T: F[x] --> F[x] (here F is some field, x is a variable, and T is the operator), storing the data of T as a list, so that T[n] keeps track of T(x^n). Here n is a nonnegative integer, of course. This has been worked pretty well for me.

Now I would like to do similar computations in a space where x is invertible. That is, I now have a linear operator T: F[x,x^{-1}] --> F[x, x^{-1}]. I would love to store the data of T as a signed-integer-indexed list, so that T[n] again keeps track of T(x^n), but now n can be any (positive or negative) integer.

Is there a way to implement this easily in Python/SAGE? Right now I am dragging T around as a pair of lists, T1[n] := T(x^n) and T2[n] := T(x^{-n}). This is annoying and inelegant and inefficient (I constantly have to take three cases -- n positive, negative, or zero), but it does work. But is there something better out there?

I am not a good programmer in any sense, and I won't have time to work very hard on this. But I hope that I am missing something clean and simple. Thanks in advance for any help.

edit retag close merge delete

## 1 answer

Sort by ยป oldest newest most voted

I would suggest to use Python dictionaries (you will easily find a lot of documentation with examples on the web), here is a very brief overview:

sage: D = {-1:12, 0:14, 1: 16}
sage: D
{-1: 12, 0: 14, 1: 16}
sage: D[0]
14
sage: D.has_key(-2)
False
sage: D[-2] = 18
sage: D
{-2: 18, -1: 12, 0: 14, 1: 16}
sage: D.keys()
[0, 1, -2, -1]
sage: D.values()
[14, 16, 18, 12]
sage: D.items()
[(0, 14), (1, 16), (-2, 18), (-1, 12)]
sage: for k in D:
....:     print k
....:     print D[k]
0
14
1
16
-2
18
-1
12
sage: for k in sorted(D):
....:     print k
....:     print D[k]
-2
18
-1
12
0
14
1
16

more

## Comments

Very promising -- thank you! I especially like that the syntax is the same, so that a lot of my old procedures will work without much editing. (I can't upvote yet; sorry.)

( 2015-07-30 19:34:23 -0600 )edit

Actually, it's hard to imagine that anything better exists. I accept.

( 2015-07-30 19:47:59 -0600 )edit

## Your Answer

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

Add Answer

## Stats

Asked: 2015-07-30 15:23:40 -0600

Seen: 36 times

Last updated: Jul 30 '15