Re: Ideas for an advanced array procedure SRFI Bradley Lucier 26 Jan 2020 19:37 UTC
On 1/12/20 1:11 PM, Lucier, Bradley J wrote: > That being said, I tried my hand as implementing some of these > procedures using the updated SRFI-122. > > (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)))) With the new array-rotate and array-reduce procedures I just added to SRFI 179, this would be (define (array-inner-product sum product a1 a2) ;; Assumes that both a1 and a2 are both of ;; dimension higher than 1. ;; Otherwise, must use the array-reduce in the ;; following lambda. (array-outer-product (lambda (sub1 sub2) (array-reduce sum (array-map product sub1 sub2))) ;; make subarrays with the rightmost index (array-curry a1 1) ;; rotate to put the leftmost index on the right, ;; then make subarrays (array-curry (array-rotate a2 1) 1))) But neither code will work for the usual vector x vector -> scalar inner product. Brad