Ask Your Question
1

List indexed by (signed) integers?

asked 2015-07-30 22:23:40 +0200

sibilant gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-07-30 22:54:57 +0200

tmonteil gravatar image

updated 2015-07-30 22:57:19 +0200

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
edit flag offensive delete link 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.)

sibilant gravatar imagesibilant ( 2015-07-31 02:34:23 +0200 )edit

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

sibilant gravatar imagesibilant ( 2015-07-31 02:47:59 +0200 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2015-07-30 22:23:40 +0200

Seen: 147 times

Last updated: Jul 30 '15