guile-user
[Top][All Lists]

## Re: Matrix or array operations library

 From: John Cowan Subject: Re: Matrix or array operations library Date: Thu, 27 Dec 2018 16:24:29 -0500

```>
> >> 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

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.

--