[plt-scheme] Re: More PLT Scheme as an Alternative to Matlab

From: Doug Williams (m.douglas.williams at gmail.com)
Date: Sun Aug 16 17:56:37 EDT 2009

I've got more of the multidimensional array code prototyped and I'll put it
up on GitHub (http://github.com/DrDoug/numeric/tree/master). The interface
elements are shown below:

(provide object u8 u16 u32 u64 s8 s16 s32 s64 f32 f64 cf32 cf64)

(provide/contract
 (vtype?
  (-> any/c boolean?))
 (array?
  (-> any/c boolean?))
 (array-shape
  (-> array? (listof exact-nonnegative-integer?)))
 (array-type
  (-> array? vtype?))
 (array-order
  (-> array? (symbols 'row 'column)))
 (array-n-dim
  (-> array? exact-nonnegative-integer?))
 (array-size
  (-> array? exact-nonnegative-integer?))
 (describe-array
  (-> array? void?))
 (make-array
  (->* ((listof exact-nonnegative-integer?))
       (#:type (or/c vtype? symbol?)
        #:order (symbols 'row 'column)
        #:fill any/c)
       array?))
 (build-array
  (->* ((listof exact-nonnegative-integer?)
        procedure?)
       (#:type (or/c vtype? symbol?)
        #:order (symbols 'row 'column))
       array?))
 (arange
  (->* (exact-nonnegative-integer?)
       (#:type (or/c vtype? symbol?)
        #:order (symbols 'row 'column))
       array?))
 (transpose
  (-> array? array?))
 (reshape
  (-> array? (listof exact-nonnegative-integer?) array?))
 (array-ref
  (->* (array?) () #:rest (listof exact-nonnegative-integer?) any/c))
 (array-ref*
  (-> array? list? any/c))
 (real
  (-> array? array?))
 (imag
  (-> array? array?))
 (array->list
  (-> array? list?))
 (print-array
  (-> array? void?))
 (list->array
  (->* (list?)
       (#:type (or/c vtype? symbol?)
        #:order (symbols 'row 'column))
       array?)))

This is prototype code only. It runs, but with limited error checking
(although the contracts catch a lot of errors). I'm sure there are fragile
areas.

There are a couple of SRFIs dealing with arrays, but they aren't of the same
ilk as what we're looking for here. But, this is somewhat of a superset of
those and I should avoid being different just out of ignorance.

Per Scott's suggestion, I have changed the named of the complex floats from
c64 and C128 to cf32 and cf64.

I have both array-ref and array-ref*. The difference is that array-ref*
takes the index specification as a list of indices while array-ref takes the
indices as parameters. So, (array-ref some-array i j) is equivalent to
(array-ref* some-array (list i j)). I've found both to be convenient. I'm
also open to alternatives.

I've added slicing to array ref (and array-ref*). A slice specifies a
subarray. So: if we define an array, a:

(define a (reshape (arange 12) '(3 4))) ;; Creates a 3 x 4 array with a[0,0]
= 0, a[0,1] = 1, ..., a[2.3] = 11

a = [[0 1 2 3]
       [4 5 6 7]
       [8 9 10 11]]

then (array-ref* a '(0 1)) -> 1, while (array-ref* a '(* 3)) is the 4th
column of a and is [3 7 11] and (array-ref* a '(1 *)) is the 2nd row of a
and id [4 5 6 7].

An index can also specify indices as (start stop step). So, (array-ref a '(1
(1 * 2))) is every second column starting with index 1 of the first row of a
and is [5 7].

Some examples of index ranges are:
(1), which is equivalent to (1 * *), is index 1 through the last index with
a step of 1 (the default)
(1 4) is index 1 through 4 with a step of 1
(1 * 2) is index 1 through the last index with a step of 2.
(* * 2) is every second index starting with 0

This is concise, but maybe not obvious. Also. it differs from the in-range
sequence when maybe it should follow that. Some others might prefer a more
verbose, but more readable alternative - maybe (#:start 1 #:step 2).
Comments are welcome.

So, array-ref can either return a scalar (if the indices are fully
specified) or a subarray.

There are a number of functions to construct arrays: make-array,
build-array, arange, and list-> array. [With immutability, the primitive
make-array isn't very useful.]

The functions print-array and describe-array are quick-and-dirty
implementations to support the prototyping effort.

The functions like transpose, reshape, real, imag, and array-list show some
of the typical functions we'll have to provide.

---

I think it would be good to move this discussion off the general list -
unless others want to keep it here. Github has a wiki we can use. It is set
up automatically, but I've never used it and don't know how convenient it
is. If anyone here has used it in the past let me know if you think it's a
good place to move to. Also, I'll try to find time to update my Scheme blog
with what I'm doing.

As always, comments and suggestions are welcome.

Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090816/26f637e0/attachment.html>

Posted on the users mailing list.