| 
									
										
										
										
											2010-01-14 10:10:13 -05:00
										 |  |  | ! Copyright (C) 2005, 2010 Slava Pestov, Joe Groff. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-07-02 20:05:24 -04:00
										 |  |  | USING: accessors arrays columns kernel locals math math.bits | 
					
						
							|  |  |  | math.functions math.order math.vectors sequences | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | sequences.private fry math.statistics grouping | 
					
						
							|  |  |  | combinators.short-circuit math.ranges combinators.smart ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: math.matrices | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Matrices | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : make-matrix ( m n quot -- matrix )
 | 
					
						
							|  |  |  |     '[ _ _ replicate ] replicate ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <matrix> ( m n element -- matrix )
 | 
					
						
							|  |  |  |     '[ _ _ <array> ] replicate ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : zero-matrix ( m n -- matrix )
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  |     0 <matrix> ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-04 14:23:38 -04:00
										 |  |  | : diagonal-matrix ( diagonal-seq -- matrix )
 | 
					
						
							|  |  |  |     dup length dup zero-matrix | 
					
						
							|  |  |  |     [ '[ dup _ nth set-nth ] each-index ] keep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : identity-matrix ( n -- matrix )
 | 
					
						
							| 
									
										
										
										
											2012-05-04 14:23:38 -04:00
										 |  |  |     1 <repetition> diagonal-matrix ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : eye ( m n k -- matrix )
 | 
					
						
							|  |  |  |     [ [ iota ] bi@ ] dip neg '[ _ + = 1 0 ? ] cartesian-map ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-04 21:01:21 -04:00
										 |  |  | : hilbert-matrix ( m n -- matrix )
 | 
					
						
							|  |  |  |     [ iota ] bi@ [ + 1 + recip ] cartesian-map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : toeplitz-matrix ( n -- matrix )
 | 
					
						
							|  |  |  |     iota dup [ - abs 1 + ] cartesian-map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : hankel-matrix ( n -- matrix )
 | 
					
						
							|  |  |  |     [ iota dup ] keep '[ + abs 1 + dup _ > [ drop 0 ] when ] cartesian-map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : box-matrix ( r -- matrix )
 | 
					
						
							|  |  |  |     2 * 1 + dup '[ _ 1 <array> ] replicate ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-30 19:44:08 -04:00
										 |  |  | : vandermonde-matrix ( u n -- matrix )
 | 
					
						
							| 
									
										
										
										
											2012-07-30 19:15:21 -04:00
										 |  |  |     iota [ v^n ] with map reverse flip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-02 20:05:24 -04:00
										 |  |  | :: rotation-matrix3 ( axis theta -- matrix )
 | 
					
						
							|  |  |  |     theta cos :> c | 
					
						
							|  |  |  |     theta sin :> s | 
					
						
							| 
									
										
										
										
											2009-10-28 17:11:33 -04:00
										 |  |  |     axis first3 :> ( x y z )
 | 
					
						
							| 
									
										
										
										
											2009-07-02 20:05:24 -04:00
										 |  |  |     x sq 1.0 x sq - c * +     x y * 1.0 c - * z s * -   x z * 1.0 c - * y s * + 3array
 | 
					
						
							|  |  |  |     x y * 1.0 c - * z s * +   y sq 1.0 y sq - c * +     y z * 1.0 c - * x s * - 3array
 | 
					
						
							|  |  |  |     x z * 1.0 c - * y s * -   y z * 1.0 c - * x s * +   z sq 1.0 z sq - c * +   3array
 | 
					
						
							|  |  |  |     3array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: rotation-matrix4 ( axis theta -- matrix )
 | 
					
						
							|  |  |  |     theta cos :> c | 
					
						
							|  |  |  |     theta sin :> s | 
					
						
							| 
									
										
										
										
											2009-10-28 17:11:33 -04:00
										 |  |  |     axis first3 :> ( x y z )
 | 
					
						
							| 
									
										
										
										
											2009-07-02 20:05:24 -04:00
										 |  |  |     x sq 1.0 x sq - c * +     x y * 1.0 c - * z s * -   x z * 1.0 c - * y s * +   0 4array
 | 
					
						
							|  |  |  |     x y * 1.0 c - * z s * +   y sq 1.0 y sq - c * +     y z * 1.0 c - * x s * -   0 4array
 | 
					
						
							|  |  |  |     x z * 1.0 c - * y s * -   y z * 1.0 c - * x s * +   z sq 1.0 z sq - c * +     0 4array
 | 
					
						
							|  |  |  |     { 0.0 0.0 0.0 1.0 } 4array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: translation-matrix4 ( offset -- matrix )
 | 
					
						
							| 
									
										
										
										
											2009-10-28 17:11:33 -04:00
										 |  |  |     offset first3 :> ( x y z )
 | 
					
						
							| 
									
										
										
										
											2009-07-02 20:05:24 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         { 1.0 0.0 0.0 x   } | 
					
						
							|  |  |  |         { 0.0 1.0 0.0 y   } | 
					
						
							|  |  |  |         { 0.0 0.0 1.0 z   } | 
					
						
							|  |  |  |         { 0.0 0.0 0.0 1.0 } | 
					
						
							|  |  |  |     } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >scale-factors ( number/sequence -- x y z )
 | 
					
						
							|  |  |  |     dup number? [ dup dup ] [ first3 ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: scale-matrix3 ( factors -- matrix )
 | 
					
						
							| 
									
										
										
										
											2009-10-28 17:11:33 -04:00
										 |  |  |     factors >scale-factors :> ( x y z )
 | 
					
						
							| 
									
										
										
										
											2009-07-02 20:05:24 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         { x   0.0 0.0 } | 
					
						
							|  |  |  |         { 0.0 y   0.0 } | 
					
						
							|  |  |  |         { 0.0 0.0 z   } | 
					
						
							|  |  |  |     } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: scale-matrix4 ( factors -- matrix )
 | 
					
						
							| 
									
										
										
										
											2009-10-28 17:11:33 -04:00
										 |  |  |     factors >scale-factors :> ( x y z )
 | 
					
						
							| 
									
										
										
										
											2009-07-02 20:05:24 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         { x   0.0 0.0 0.0 } | 
					
						
							|  |  |  |         { 0.0 y   0.0 0.0 } | 
					
						
							|  |  |  |         { 0.0 0.0 z   0.0 } | 
					
						
							|  |  |  |         { 0.0 0.0 0.0 1.0 } | 
					
						
							|  |  |  |     } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ortho-matrix4 ( dim -- matrix )
 | 
					
						
							|  |  |  |     [ recip ] map scale-matrix4 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: frustum-matrix4 ( xy-dim near far -- matrix )
 | 
					
						
							| 
									
										
										
										
											2009-10-28 17:11:33 -04:00
										 |  |  |     xy-dim first2 :> ( x y )
 | 
					
						
							| 
									
										
										
										
											2009-07-02 20:05:24 -04:00
										 |  |  |     near x /f :> xf | 
					
						
							|  |  |  |     near y /f :> yf | 
					
						
							|  |  |  |     near far + near far - /f :> zf | 
					
						
							|  |  |  |     2 near far * * near far - /f :> wf | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { xf  0.0  0.0 0.0 } | 
					
						
							|  |  |  |         { 0.0 yf   0.0 0.0 } | 
					
						
							|  |  |  |         { 0.0 0.0  zf  wf  } | 
					
						
							|  |  |  |         { 0.0 0.0 -1.0 0.0 } | 
					
						
							|  |  |  |     } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-04 20:13:31 -04:00
										 |  |  | :: skew-matrix4 ( theta -- matrix )
 | 
					
						
							|  |  |  |     theta tan :> zf | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { 1.0 0.0 0.0 0.0 } | 
					
						
							|  |  |  |         { 0.0 1.0 0.0 0.0 } | 
					
						
							|  |  |  |         { 0.0 zf  1.0 0.0 } | 
					
						
							|  |  |  |         { 0.0 0.0 0.0 1.0 } | 
					
						
							|  |  |  |     } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Matrix operations | 
					
						
							|  |  |  | : mneg ( m -- m ) [ vneg ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-05 11:42:50 -05:00
										 |  |  | : n+m  ( n m -- m ) [ n+v ] with map ;
 | 
					
						
							|  |  |  | : m+n  ( m n -- m ) [ v+n ] curry map ;
 | 
					
						
							|  |  |  | : n-m  ( n m -- m ) [ n-v ] with map ;
 | 
					
						
							|  |  |  | : m-n  ( m n -- m ) [ v-n ] curry map ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  | : n*m ( n m -- m ) [ n*v ] with map ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : m*n ( m n -- m ) [ v*n ] curry map ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  | : n/m ( n m -- m ) [ n/v ] with map ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : m/n ( m n -- m ) [ v/n ] curry map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : m+   ( m m -- m ) [ v+ ] 2map ;
 | 
					
						
							|  |  |  | : m-   ( m m -- m ) [ v- ] 2map ;
 | 
					
						
							|  |  |  | : m*   ( m m -- m ) [ v* ] 2map ;
 | 
					
						
							|  |  |  | : m/   ( m m -- m ) [ v/ ] 2map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-23 04:57:05 -04:00
										 |  |  | : v.m ( v m -- v ) flip [ v. ] with map ;
 | 
					
						
							|  |  |  | : m.v ( m v -- v ) [ v. ] curry map ;
 | 
					
						
							|  |  |  | : m.  ( m m -- m ) flip [ swap m.v ] curry map ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-29 23:54:11 -04:00
										 |  |  | : m~  ( m m epsilon -- ? ) [ v~ ] curry 2all? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-07 01:24:32 -05:00
										 |  |  | : mmin ( m -- n ) [ 1/0. ] dip [ [ min ] each ] each ;
 | 
					
						
							|  |  |  | : mmax ( m -- n ) [ -1/0. ] dip [ [ max ] each ] each ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : mnorm ( m -- n ) dup mmax abs m/n ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-22 17:26:25 -04:00
										 |  |  | : cross ( vec1 vec2 -- vec3 )
 | 
					
						
							| 
									
										
										
										
											2010-02-25 11:15:53 -05:00
										 |  |  |     [ [ { 1 2 0 } vshuffle ] [ { 2 0 1 } vshuffle ] bi* v* ] | 
					
						
							|  |  |  |     [ [ { 2 0 1 } vshuffle ] [ { 1 2 0 } vshuffle ] bi* v* ] 2bi v- ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-23 23:41:08 -04:00
										 |  |  | :: normal ( vec1 vec2 vec3 -- vec4 )
 | 
					
						
							|  |  |  |     vec2 vec1 v- vec3 vec1 v- cross normalize ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : proj ( v u -- w )
 | 
					
						
							| 
									
										
										
										
											2008-03-13 04:36:13 -04:00
										 |  |  |     [ [ v. ] [ norm-sq ] bi / ] keep n*v ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-25 11:15:53 -05:00
										 |  |  | : perp ( v u -- w )
 | 
					
						
							|  |  |  |     dupd proj v- ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : angle-between ( v u -- a )
 | 
					
						
							| 
									
										
										
										
											2012-03-29 13:56:39 -04:00
										 |  |  |     [ normalize ] bi@ h. acos ;
 | 
					
						
							| 
									
										
										
										
											2010-02-25 11:15:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : (gram-schmidt) ( v seq -- newseq )
 | 
					
						
							|  |  |  |     [ dupd proj v- ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gram-schmidt ( seq -- orthogonal )
 | 
					
						
							|  |  |  |     V{ } clone [ over (gram-schmidt) over push ] reduce ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : norm-gram-schmidt ( seq -- orthonormal )
 | 
					
						
							|  |  |  |     gram-schmidt [ normalize ] map ;
 | 
					
						
							| 
									
										
										
										
											2009-03-27 19:31:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-04 23:42:29 -04:00
										 |  |  | : m^n ( m n -- n )  | 
					
						
							|  |  |  |     make-bits over first length identity-matrix | 
					
						
							| 
									
										
										
										
											2009-09-29 23:54:11 -04:00
										 |  |  |     [ [ dupd m. ] when [ dup m. ] dip ] reduce nip ;
 | 
					
						
							| 
									
										
										
										
											2012-05-24 11:52:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : stitch ( m -- m' )
 | 
					
						
							|  |  |  |     [ ] [ [ append ] 2map ] map-reduce ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : kron ( m1 m2 -- m )
 | 
					
						
							|  |  |  |     '[ [ _ n*m  ] map ] map stitch stitch ;
 | 
					
						
							| 
									
										
										
										
											2012-08-08 20:02:39 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : outer ( u v -- m )
 | 
					
						
							|  |  |  |     [ n*v ] curry map ;
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : row ( n matrix -- col )
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  |     nth ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : rows ( seq matrix -- cols )
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  |     '[ _ row ] map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : col ( n matrix -- col )
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  |     swap '[ _ swap nth ] map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : cols ( seq matrix -- cols )
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  |     '[ _ col ] map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : set-index ( object pair matrix -- )
 | 
					
						
							|  |  |  |     [ first2 swap ] dip nth set-nth ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : set-indices ( object sequence matrix -- )
 | 
					
						
							|  |  |  |     '[ _ set-index ] with each ; inline
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : matrix-map ( matrix quot -- )
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  |     '[ _ map ] map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : column-map ( matrix quot -- seq )
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  |     [ [ first length iota ] keep ] dip '[ _ col @ ] map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : cartesian-square-indices ( n -- matrix )
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  |     iota dup cartesian-product ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : cartesian-matrix-map ( matrix quot -- matrix' )
 | 
					
						
							|  |  |  |     [ [ first length cartesian-square-indices ] keep ] dip
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  |     '[ _ @ ] matrix-map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : cartesian-matrix-column-map ( matrix quot -- matrix' )
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:53:24 -04:00
										 |  |  |     [ cols first2 ] prepose cartesian-matrix-map ; inline
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:59:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : cov-matrix-ddof ( matrix ddof -- cov )
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:59:47 -04:00
										 |  |  |     '[ _ cov-ddof ] cartesian-matrix-column-map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : cov-matrix ( matrix -- cov ) 0 cov-matrix-ddof ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : sample-cov-matrix ( matrix -- cov ) 1 cov-matrix-ddof ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: square-rows ( object -- matrix )
 | 
					
						
							|  |  |  | M: integer square-rows iota square-rows ;
 | 
					
						
							|  |  |  | M: sequence square-rows dup [ nip ] cartesian-map ;  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: square-cols ( object -- matrix )
 | 
					
						
							|  |  |  | M: integer square-cols iota square-cols ;
 | 
					
						
							|  |  |  | M: sequence square-cols dup [ drop ] cartesian-map ;  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : make-matrix-with-indices ( m n quot -- matrix )
 | 
					
						
							|  |  |  |     [ [ iota ] bi@ ] dip '[ @ ] cartesian-map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : null-matrix? ( matrix -- ? ) empty? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : well-formed-matrix? ( matrix -- ? )
 | 
					
						
							|  |  |  |     dup null-matrix? [ | 
					
						
							|  |  |  |         drop t
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         [ ] [ first length ] bi
 | 
					
						
							|  |  |  |         '[ length _ = ] all?
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : dim ( matrix -- pair/f )
 | 
					
						
							|  |  |  |     [ 2 0 <array> ] | 
					
						
							|  |  |  |     [ [ length ] [ first length ] bi 2array ] if-empty ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : square-matrix? ( matrix -- ? )
 | 
					
						
							|  |  |  |     { [ well-formed-matrix? ] [ dim all-eq? ] } 1&& ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : matrix-coordinates ( dim -- coordinates )
 | 
					
						
							|  |  |  |     first2 [ iota ] bi@ cartesian-product ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : dimension-range ( matrix -- dim range )
 | 
					
						
							|  |  |  |     dim [ matrix-coordinates ] [ first [1,b] ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : upper-matrix-indices ( matrix -- matrix' )
 | 
					
						
							|  |  |  |     dimension-range <reversed> [ tail-slice* >array ] 2map concat ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : lower-matrix-indices ( matrix -- matrix' )
 | 
					
						
							|  |  |  |     dimension-range [ head-slice >array ] 2map concat ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:59:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : make-lower-matrix ( object m n -- matrix )
 | 
					
						
							|  |  |  |     zero-matrix [ lower-matrix-indices ] [ set-indices ] [ ] tri ;
 | 
					
						
							| 
									
										
										
										
											2012-10-02 20:59:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 14:35:13 -04:00
										 |  |  | : make-upper-matrix ( object m n -- matrix )
 | 
					
						
							|  |  |  |     zero-matrix [ upper-matrix-indices ] [ set-indices ] [ ] tri ;
 |