guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strides


From: Zelphir Kaltstahl
Subject: Re: Strides
Date: Fri, 28 Dec 2018 16:04:52 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1

On 28.12.18 15:34, address@hidden wrote:
>>>> With regard to NumPy, especially the stuff about the "strides" (whatever
>>>> that is)
> Strides are an extremely powerful concept that allow transformations to be
> done on arrays without actually moving or changing any of their data.  The
> stride of a dimension is the distance in storage units (pointers, small
> integers, floats, characters, whatever is in the array) between consecutive
> indexes  along that dimension.
>
> Suppose you have a 3 x 3 matrix:
>
> 0 1 2
> 3 4 5
> 6 7 8
>
> If it is stored row-by-row (C order) then it will appear in storage as 0 1
> 2 3 4 5 6 7 8.  Suppose you are at location [0, 1], whose value is 1 (row
> indices are written before column indices).  In order to get to [0, 2], the
> next column in the same row, you have to move along just 1 element.  But to
> get to  [1, 1],  the next row in the same column, you have to move along 3
> elements.
>
> So knowing the strides makes it easy to step along either rows or columns
> fast.  But wait, there's more!  Suppose you create a new array object,
> still 3 x 3, but with a row stride of 1 and a column stride of 3, but
> sharing the same storage.  Hey presto, you now have transposed (rotated by
> 90 degrees) the original array, and it is now
>
> 0 3 6
> 1 4 7
> 2 5 8

Aha! That's a great explanation, thank you! It seems that strides are
simply "how to calculate your indices" and similar to what I've done
sometimes earlier, when I used a lot of loops in other languages. By
changing the strides (which are added to addresses to get to some
element), I can make the array look differently, because my "movement"
in the array is translated differently to an actual way of which address
I am talking about. Thus I do not need to change the array's data at
all, saving a lot of time, by doing something with only the indices
(addresses).

> In languages without strides, you'd have to copy all the elements.  Here,
> no elements were copied in the making of this new array.
>
> As another example, say you create yet another array sharing the same
> storage.  This one has one dimension only and a stride of 4.  Its elements
> are
>
> 0 4 8
>
> and in fact it is the diagonal of the (original or transposed) array.
> Again, no copying was done.  By playing tricks with upper and lower bounds,
> strides, and the array offset (which is the storage address of point [0,
> 0]) you can translate a 0-based matrix to a 1-based matrix whose rows and
> columns are numbered 1, 2, 3 instead of 0, 1, 2, or even one with row and
> column numbers -1, 0, 1 if you want.  You can get any of the corners of the
> array as 2 x 2 matrices.  Everything generalizes to any number of
> dimensions, and there are many more possibilities: anything that can be
> expressed as an affine transformation can be obtained in this way.
>
> -- John Cowan http://vrici.lojban.org/~cowan address@hidden There are
> books that are at once excellent and boring. Those that at once leap
> to the mind are Thoreau's Walden, Emerson's Essays, George Eliot's
> Adam Bede, and Landor's Dialogues. --Somerset Maugham
Thanks for that explanation!


reply via email to

[Prev in Thread] Current Thread [Next in Thread]