Re: Ideas for an advanced array procedure SRFI
Bradley Lucier 12 Jan 2020 18:11 UTC
John:
Thank you for putting together this list.
I would prefer to consider adding new procedures to SRFI-122bis as part
of the discussion of the new SRFI.
That being said, I tried my hand as implementing some of these
procedures using the updated SRFI-122. There is still some confusion in
my mind about what the various procedures should do (I looked up the APL
document and couldn't immediately find their equivalents), here is what
I came up with (modulo proper error checking):
(define (array-fold-on proc knil a dim)
;; curries a, with inner dimension dim,
;; and folds proc on outer array elements
(array-fold proc knil (array-curry a dim)))
;;; or
(define (array-fold-on proc knil a dim)
;; curries a, with inner dimension dim
;; and returns a new array whose elements
;; are fold applied to the inner arrays
(array-map (lambda (subarray)
(array-fold proc knil subarray))
(array-curry a dim)))
(define (array-inner-product sum sum-knil product a1 a2)
(let ((rotated-a2
;; move the first index of a2 to the end,
;; keeping all others the same
(let* ((n2 (array-dimension a2))
(permutation (make-vector n2)))
(do ((i 1 (fx+ i 1)))
((fx= i n2)
(vector-set! permutation (fx- n2 1) 0))
(vector-set! permutation (fx- i 1) i))
(array-permute a2 permutation))))
(array-outer-product
(lambda (sub1 sub2)
(array-fold sum sum-knil
(array-map product sub1 sub2)))
(array-curry a1 1)
(array-curry rotated-a2 1))))
(define (recursive-array->list a)
;; assumes a is an array
(define (helper a)
;; a need not be an array
(if (array? a)
(array->list
(array-map recursive-array->list a))
a))
(helper a)
(define (array-depth a)
(if (array? a)
(fx+ 1 (array-fold fxmax 0 (array-map array-depth a)))
0))
This exercise suggests that (array-rotate a dim) would shorten
array-inner-product and might be more generally useful as a special case
of array-permute. Otherwise the definitions are rather short.
Brad