public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
From: Jamison Hope <jrh@theptrgroup.com>
To: Kawa mailing list <kawa@sourceware.org>
Subject: Re: APL-style array indexing in Kawa - a sketch
Date: Mon, 17 Aug 2015 20:24:00 -0000	[thread overview]
Message-ID: <4E24D268-9062-465B-BF2B-966806394A39@theptrgroup.com> (raw)
In-Reply-To: <55CD4B6A.8010606@bothner.com>

On Aug 13, 2015, at 9:59 PM, Per Bothner <per@bothner.com> wrote:

> 
> I recently changed the various vector classes to use an index mapper:
> Every vector has two fields: a raw Java array buffer, as well as
> an IntSequence mapper which maps vector indexes to buffer indexes.
> This allows slices, rotations, permutations, etc, using the
> same same buffer:
>  (VEC I) => index I in VEC ; function-call notation for indexeing
>  (set! (VEC i) VALUE) - update value
>  (VEC [i <: J]) => slice of VEC ; same buffer, new index mapper
>  (VEC [i0 i1 ... in]) ==> [(VEC i0) (VEC i1) ... (VEC in)]
>      - but re-using the VEC buffer with a new index mapper
>  (set! (VEC [i <: J]) VALUE) - replace elements (possible re-size)

In this last example, VALUE is a sequence and not an atom, right?  So:

(define v (make-vector 5 0)) ;; -> #(0 0 0 0 0)

(set! (v [2 <: 4]) 1) ;; error!

(set! (v [2 <: 4]) #(1 1)) ;; -> v is now #(0 0 1 1 0)

(set! (v [2 <: 4]) #(1)) ;; -> v is now #(0 0 1 0)

> Kawa also supports multi-dimensional arrays, based on SRFI-25, and you
> can use the same function-call notation:
>  (ARR X Y)
>  (set! (ARR X Y) VALUE)
> SRFI-25 (and Kawa) has functions for slices, transpose, rotations, etc.
> However, I'd like to have APL-style array-as-index for general arrays:
>  (ARR [A <: B] Y [c <: D])
>  (ARR [5 2 6] [2 3])

So the way to read this is that it constructs a new 3x2 2D array which
shares backing store with ARR, and its six elements in row-major order
are (ARR 5 2), (ARR 5 3), (ARR 2 2), (ARR 2 3), (ARR 6 2), and (ARR 6 3),
right?


Is there some clever notation for a range meaning "from here to the end,
wherever that is"?  (VEC [i <: ]) doesn't work;
(VEC [i <: (length VEC)]) works, but seems verbose.

Matlab (which uses 1-based indexing) lets you do things like A(2:end) to
extract all-but-the-first element.  Also, for dimensions for which you
want the whole thing, instead of writing "1:end" you can use ":" as
shorthand:  A(:,3) means the whole third column (regardless of the
number of rows), and A(3,:) means the whole third row (regardless of the
number of columns).


>  (ARR IARR1 IARR2 IARR3)
> 
> The bulk of this note shows (tersely) the types, classes, and
> basic representation of generalized arrays.  It focuses on the
> differences from the version of gnu.lists in the Kawa trunk.
> The actual Scheme API (in terms of functions) is a later discussion;
> initially we will prioritize array construction plus indexing.
> 
> * Is this a feature you'd find exciting and/or use it?

I can definitely think of math-ish things to do with something like this.

If I have a 4x4 matrix T representing a spatial transformation, then
(T [0 <: 3] [0 <: 3]) would be the upper-left 3x3 submatrix, which would
be a rotation matrix that magically stays in sync with the original
transform data.  That could be handy.

The values at [(T 0 3) (T 1 3) (T 2 3)] are the translation vector.
Using this new notation, that's *almost* the same as (T [0 <: 3] [3]),
except that the latter gives me a 3x1 2D matrix rather than a 3-element
1D vector.

Will there be some simple way to flatten the result to reduce its rank
so I can omit the index that would always be 0?

> * Would your friends or co-workers find it exciting and/or use it?

They may, if I tell them to.. most likely they would use it unknowingly
by using some program that I wrote (there aren't a lot of Schemers or
Lispers -- or Java-ers, really -- here).

> * I know computers and data structures.  I don't know matrixes or math, much.
>  Would this approach cause problems if trying to to do high-performace
>  array/matrix manipulations, or interfacing to other libraries?

It's kind of hard to say.  It depends whether the added indirection of
the index mapper introduces more overhead than the gain from not
allocating a new data store and copying over the values at array
creation time, right?  It certainly *seems* like it would be a win, at
least for arrays larger than some size N (wherever the size of the data
starts to outgrow the size of the index mapper object).

What would be the idiomatic way to do a deep(?) copy, to break the
connection with the original data buffer?  One situation I can think of
where the sharing could cause trouble would be passing the sub-vector to
something like an in-place sort function, where we might not actually
want to overwrite the parent array's data.


> * Would you be interested in helping out?


I'll certainly try it out and let you know if stuff doesn't work, but I
don't know how much help I would be with the actual implementation.

--
Jamison Hope
The PTR Group
www.theptrgroup.com



  reply	other threads:[~2015-08-17 20:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-14  1:59 Per Bothner
2015-08-17 20:24 ` Jamison Hope [this message]
2015-08-17 22:48   ` Per Bothner
2015-08-18  3:37     ` Jamison Hope
2015-08-18  4:12       ` Per Bothner
2015-08-18  4:43         ` Jamison Hope

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

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

  git send-email \
    --in-reply-to=4E24D268-9062-465B-BF2B-966806394A39@theptrgroup.com \
    --to=jrh@theptrgroup.com \
    --cc=kawa@sourceware.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).