[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] array-lib : prepending unit axes

From: Kon Lovett
Subject: Re: [Chicken-users] array-lib : prepending unit axes
Date: Wed, 7 Nov 2007 18:35:45 -0800

On Nov 4, 2007, at 3:41 PM, Terrence Brannon wrote:

i wrote a function to prepend unit axes onto an array. this is
oftentimes necessary to get arrays to the same rank before applying
some sort of operation to them.

if there is a better way to do this, I'm all ears. Please, no comments
about my parenthesizing style :)

(define prepend-unit-axes
  (lambda (n a)
    (if (<= n 0)
        (let* (
               [ones (make-list n 1)]
               [new-dim (flatten ones (array-dimensions a))]
               [as-v (array->vector a)]
               [as-l (vector->list as-v)]
          (apply array '#() new-dim as-l)))))


(use srfi-1)

(define (prepend-unit-axes cnt arr)
  (if (positive? cnt)
      (apply vector->array (array->vector arr)
                           arr ; Use same storage as source array
(append! (make-list cnt 1) (array- dimensions arr)))
      arr ) )

Even faster:

(define (prepend-unit-axes cnt arr)
  (if (positive? cnt)
      (apply make-shared-array arr
                               (lambda idxs (drop idxs cnt))
(append! (make-list cnt 1) (array- dimensions arr)))
      arr ) )

Chicken-users mailing list

Best Wishes,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]