unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: John Cowan <cowan@ccil.org>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: Matrix or array operations library
Date: Thu, 27 Dec 2018 16:24:29 -0500	[thread overview]
Message-ID: <CAD2gp_Q7rsT63HfUaNuyNDcuQ-4OCrHhs9_aO5NpH0nrQhfwpQ@mail.gmail.com> (raw)
In-Reply-To: <2FB4CE72-80C9-46CE-B33F-8D133CD77A84@bluewin.ch>

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

-- 
John Cowan          http://vrici.lojban.org/~cowan        cowan@ccil.org
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


  reply	other threads:[~2018-12-27 21:24 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.108.1545930019.12294.guile-user@gnu.org>
2018-12-27 18:43 ` Matrix or array operations library Daniel Llorens
2018-12-27 21:24   ` John Cowan [this message]
2018-12-27 22:24   ` Matt Wette
     [not found] <mailman.32929.1546007691.1283.guile-user@gnu.org>
2018-12-28 20:23 ` Daniel Llorens
2018-12-28 23:16   ` John Cowan
2018-12-29  0:17     ` Daniel Llorens
2019-01-27 18:41       ` Matt Wette
2018-12-28 23:24   ` Matt Wette
     [not found] <mailman.73.1545757221.8862.guile-user@gnu.org>
2018-12-26 11:38 ` Zelphir Kaltstahl
2018-12-27 14:20   ` Matt Wette
2018-12-27 14:38     ` Mike Gran
     [not found] <c2172031-a0a6-9dd3-6ceb-7b6d94648475@gmail.com>
2018-12-24 22:01 ` Zelphir Kaltstahl
2018-12-24 23:06   ` Tk
2018-12-25  0:21   ` Matt Wette
2018-12-25 17:12   ` David Pirotte
2019-06-02 21:44   ` Linas Vepstas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAD2gp_Q7rsT63HfUaNuyNDcuQ-4OCrHhs9_aO5NpH0nrQhfwpQ@mail.gmail.com \
    --to=cowan@ccil.org \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).