 
 GENERIC: square-rows ( object -- matrix )
 M: integer square-rows iota square-rows ;
-M: sequence square-rows dup [ nip ] cartesian-map ; 
+M: sequence square-rows
+    >array [ length ] keep [ clone ] curry { } replicate-as ;
 
 GENERIC: square-cols ( object -- matrix )
 M: integer square-cols iota square-cols ;
-M: sequence square-cols dup [ drop ] cartesian-map ; 
+M: sequence square-cols
+    [ length ] keep [ <array> ] with { } map-as ;
 
 : make-matrix-with-indices ( m n quot -- matrix )
     [ [ iota ] bi@ ] dip '[ @ ] cartesian-map ; inline
 
-: null-matrix? ( matrix -- ? ) empty? ;
+: null-matrix? ( matrix -- ? ) empty? ; inline
 
 : well-formed-matrix? ( matrix -- ? )
-    dup null-matrix? [
-        drop t
-    ] [
+    [ t ] [
         [ ] [ first length ] bi
         '[ length _ = ] all?
-    ] if ;
+    ] if-empty ;
 
 : dim ( matrix -- pair/f )
     [ 2 0 <array> ]


: set-indices ( object sequence matrix -- )
    '[ _ set-index ] with each ; inline
    

: matrix-map ( matrix quot -- )
    '[ _ map ] map ; inline

: norm-gram-schmidt ( seq -- orthonormal )
    gram-schmidt [ normalize ] map ;

: m^n ( m n -- n )
    make-bits over first length identity-matrix
    [ [ dupd m. ] when [ dup m. ] dip ] reduce nip ;

