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, 31 Jul 2015 02:47:59 +0200List indexed by (signed) integers?https://ask.sagemath.org/question/28726/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. Thu, 30 Jul 2015 22:23:40 +0200https://ask.sagemath.org/question/28726/list-indexed-by-signed-integers/Answer by tmonteil for <p>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. </p>
<p>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. </p>
<p>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? </p>
<p>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. </p>
https://ask.sagemath.org/question/28726/list-indexed-by-signed-integers/?answer=28727#post-id-28727I 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
Thu, 30 Jul 2015 22:54:57 +0200https://ask.sagemath.org/question/28726/list-indexed-by-signed-integers/?answer=28727#post-id-28727Comment by sibilant for <p>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:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/28726/list-indexed-by-signed-integers/?comment=28730#post-id-28730Very 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.)Fri, 31 Jul 2015 02:34:23 +0200https://ask.sagemath.org/question/28726/list-indexed-by-signed-integers/?comment=28730#post-id-28730Comment by sibilant for <p>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:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/28726/list-indexed-by-signed-integers/?comment=28731#post-id-28731Actually, it's hard to imagine that anything better exists. I accept.Fri, 31 Jul 2015 02:47:59 +0200https://ask.sagemath.org/question/28726/list-indexed-by-signed-integers/?comment=28731#post-id-28731