math.matrices: Add stitch. Add Kronecker product.
							parent
							
								
									bad2d7e499
								
							
						
					
					
						commit
						78f1ca9f14
					
				| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
 | 
					USING: math.matrices math.vectors tools.test math kernel ;
 | 
				
			||||||
IN: math.matrices.tests
 | 
					IN: math.matrices.tests
 | 
				
			||||||
USING: math.matrices math.vectors tools.test math ;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
[
 | 
					[
 | 
				
			||||||
    { { 0 } { 0 } { 0 } }
 | 
					    { { 0 } { 0 } { 0 } }
 | 
				
			||||||
| 
						 | 
					@ -199,3 +199,43 @@ USING: math.matrices math.vectors tools.test math ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[ { { 4181 6765 } { 6765 10946 } } ]
 | 
					[ { { 4181 6765 } { 6765 10946 } } ]
 | 
				
			||||||
[ { { 0 1 } { 1 1 } } 20 m^n ] unit-test
 | 
					[ { { 0 1 } { 1 1 } } 20 m^n ] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    { { 0 5 0 10 } { 6 7 12 14 } { 0 15 0 20 } { 18 21 24 28 } }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					[ { { 1 2 } { 3 4 } } { { 0 5 } { 6 7 } } kron ] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        { 1 1 1 1 }
 | 
				
			||||||
 | 
					        { 1 -1 1 -1 }
 | 
				
			||||||
 | 
					        { 1 1 -1 -1 }
 | 
				
			||||||
 | 
					        { 1 -1 -1 1 }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					} [ { { 1 1 } { 1 -1 } } dup kron ] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        { 1 1 1 1 1 1 1 1 }
 | 
				
			||||||
 | 
					        { 1 -1 1 -1 1 -1 1 -1 }
 | 
				
			||||||
 | 
					        { 1 1 -1 -1 1 1 -1 -1 }
 | 
				
			||||||
 | 
					        { 1 -1 -1 1 1 -1 -1 1 }
 | 
				
			||||||
 | 
					        { 1 1 1 1 -1 -1 -1 -1 }
 | 
				
			||||||
 | 
					        { 1 -1 1 -1 -1 1 -1 1 }
 | 
				
			||||||
 | 
					        { 1 1 -1 -1 -1 -1 1 1 }
 | 
				
			||||||
 | 
					        { 1 -1 -1 1 -1 1 1 -1 }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					} [ { { 1 1 } { 1 -1 } } dup dup kron kron ] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        { 1 1 1 1 1 1 1 1 }
 | 
				
			||||||
 | 
					        { 1 -1 1 -1 1 -1 1 -1 }
 | 
				
			||||||
 | 
					        { 1 1 -1 -1 1 1 -1 -1 }
 | 
				
			||||||
 | 
					        { 1 -1 -1 1 1 -1 -1 1 }
 | 
				
			||||||
 | 
					        { 1 1 1 1 -1 -1 -1 -1 }
 | 
				
			||||||
 | 
					        { 1 -1 1 -1 -1 1 -1 1 }
 | 
				
			||||||
 | 
					        { 1 1 -1 -1 -1 -1 1 1 }
 | 
				
			||||||
 | 
					        { 1 -1 -1 1 -1 1 1 -1 }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					} [ { { 1 1 } { 1 -1 } } dup dup kron swap kron ] unit-test
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -156,3 +156,10 @@ IN: math.matrices
 | 
				
			||||||
: m^n ( m n -- n ) 
 | 
					: m^n ( m n -- n ) 
 | 
				
			||||||
    make-bits over first length identity-matrix
 | 
					    make-bits over first length identity-matrix
 | 
				
			||||||
    [ [ dupd m. ] when [ dup m. ] dip ] reduce nip ;
 | 
					    [ [ dupd m. ] when [ dup m. ] dip ] reduce nip ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					: stitch ( m -- m' )
 | 
				
			||||||
 | 
					    [ ] [ [ append ] 2map ] map-reduce ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					: kron ( m1 m2 -- m )
 | 
				
			||||||
 | 
					    '[ [ _ n*m  ] map ] map stitch stitch ;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue