chickadee » generalized-arrays » array-copy

array-copy array #!optional start endprocedure

Makes a copy of the array by allocating a new storage object, with the values copied into the new storage-object in lexicographic order.

This procedure is often most useful when one wants to pass the storage object of an array to some lower-level API (e.g. BLAS). Array views (i.e. satisfies array-view?) share the underlying storage object with an array allocated previously, and so need to be coerced into a newly allocated copy of the storage object if one wants to use the storage object with e.g. BLAS.

 
(import generalized-arrays
        blas
        srfi-4
        test)

(define order ColMajor)
(define transa NoTrans)

(define alpha 1)
(define beta 0)

(define a
  (make-array-from-storage
    f64vector-storage-class
    (vector 5 5)
    ;; Padded zeros to demonstrate how slicing might be wrong
    (f64vector 0 0 0 0 0
               0 1 1 3 5
               0 2 1 4 6
               0 3 1 5 7
               0 4 1 6 8)))

(define x (f64vector 1 2 1 1))
(define y (f64vector 0 0 0 0))

(define b
  (array-transpose
    (array-slice a #(1 1))))

;; WRONG! Do not do the following, since the storage object of `b` is the same
;; storage object of `a`. Instead, we need to first define a copy `c` that
;; copies the data so that the storage object has the correct layout.
;;
;; (dgemv! order transa 4 4 alpha (array-storage-object b) x beta y)

(define c (array-copy b))
(dgemv! order transa 4 4 alpha (array-storage-object a) x beta y)

(test "Result of multiplying the array c by the vector x"
      y
      (f64vector 12 5 22 32))