Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Why is it that a:b specifies the columns from a to b-1?</p> </blockquote> <p>This is a Python convention. The same is true for ordinary lists and <code>range(a, b)</code> will return a list containing the numbers <code>a</code> up to and including <code>b-1</code>, but not <code>b</code>. The benefit of this convention is that slicing by <code>a:b</code> where <code>a</code> and <code>b</code> are numbers will return <code>b-a</code> elements/rows/columns instead of the more complicated <code>a-b+1</code>.</p> <blockquote> <p>It looks like if you create a view using a tuple, it somehow doesn't correctly propagate any further side effects on the original matrix.</p> </blockquote> <p>This is a Numpy idiosyncrasy, caused by the fact that it can only create views based on slices; these can be efficiently implemented, while tuple-based slices cannot. You can simulate this behavior with the following snippet, which shows what Python index syntax does under the hood:</p> <pre><code>class FakeArray(object): def __getitem__(self, idx): return "You can't change the original FakeArray through me" def __setitem__(self, idx, val): print("We could set elements %r to %r here" % (idx, val)) </code></pre> <p>Now try</p> <pre><code>&gt;&gt;&gt; A = FakeArray() &gt;&gt;&gt; A[1:2] "You can't change the original FakeArray through me" &gt;&gt;&gt; A[1:2] = 'ham' We could set elements slice(1, 2, None) to 'ham' </code></pre> <p>So <code>A[1:2]</code> is shorthand for <code>A.__getitem__(slice(1, 2, None))</code> while <code>A[1:2] = 'ham'</code> is shorthand for <code>A.__setitem__(slice(1, 2, None), 'ham')</code>. Since there's really two distinct methods involved, the behavior of slicing can be very different depending on whether it's part of an assignment statement. In the Numpy case, there's a subtle interplay between this difference and that between <code>slice</code> and <code>tuple</code> objects.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload