-
Notifications
You must be signed in to change notification settings - Fork 0
/
lufact.lisp
168 lines (150 loc) · 5.41 KB
/
lufact.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
(in-package :rationalsimplex)
;;;;; LU factorization of the basis
;;;;;
;;;;; Factorizes B from scratch, by gaussian elimination
;;;; Makes new L-eta matrix, if necessary
(defun lu-split-column (bm pivot-i pivot-j k pivot-ci)
(let* ((i->pi (basis-matrix-i->pi bm))
(fill-in-counter (aref (basis-matrix-fill-ins bm) pivot-j))
(u (aref (basis-matrix-u-columns bm) pivot-j))
(u-is (hsv-is u))
(u-vis (hsv-vis u))
(u-seq (aref (basis-matrix-u-seqs bm) pivot-j))
(l nil)
(lf nil))
;; re-order fill-in
(dotimes (fill-in-k fill-in-counter)
(let ((ci (+ (- (hsv-length u) fill-in-counter) fill-in-k)))
(loop
(cond ((zerop ci)
(return))
((< (aref u-is ci) (aref u-is (- ci 1)))
(rotatef (aref u-is ci) (aref u-is (- ci 1)))
(rotatef (aref u-vis ci) (aref u-vis (- ci 1)))
(cond ((= ci pivot-ci)
(decf pivot-ci))
((= (- ci 1) pivot-ci)
(incf pivot-ci)))
(decf ci))
(t
(return))))))
;; build l
(dotimes (ci (hsv-length u))
(let* ((i (aref u-is ci))
(ip (aref i->pi i))
(vi (aref u-vis ci)))
(cond ((zerop vi))
((< ip k))
((= ip k)
(assert (= ci pivot-ci)))
((not l)
(setf l (aref (basis-matrix-l-file bm) (basis-matrix-n-l-file bm))
lf (aref (basis-matrix-lf-file bm) (basis-matrix-n-l-file bm)))
(setf (aref (basis-matrix-l-pivot-file bm) (basis-matrix-n-l-file bm)) pivot-i)
(incf (basis-matrix-n-l-file bm))
(incf (basis-matrix-n-l-factor-file bm))
(setf (hsv-coef l) (/ 1 (aref u-vis pivot-ci)))
(hsv-add i (- vi) l)
(setf (aref u-vis ci) 0))
(t
(hsv-add i (- vi) l)
(setf (aref u-vis ci) 0)))))
;; rebuild l and u
(when l
(hsv-normalize l)
(copy-hsv-into-hsv-float l lf))
(hsv-remove-zeros u)
(hsv-normalize u)
(copy-hsv-into-hsv-float u (aref (basis-matrix-uf-columns bm) pivot-j))
(dotimes (ci (hsv-length u))
(setf (aref u-seq ci) ci))
(sort-increasing-bounded u-seq (hsv-length u)
#'(lambda (k) (aref i->pi (aref (hsv-is u) k))))
;; return l index
(if l
(- (basis-matrix-n-l-file bm) 1)
-1)))
;;;; Auxilliary function for basis-matrix-lu-factorization
(defun lu-prepare-update (b pivot-l k)
(let ((refs (basis-matrix-refs b))
(m (basis-matrix-size b)))
(loop for qi from k below m
do (setf (aref refs qi) -1))
(let* ((l-is (hsv-is pivot-l))
(l-n-nz (hsv-length pivot-l)))
(dotimes (index l-n-nz)
(setf (aref refs (aref (basis-matrix-i->pi b) (aref l-is index)))
index)))))
;;;; Updates column in residual matrix after pivot
(defun lu-update-right (bm pivot-l pivot-pj j index-j-pivot-i)
(let* ((u (aref (basis-matrix-u-columns bm) j))
(m (basis-matrix-size bm))
(flags (basis-matrix-flags bm))
(refs (basis-matrix-refs bm))
(pivot-coef (hsv-coef pivot-l))
(pivot-n (numerator pivot-coef))
(pivot-d (denominator pivot-coef)))
(loop for ip from pivot-pj below m do
(setf (aref flags ip) nil))
;; update coef on j
(divf (hsv-coef u) pivot-d)
;; do rows with nonzeros
(dotimes (index (hsv-length u))
(let* ((i (aref (hsv-is u) index))
(ip (aref (basis-matrix-i->pi bm) i)))
;; update rows pi /= pivot-pj (is nonzero)
(unless (= pivot-pj ip)
(mulf (aref (hsv-vis u) index) pivot-d))
;; update rows pi > pivot-pj with nonzeros
(when (< pivot-pj ip)
(setf (aref flags ip) t)
(let ((index-i-pivot-j (aref refs ip)))
(unless (= -1 index-i-pivot-j)
(incf (aref (hsv-vis u) index)
(* pivot-n
(aref (hsv-vis u) index-j-pivot-i)
(aref (hsv-vis pivot-l) index-i-pivot-j))))))))
;; do fill-in on rows pi > pivot-pj
(loop for ip from (+ pivot-pj 1) below m
do (let ((i (aref (basis-matrix-pi->i bm) ip))
(index-i-pivot-j (aref refs ip)))
(unless (or (= -1 index-i-pivot-j)
(aref flags ip))
;; perform fill-in
(let ((val (* pivot-n
(aref (hsv-vis u) index-j-pivot-i)
(aref (hsv-vis pivot-l) index-i-pivot-j))))
(unless (zerop val)
(pivot-add bm i j (hsv-length u))
(hsv-add i val u)
(incf (aref (basis-matrix-fill-ins bm) j)))))))
;; update row pivot-pj (is nonzero)
(mulf (aref (hsv-vis u) index-j-pivot-i) pivot-d)
;; normalize j
(hsv-normalize u)))
;;;; LU factorization
;;;; returns t on success, nil if basis matrix is singular
(defun basis-matrix-lu-factorization (bm)
(dotimes (k (basis-matrix-size bm) t)
;; select and perform pivot
(multiple-value-bind (pivot-i pivot-j pivot-ci pivot-row-nnz)
(basis-matrix-perform-pivot bm k)
(when (= -1 pivot-j)
(return)) ; pivot unsuccessful
(assert (not (basis-matrix-is-singular bm)))
(assert (= pivot-i (aref (basis-matrix-pi->i bm) k)))
(assert (= pivot-j (aref (basis-matrix-pj->j bm) k)))
;; make L eta matrix, if necessary
(let ((l-file-index (lu-split-column bm pivot-i pivot-j k pivot-ci)))
(unless (= -1 l-file-index)
;; update remaining columns
(let* ((pivot-l (aref (basis-matrix-l-file bm) l-file-index))
(pivot-row-js (aref (basis-matrix-row-js bm) pivot-i))
(pivot-row-cis (aref (basis-matrix-row-cis bm) pivot-i)))
(unless (= 1 pivot-row-nnz)
(lu-prepare-update bm pivot-l k)
(dotimes (index pivot-row-nnz)
(let ((j (aref pivot-row-js index))
(ci (aref pivot-row-cis index)))
(unless (= j pivot-j)
(lu-update-right bm pivot-l k j ci)))))))))))