Proposal to allow a single infinity in the shape.  This model doesn't
consider empty arrays or other issues I'm unaware of.  If infinity is in
the shape, the product of the defined ranks must be a factor of the tally
of y .

reshape=: dyad define
 shape=. x
 if. _ -.@e. shape do.
  shape $ y
  return.
 end.
 error=. 13!:8
 valueError=. 21
 vector=. , y  NB. or <"_1 followed by unboxing
 if. 1 < +/ _ = shape do.
  'at most one replacement' error valueError
 end.
 f=. (# vector) % (*/ shape -. _)
 if. (~: <.) f do.  NB. f must be integral
  'shape mismatch' error valueError
 end.
 shape =. f (shape i. _)} shape
 shape $ vector
)
   _ 4 reshape i. 6
|shape mismatch: error
|   'shape mismatch'    error 21

   _ _ 2 reshape 'abcd'
|at most one replacement: error
|   'at most one replacement'    error 21

   2 _ reshape i. 6
0 1 2
3 4 5

   _ 2 reshape i. 6     NB.    _2 [\ i. 6
0 1
2 3
4 5

   _ 2 $ i.6
|domain error
|   _ 2    $i.6

   NB. while we're at it, permit negative shape values akin to integers
(i.).
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to