Given a noun  V  , which is either scalar or vector (i.e.   2 > # $ V  ), and a 
vector of integers  ts  , provide an elegant 
expression to make  ts  the trailing shape of  V  :

           A  =.  ts reshape V
           (#V) -: */$A
        1


           'N TS' =.  ({. ; }.) $ A
           TS -: ts
        1


           N -: # A
        1
           N -: (*/ts) %~ # V
        1
           A -: (N,ts) $ V
        1

Constraints:  

 *  Assume that  V  will fit evenly into  ts   .  That is:
  
           0 -: (*/ts) | # V  .
        1

 *  You may not calculate  ($V)  or  N   directly.  That is,
    for:

           ts =.  ,2
           V  =.  i. 10

    This expression is appropriate:

           A  =.  (-{.ts) ]\ V
           
    but this is not:

           A  =.  (ts ,~ (*/ts) %~ # V) $ V

 *  The usual constraints regarding shortness of expression
    in both words and characters, efficiency in both time and
    space, and the cool factor apply.

-Dan
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to