]> gitweb.factorcode.org Git - factor.git/commitdiff
debugging matrices
authorSlava Pestov <slava@factorcode.org>
Mon, 23 May 2005 04:25:52 +0000 (04:25 +0000)
committerSlava Pestov <slava@factorcode.org>
Mon, 23 May 2005 04:25:52 +0000 (04:25 +0000)
library/compiler/optimizer.factor
library/generic/generic.factor
library/inference/branches.factor
library/inference/words.factor
library/math/matrices.factor
library/test/inference.factor
library/test/math/matrices.factor

index 177919412991c9f9af398cc80eb31c1911567b7f..e7a27f2bb0d01b13048d6bf799305fb8d6eada0f 100644 (file)
@@ -197,7 +197,7 @@ M: #values can-kill* ( literal node -- ? )
 
 : branch-values ( branches -- )
     [ last-node node-in-d >list ] map
-    unify-lengths vector-transpose >list branch-returns set ;
+    unify-lengths dual branch-returns set ;
 
 : can-kill-branches? ( literal node -- ? )
     #! Check if the literal appears in either branch. This
index 7f85732efab8c73743c04560d5c0a66bf8469213..bee78566854191071ba701ee7464cc48ec04e03f 100644 (file)
@@ -63,13 +63,20 @@ math-internals ;
 
 : dispatcher% "dispatcher" word-prop % ;
 
+: error-method ( generic -- method )
+    [ literal, \ no-method , ] make-list ;
+
 : empty-method ( generic -- method )
-    [
-        [ dup delegate ] %
-        [ dup , ] make-list ,
-        [ literal, \ no-method , ] make-list ,
-        \ ?ifte ,
-    ] make-list ;
+    dup "picker" word-prop [ dup ] = [
+        [
+            [ dup delegate ] %
+            [ dup , ] make-list ,
+            error-method ,
+            \ ?ifte ,
+        ] make-list
+    ] [
+        error-method
+    ] ifte ;
 
 : <empty-vtable> ( generic -- vtable )
     empty-method num-types swap <repeated> >vector ;
index 781fc2f3bbb2be42081b5a9967d8fd84890023c2..7288634a0b114eae7d6c09953ce182fa1ca14c06 100644 (file)
@@ -28,16 +28,13 @@ sequences strings vectors words hashtables prettyprint ;
         [ value-class ] map class-or-list <computed>
     ] ifte ;
 
-: vector-transpose ( list -- vector )
-    #! Turn a list of same-length vectors into a vector of lists.
-    dup car length [
-        over [ nth ] map-with
-    ] project >vector nip ;
+: dual ( list -- list )
+    0 over nth length [ swap [ nth ] map-with ] project-with ;
 
 : unify-stacks ( list -- stack )
     #! Replace differing literals in stacks with unknown
     #! results.
-    unify-lengths vector-transpose [ unify-results ] map ; 
+    unify-lengths dual [ unify-results ] map >vector ; 
 
 : balanced? ( list -- ? )
     #! Check if a list of [[ instack outstack ]] pairs is
@@ -105,24 +102,13 @@ sequences strings vectors words hashtables prettyprint ;
     ] extend ;
 
 : (infer-branches) ( branchlist -- list )
-    [
-        [
-            inferring-base-case get [
-                [ infer-branch , ] [ [ drop ] when ] catch
-            ] [
-                infer-branch ,
-            ] ifte
-        ] each
-    ] make-list ;
+    [ infer-branch ] map dup unify-effects unify-dataflow ;
 
 : infer-branches ( branches node -- )
     #! Recursive stack effect inference is done here. If one of
     #! the branches has an undecidable stack effect, we set the
     #! base case to this stack effect and try again.
-    [
-        >r (infer-branches) dup unify-effects unify-dataflow
-        r> set-node-children
-    ] keep node, ;
+    [ >r (infer-branches) r> set-node-children ] keep node, ;
 
 \ ifte [
     2 #drop node, pop-d pop-d swap 2list
index 85e919102ac35b1e29ac128b3732148f453fb865..65d8fb4416e1c1e0babc57702e4adf4b350b8854 100644 (file)
@@ -144,7 +144,7 @@ M: compound apply-word ( word -- )
             nip consume/produce
         ] [
             inferring-base-case get [
-                drop no-base-case
+                2drop terminate
             ] [
                 car base-case
             ] ifte
index bebef9e469ebc195f5c7a2b90ac6e559627ec48b..b62ba3d9d396cab2e3cd56c5921e7dd4bbe79558 100644 (file)
@@ -4,9 +4,7 @@ IN: matrices
 USING: errors generic kernel lists math namespaces sequences
 vectors ;
 
-: n*v ( n vec -- vec )
-    #! Multiply a vector by a scalar.
-    [ * ] map-with ;
+: n*v ( n vec -- vec ) [ * ] map-with ;
 
 ! Vector operations
 : v+ ( v v -- v ) [ + ] 2map ;
@@ -15,7 +13,7 @@ vectors ;
 : v** ( v v -- v ) [ conjugate * ] 2map ;
 
 ! Later, this will fixed when 2each works properly
-! : v. ( v v -- x ) 0 swap [ * + ] 2each ;
+! : v. ( v v -- x ) 0 swap [ conjugate * + ] 2each ;
 : v. ( v v -- x ) v** 0 swap [ + ] each ;
 
 : cross-trace ( v1 v2 i1 i2 -- v1 v2 n )
@@ -44,7 +42,7 @@ M: matrix clone ( matrix -- matrix )
     clone-tuple
     dup matrix-sequence clone over set-matrix-sequence ;
 
-: matrix@ ( row col matrix -- n ) matrix-rows * + ;
+: matrix@ ( row col matrix -- n ) matrix-cols rot * + ;
 
 : matrix-get ( row col matrix -- elt )
     [ matrix@ ] keep matrix-sequence nth ;
@@ -124,7 +122,7 @@ M: col thaw >vector ;
     #! Composition of two matrices.
     2dup *check 2dup *dimensions [
         ( m1 m2 row col -- m1 m2 )
-        >r >r 2dup r> rot <row> r> rot <col> v.
+        pick <col> >r pick <row> r> v.
     ] make-matrix 2nip ;
 
 : n*m ( n m -- m )
index 6da039ec12fd4e037cea472090b7ba665880d192..0a725116c4b8ee12c90abf3b9872effe9841db6f 100644 (file)
@@ -64,7 +64,7 @@ namespaces parser sequences test vectors ;
 
 : infinite-loop infinite-loop ;
 
-[ [ infinite-loop ] infer old-effect ] unit-test-fails
+[ [ infinite-loop ] infer old-effect ] unit-test-fails
 
 : simple-recursion-1
     dup [ simple-recursion-1 ] [ ] ifte ;
index 26d98af44481b23f05d1100981a5f203563c6458..f965b98c4c2fa782f723ce7e5c3be9afc3634c71 100644 (file)
@@ -1,6 +1,9 @@
 IN: temporary
 USING: kernel lists math matrices namespaces test ;
 
+[ [ [ 1 4 ] [ 2 5 ] [ 3 6 ] ] ]
+[ M[ [ 1 4 ] [ 2 5 ] [ 3 6 ] ]M row-list ] unit-test
+
 [
     M[ [ 0 ] [ 0 ] [ 0 ] ]M
 ] [
@@ -103,6 +106,26 @@ USING: kernel lists math matrices namespaces test ;
 [ { 1 0 0 } ] [ { 0 1 0 } { 0 0 1 } cross ] unit-test
 [ { 0 1 0 } ] [ { 0 0 1 } { 1 0 0 } cross ] unit-test
 
+[ M[ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]M ]
+[ M[ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]M transpose transpose ]
+unit-test
+
+[ M[ [ 1 3 5 ] [ 2 4 6 ] ]M ]
+[ M[ [ 1 3 5 ] [ 2 4 6 ] ]M transpose transpose ]
+unit-test
+
 [ M[ [ 1 3 5 ] [ 2 4 6 ] ]M ]
 [ M[ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]M transpose ]
 unit-test
+
+[
+    M[ [ 28 ] ]M
+] [
+    M[ [ 2 4 6 ] ]M
+
+    M[ [ 1 ]
+       [ 2 ]
+       [ 3 ] ]M
+    
+    m.
+] unit-test