]> gitweb.factorcode.org Git - factor.git/blob - basis/math/matrices/elimination/elimination.factor
Factor source files should not be executable
[factor.git] / basis / math / matrices / elimination / elimination.factor
1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel locals math math.vectors math.matrices
4 namespaces sequences fry sorting ;
5 IN: math.matrices.elimination
6
7 SYMBOL: matrix
8
9 : with-matrix ( matrix quot -- )
10     [ swap matrix set call matrix get ] with-scope ; inline
11
12 : nth-row ( row# -- seq ) matrix get nth ;
13
14 : change-row ( row# quot: ( seq -- seq ) -- )
15     matrix get swap change-nth ; inline
16
17 : exchange-rows ( row# row# -- ) matrix get exchange ;
18
19 : rows ( -- n ) matrix get length ;
20
21 : cols ( -- n ) 0 nth-row length ;
22
23 : skip ( i seq quot -- n )
24     over [ find-from drop ] dip length or ; inline
25
26 : first-col ( row# -- n )
27     #! First non-zero column
28     0 swap nth-row [ zero? not ] skip ;
29
30 : clear-scale ( col# pivot-row i-row -- n )
31     [ over ] dip nth dup zero? [
32         3drop 0
33     ] [
34         [ nth dup zero? ] dip swap [
35             2drop 0
36         ] [
37             swap / neg
38         ] if
39     ] if ;
40
41 : (clear-col) ( col# pivot-row i -- )
42     [ [ clear-scale ] 2keep [ n*v ] dip v+ ] change-row ;
43
44 : rows-from ( row# -- slice )
45     rows dup <slice> ;
46
47 : clear-col ( col# row# rows -- )
48     [ nth-row ] dip [ [ 2dup ] dip (clear-col) ] each 2drop ;
49
50 : do-row ( exchange-with row# -- )
51     [ exchange-rows ] keep
52     [ first-col ] keep
53     dup 1 + rows-from clear-col ;
54
55 : pivot-row ( col# row# -- n )
56     rows-from swap '[ [ _ ] dip nth-row nth abs ] sort-with last ;
57
58 : (echelon) ( col# row# -- )
59     over cols < over rows < and [
60         2dup pivot-row [ over do-row 1 + ] when*
61         [ 1 + ] dip (echelon)
62     ] [
63         2drop
64     ] if ;
65
66 : echelon ( matrix -- matrix' )
67     [ 0 0 (echelon) ] with-matrix ;
68
69 : nonzero-rows ( matrix -- matrix' )
70     [ [ zero? ] all? not ] filter ;
71
72 : null/rank ( matrix -- null rank )
73     echelon dup length swap nonzero-rows length [ - ] keep ;
74
75 : leading ( seq -- n elt ) [ zero? not ] find ;
76
77 : reduced ( matrix' -- matrix'' )
78     [
79         rows <reversed> [
80             dup nth-row leading drop
81             dup [ swap dup clear-col ] [ 2drop ] if
82         ] each
83     ] with-matrix ;
84
85 :: basis-vector ( row col# -- )
86     row clone :> row'
87     col# row' nth neg recip :> a
88     0 col# row' set-nth
89     a row n*v col# matrix get set-nth ;
90
91 : nullspace ( matrix -- seq )
92     echelon reduced dup empty? [
93         dup first length identity-matrix [
94             [
95                 dup leading drop
96                 dup [ basis-vector ] [ 2drop ] if
97             ] each
98         ] with-matrix flip nonzero-rows
99     ] unless ;
100
101 : 1-pivots ( matrix -- matrix )
102     [ dup leading nip [ recip v*n ] when* ] map ;
103
104 : solution ( matrix -- matrix )
105     echelon nonzero-rows reduced 1-pivots ;
106
107 : inverse ( matrix -- matrix ) ! Assumes an invertible matrix
108     dup length
109     [ identity-matrix [ append ] 2map solution ] keep
110     [ tail ] curry map ;