ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 18 Nov 2019 17:18:47 -0600[Sage vs Python] Matrix indexing and list standardshttps://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/Hello, Sage Community!
Since I learned Sage, I have used the notation `A[i][j]` (which obviously came from my previous experience with Python lists) to access the (i+1, j+1)-th element of a matrix. However, I discovered a few weeks ago that I can also use the syntax `A[i,j]`. I am wondering which of these two ways of matrix indexing is preferred in Sage?
My tests using `timeit` indicate that the second syntax is faster than the first one by a factor of 500. So I suppose it to be simply natural that the fastest syntax is preferred. However, why to deviate from the list syntax? After all, lists and lists of lists are (in some sense) arrays, too.
On the other hand, talking about lists, there is the syntax `[1..10]`, which creates the list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Why is this the case when Python would ignore the 10? For example, `range(1, 10)` is equivalent to
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Even `srange(1, 10)` creates the same list, but with Sage `Integer`s, instead of Python's `int`s. Is there any good reason for having `[1..10]`be equivalent to `srange(1, 11)`?
Thanks in advance for your answers!Mon, 18 Nov 2019 16:18:18 -0600https://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/Answer by John Palmieri for <p>Hello, Sage Community!</p>
<p>Since I learned Sage, I have used the notation <code>A[i][j]</code> (which obviously came from my previous experience with Python lists) to access the (i+1, j+1)-th element of a matrix. However, I discovered a few weeks ago that I can also use the syntax <code>A[i,j]</code>. I am wondering which of these two ways of matrix indexing is preferred in Sage?</p>
<p>My tests using <code>timeit</code> indicate that the second syntax is faster than the first one by a factor of 500. So I suppose it to be simply natural that the fastest syntax is preferred. However, why to deviate from the list syntax? After all, lists and lists of lists are (in some sense) arrays, too.</p>
<p>On the other hand, talking about lists, there is the syntax <code>[1..10]</code>, which creates the list</p>
<pre><code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
</code></pre>
<p>Why is this the case when Python would ignore the 10? For example, <code>range(1, 10)</code> is equivalent to</p>
<pre><code>[1, 2, 3, 4, 5, 6, 7, 8, 9]
</code></pre>
<p>Even <code>srange(1, 10)</code> creates the same list, but with Sage <code>Integer</code>s, instead of Python's <code>int</code>s. Is there any good reason for having <code>[1..10]</code>be equivalent to <code>srange(1, 11)</code>?</p>
<p>Thanks in advance for your answers!</p>
https://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/?answer=48804#post-id-48804I would expect `A[i][j]` to be slower because it first has to create the vector `A[i]` and then extract an element from it, whereas `A[i,j]` does a single extraction without creating intermediate objects. There is overhead in constructing a mathematical object like a vector. Also, `A[i,j]` calls a single method, `A.__getitem__`, whereas `A[i][j]` performs two calls: it calls `A.__getitem__` once to form the vector `v = A[i]`, and then calls `v.__getitem__` on that vector.
So if you care about performance, use `A[i,j]`. If you need the intermediate vector, use `A[i]`.
Regarding the ellipsis notation, it's written for mathematicians, like a lot of Sage. In mathematical writing, "1, 2, ..., 10" includes 10. `range` is not a standard thing for mathematicians, so there was no reason to change Python's behavior, and `srange` is the Sage version of that, so again, no reason to deviate from Python. But mathematicians will expect `[2, 4, .., 10]` to include 10.Mon, 18 Nov 2019 16:37:27 -0600https://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/?answer=48804#post-id-48804Comment by dsejas for <p>I would expect <code>A[i][j]</code> to be slower because it first has to create the vector <code>A[i]</code> and then extract an element from it, whereas <code>A[i,j]</code> does a single extraction without creating intermediate objects. There is overhead in constructing a mathematical object like a vector. Also, <code>A[i,j]</code> calls a single method, <code>A.__getitem__</code>, whereas <code>A[i][j]</code> performs two calls: it calls <code>A.__getitem__</code> once to form the vector <code>v = A[i]</code>, and then calls <code>v.__getitem__</code> on that vector.</p>
<p>So if you care about performance, use <code>A[i,j]</code>. If you need the intermediate vector, use <code>A[i]</code>.</p>
<p>Regarding the ellipsis notation, it's written for mathematicians, like a lot of Sage. In mathematical writing, "1, 2, ..., 10" includes 10. <code>range</code> is not a standard thing for mathematicians, so there was no reason to change Python's behavior, and <code>srange</code> is the Sage version of that, so again, no reason to deviate from Python. But mathematicians will expect <code>[2, 4, .., 10]</code> to include 10.</p>
https://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/?comment=48807#post-id-48807Yeah, sorry. I am exhausted by so much work, that I didn't organize my ideas correctly. Thank you very much for answering.
Should I separate them?
I am a mathematician, but I never used the ellipsis in the context of programming. Happy to learn something new.Mon, 18 Nov 2019 17:18:47 -0600https://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/?comment=48807#post-id-48807Comment by John Palmieri for <p>I would expect <code>A[i][j]</code> to be slower because it first has to create the vector <code>A[i]</code> and then extract an element from it, whereas <code>A[i,j]</code> does a single extraction without creating intermediate objects. There is overhead in constructing a mathematical object like a vector. Also, <code>A[i,j]</code> calls a single method, <code>A.__getitem__</code>, whereas <code>A[i][j]</code> performs two calls: it calls <code>A.__getitem__</code> once to form the vector <code>v = A[i]</code>, and then calls <code>v.__getitem__</code> on that vector.</p>
<p>So if you care about performance, use <code>A[i,j]</code>. If you need the intermediate vector, use <code>A[i]</code>.</p>
<p>Regarding the ellipsis notation, it's written for mathematicians, like a lot of Sage. In mathematical writing, "1, 2, ..., 10" includes 10. <code>range</code> is not a standard thing for mathematicians, so there was no reason to change Python's behavior, and <code>srange</code> is the Sage version of that, so again, no reason to deviate from Python. But mathematicians will expect <code>[2, 4, .., 10]</code> to include 10.</p>
https://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/?comment=48806#post-id-48806These are really two separate questions, aren't they?Mon, 18 Nov 2019 16:55:09 -0600https://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/?comment=48806#post-id-48806Comment by dsejas for <p>I would expect <code>A[i][j]</code> to be slower because it first has to create the vector <code>A[i]</code> and then extract an element from it, whereas <code>A[i,j]</code> does a single extraction without creating intermediate objects. There is overhead in constructing a mathematical object like a vector. Also, <code>A[i,j]</code> calls a single method, <code>A.__getitem__</code>, whereas <code>A[i][j]</code> performs two calls: it calls <code>A.__getitem__</code> once to form the vector <code>v = A[i]</code>, and then calls <code>v.__getitem__</code> on that vector.</p>
<p>So if you care about performance, use <code>A[i,j]</code>. If you need the intermediate vector, use <code>A[i]</code>.</p>
<p>Regarding the ellipsis notation, it's written for mathematicians, like a lot of Sage. In mathematical writing, "1, 2, ..., 10" includes 10. <code>range</code> is not a standard thing for mathematicians, so there was no reason to change Python's behavior, and <code>srange</code> is the Sage version of that, so again, no reason to deviate from Python. But mathematicians will expect <code>[2, 4, .., 10]</code> to include 10.</p>
https://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/?comment=48805#post-id-48805Hello, @john-palmieri. Thank you very much for answering my question. Unfortunately, I have updated it to include another inconsistency between Sage and Python. I hope you can help me with that too, so I can tick your answer as correct.Mon, 18 Nov 2019 16:46:56 -0600https://ask.sagemath.org/question/48802/sage-vs-python-matrix-indexing-and-list-standards/?comment=48805#post-id-48805