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

2007-11-07 Thread Kon Lovett


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)
a
(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)



Faster:

(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
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Best Wishes,
Kon




___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


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

2007-11-04 Thread Terrence Brannon
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)
a
(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)


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users