alpar@1
|
1 |
/* glpluf.h (LU-factorization) */
|
alpar@1
|
2 |
|
alpar@1
|
3 |
/***********************************************************************
|
alpar@1
|
4 |
* This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@1
|
5 |
*
|
alpar@1
|
6 |
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@1
|
7 |
* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
|
alpar@1
|
8 |
* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@1
|
9 |
* E-mail: <mao@gnu.org>.
|
alpar@1
|
10 |
*
|
alpar@1
|
11 |
* GLPK is free software: you can redistribute it and/or modify it
|
alpar@1
|
12 |
* under the terms of the GNU General Public License as published by
|
alpar@1
|
13 |
* the Free Software Foundation, either version 3 of the License, or
|
alpar@1
|
14 |
* (at your option) any later version.
|
alpar@1
|
15 |
*
|
alpar@1
|
16 |
* GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@1
|
17 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@1
|
18 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@1
|
19 |
* License for more details.
|
alpar@1
|
20 |
*
|
alpar@1
|
21 |
* You should have received a copy of the GNU General Public License
|
alpar@1
|
22 |
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@1
|
23 |
***********************************************************************/
|
alpar@1
|
24 |
|
alpar@1
|
25 |
#ifndef GLPLUF_H
|
alpar@1
|
26 |
#define GLPLUF_H
|
alpar@1
|
27 |
|
alpar@1
|
28 |
/***********************************************************************
|
alpar@1
|
29 |
* The structure LUF defines LU-factorization of a square matrix A and
|
alpar@1
|
30 |
* is the following quartet:
|
alpar@1
|
31 |
*
|
alpar@1
|
32 |
* [A] = (F, V, P, Q), (1)
|
alpar@1
|
33 |
*
|
alpar@1
|
34 |
* where F and V are such matrices that
|
alpar@1
|
35 |
*
|
alpar@1
|
36 |
* A = F * V, (2)
|
alpar@1
|
37 |
*
|
alpar@1
|
38 |
* and P and Q are such permutation matrices that the matrix
|
alpar@1
|
39 |
*
|
alpar@1
|
40 |
* L = P * F * inv(P) (3)
|
alpar@1
|
41 |
*
|
alpar@1
|
42 |
* is lower triangular with unity diagonal, and the matrix
|
alpar@1
|
43 |
*
|
alpar@1
|
44 |
* U = P * V * Q (4)
|
alpar@1
|
45 |
*
|
alpar@1
|
46 |
* is upper triangular. All the matrices have the order n.
|
alpar@1
|
47 |
*
|
alpar@1
|
48 |
* Matrices F and V are stored in row- and column-wise sparse format
|
alpar@1
|
49 |
* as row and column linked lists of non-zero elements. Unity elements
|
alpar@1
|
50 |
* on the main diagonal of matrix F are not stored. Pivot elements of
|
alpar@1
|
51 |
* matrix V (which correspond to diagonal elements of matrix U) are
|
alpar@1
|
52 |
* stored separately in an ordinary array.
|
alpar@1
|
53 |
*
|
alpar@1
|
54 |
* Permutation matrices P and Q are stored in ordinary arrays in both
|
alpar@1
|
55 |
* row- and column-like formats.
|
alpar@1
|
56 |
*
|
alpar@1
|
57 |
* Matrices L and U are completely defined by matrices F, V, P, and Q
|
alpar@1
|
58 |
* and therefore not stored explicitly.
|
alpar@1
|
59 |
*
|
alpar@1
|
60 |
* The factorization (1)-(4) is a version of LU-factorization. Indeed,
|
alpar@1
|
61 |
* from (3) and (4) it follows that:
|
alpar@1
|
62 |
*
|
alpar@1
|
63 |
* F = inv(P) * L * P,
|
alpar@1
|
64 |
*
|
alpar@1
|
65 |
* U = inv(P) * U * inv(Q),
|
alpar@1
|
66 |
*
|
alpar@1
|
67 |
* and substitution into (2) leads to:
|
alpar@1
|
68 |
*
|
alpar@1
|
69 |
* A = F * V = inv(P) * L * U * inv(Q).
|
alpar@1
|
70 |
*
|
alpar@1
|
71 |
* For more details see the program documentation. */
|
alpar@1
|
72 |
|
alpar@1
|
73 |
typedef struct LUF LUF;
|
alpar@1
|
74 |
|
alpar@1
|
75 |
struct LUF
|
alpar@1
|
76 |
{ /* LU-factorization of a square matrix */
|
alpar@1
|
77 |
int n_max;
|
alpar@1
|
78 |
/* maximal value of n (increased automatically, if necessary) */
|
alpar@1
|
79 |
int n;
|
alpar@1
|
80 |
/* the order of matrices A, F, V, P, Q */
|
alpar@1
|
81 |
int valid;
|
alpar@1
|
82 |
/* the factorization is valid only if this flag is set */
|
alpar@1
|
83 |
/*--------------------------------------------------------------*/
|
alpar@1
|
84 |
/* matrix F in row-wise format */
|
alpar@1
|
85 |
int *fr_ptr; /* int fr_ptr[1+n_max]; */
|
alpar@1
|
86 |
/* fr_ptr[i], i = 1,...,n, is a pointer to the first element of
|
alpar@1
|
87 |
i-th row in SVA */
|
alpar@1
|
88 |
int *fr_len; /* int fr_len[1+n_max]; */
|
alpar@1
|
89 |
/* fr_len[i], i = 1,...,n, is the number of elements in i-th row
|
alpar@1
|
90 |
(except unity diagonal element) */
|
alpar@1
|
91 |
/*--------------------------------------------------------------*/
|
alpar@1
|
92 |
/* matrix F in column-wise format */
|
alpar@1
|
93 |
int *fc_ptr; /* int fc_ptr[1+n_max]; */
|
alpar@1
|
94 |
/* fc_ptr[j], j = 1,...,n, is a pointer to the first element of
|
alpar@1
|
95 |
j-th column in SVA */
|
alpar@1
|
96 |
int *fc_len; /* int fc_len[1+n_max]; */
|
alpar@1
|
97 |
/* fc_len[j], j = 1,...,n, is the number of elements in j-th
|
alpar@1
|
98 |
column (except unity diagonal element) */
|
alpar@1
|
99 |
/*--------------------------------------------------------------*/
|
alpar@1
|
100 |
/* matrix V in row-wise format */
|
alpar@1
|
101 |
int *vr_ptr; /* int vr_ptr[1+n_max]; */
|
alpar@1
|
102 |
/* vr_ptr[i], i = 1,...,n, is a pointer to the first element of
|
alpar@1
|
103 |
i-th row in SVA */
|
alpar@1
|
104 |
int *vr_len; /* int vr_len[1+n_max]; */
|
alpar@1
|
105 |
/* vr_len[i], i = 1,...,n, is the number of elements in i-th row
|
alpar@1
|
106 |
(except pivot element) */
|
alpar@1
|
107 |
int *vr_cap; /* int vr_cap[1+n_max]; */
|
alpar@1
|
108 |
/* vr_cap[i], i = 1,...,n, is the capacity of i-th row, i.e.
|
alpar@1
|
109 |
maximal number of elements which can be stored in the row
|
alpar@1
|
110 |
without relocating it, vr_cap[i] >= vr_len[i] */
|
alpar@1
|
111 |
double *vr_piv; /* double vr_piv[1+n_max]; */
|
alpar@1
|
112 |
/* vr_piv[p], p = 1,...,n, is the pivot element v[p,q] which
|
alpar@1
|
113 |
corresponds to a diagonal element of matrix U = P*V*Q */
|
alpar@1
|
114 |
/*--------------------------------------------------------------*/
|
alpar@1
|
115 |
/* matrix V in column-wise format */
|
alpar@1
|
116 |
int *vc_ptr; /* int vc_ptr[1+n_max]; */
|
alpar@1
|
117 |
/* vc_ptr[j], j = 1,...,n, is a pointer to the first element of
|
alpar@1
|
118 |
j-th column in SVA */
|
alpar@1
|
119 |
int *vc_len; /* int vc_len[1+n_max]; */
|
alpar@1
|
120 |
/* vc_len[j], j = 1,...,n, is the number of elements in j-th
|
alpar@1
|
121 |
column (except pivot element) */
|
alpar@1
|
122 |
int *vc_cap; /* int vc_cap[1+n_max]; */
|
alpar@1
|
123 |
/* vc_cap[j], j = 1,...,n, is the capacity of j-th column, i.e.
|
alpar@1
|
124 |
maximal number of elements which can be stored in the column
|
alpar@1
|
125 |
without relocating it, vc_cap[j] >= vc_len[j] */
|
alpar@1
|
126 |
/*--------------------------------------------------------------*/
|
alpar@1
|
127 |
/* matrix P */
|
alpar@1
|
128 |
int *pp_row; /* int pp_row[1+n_max]; */
|
alpar@1
|
129 |
/* pp_row[i] = j means that P[i,j] = 1 */
|
alpar@1
|
130 |
int *pp_col; /* int pp_col[1+n_max]; */
|
alpar@1
|
131 |
/* pp_col[j] = i means that P[i,j] = 1 */
|
alpar@1
|
132 |
/* if i-th row or column of matrix F is i'-th row or column of
|
alpar@1
|
133 |
matrix L, or if i-th row of matrix V is i'-th row of matrix U,
|
alpar@1
|
134 |
then pp_row[i'] = i and pp_col[i] = i' */
|
alpar@1
|
135 |
/*--------------------------------------------------------------*/
|
alpar@1
|
136 |
/* matrix Q */
|
alpar@1
|
137 |
int *qq_row; /* int qq_row[1+n_max]; */
|
alpar@1
|
138 |
/* qq_row[i] = j means that Q[i,j] = 1 */
|
alpar@1
|
139 |
int *qq_col; /* int qq_col[1+n_max]; */
|
alpar@1
|
140 |
/* qq_col[j] = i means that Q[i,j] = 1 */
|
alpar@1
|
141 |
/* if j-th column of matrix V is j'-th column of matrix U, then
|
alpar@1
|
142 |
qq_row[j] = j' and qq_col[j'] = j */
|
alpar@1
|
143 |
/*--------------------------------------------------------------*/
|
alpar@1
|
144 |
/* the Sparse Vector Area (SVA) is a set of locations used to
|
alpar@1
|
145 |
store sparse vectors representing rows and columns of matrices
|
alpar@1
|
146 |
F and V; each location is a doublet (ind, val), where ind is
|
alpar@1
|
147 |
an index, and val is a numerical value of a sparse vector
|
alpar@1
|
148 |
element; in the whole each sparse vector is a set of adjacent
|
alpar@1
|
149 |
locations defined by a pointer to the first element and the
|
alpar@1
|
150 |
number of elements; these pointer and number are stored in the
|
alpar@1
|
151 |
corresponding matrix data structure (see above); the left part
|
alpar@1
|
152 |
of SVA is used to store rows and columns of matrix V, and its
|
alpar@1
|
153 |
right part is used to store rows and columns of matrix F; the
|
alpar@1
|
154 |
middle part of SVA contains free (unused) locations */
|
alpar@1
|
155 |
int sv_size;
|
alpar@1
|
156 |
/* the size of SVA, in locations; all locations are numbered by
|
alpar@1
|
157 |
integers 1, ..., n, and location 0 is not used; if necessary,
|
alpar@1
|
158 |
the SVA size is automatically increased */
|
alpar@1
|
159 |
int sv_beg, sv_end;
|
alpar@1
|
160 |
/* SVA partitioning pointers:
|
alpar@1
|
161 |
locations from 1 to sv_beg-1 belong to the left part
|
alpar@1
|
162 |
locations from sv_beg to sv_end-1 belong to the middle part
|
alpar@1
|
163 |
locations from sv_end to sv_size belong to the right part
|
alpar@1
|
164 |
the size of the middle part is (sv_end - sv_beg) */
|
alpar@1
|
165 |
int *sv_ind; /* sv_ind[1+sv_size]; */
|
alpar@1
|
166 |
/* sv_ind[k], 1 <= k <= sv_size, is the index field of k-th
|
alpar@1
|
167 |
location */
|
alpar@1
|
168 |
double *sv_val; /* sv_val[1+sv_size]; */
|
alpar@1
|
169 |
/* sv_val[k], 1 <= k <= sv_size, is the value field of k-th
|
alpar@1
|
170 |
location */
|
alpar@1
|
171 |
/*--------------------------------------------------------------*/
|
alpar@1
|
172 |
/* in order to efficiently defragment the left part of SVA there
|
alpar@1
|
173 |
is a doubly linked list of rows and columns of matrix V, where
|
alpar@1
|
174 |
rows are numbered by 1, ..., n, while columns are numbered by
|
alpar@1
|
175 |
n+1, ..., n+n, that allows uniquely identifying each row and
|
alpar@1
|
176 |
column of V by only one integer; in this list rows and columns
|
alpar@1
|
177 |
are ordered by ascending their pointers vr_ptr and vc_ptr */
|
alpar@1
|
178 |
int sv_head;
|
alpar@1
|
179 |
/* the number of leftmost row/column */
|
alpar@1
|
180 |
int sv_tail;
|
alpar@1
|
181 |
/* the number of rightmost row/column */
|
alpar@1
|
182 |
int *sv_prev; /* int sv_prev[1+n_max+n_max]; */
|
alpar@1
|
183 |
/* sv_prev[k], k = 1,...,n+n, is the number of a row/column which
|
alpar@1
|
184 |
precedes k-th row/column */
|
alpar@1
|
185 |
int *sv_next; /* int sv_next[1+n_max+n_max]; */
|
alpar@1
|
186 |
/* sv_next[k], k = 1,...,n+n, is the number of a row/column which
|
alpar@1
|
187 |
succedes k-th row/column */
|
alpar@1
|
188 |
/*--------------------------------------------------------------*/
|
alpar@1
|
189 |
/* working segment (used only during factorization) */
|
alpar@1
|
190 |
double *vr_max; /* int vr_max[1+n_max]; */
|
alpar@1
|
191 |
/* vr_max[i], 1 <= i <= n, is used only if i-th row of matrix V
|
alpar@1
|
192 |
is active (i.e. belongs to the active submatrix), and is the
|
alpar@1
|
193 |
largest magnitude of elements in i-th row; if vr_max[i] < 0,
|
alpar@1
|
194 |
the largest magnitude is not known yet and should be computed
|
alpar@1
|
195 |
by the pivoting routine */
|
alpar@1
|
196 |
/*--------------------------------------------------------------*/
|
alpar@1
|
197 |
/* in order to efficiently implement Markowitz strategy and Duff
|
alpar@1
|
198 |
search technique there are two families {R[0], R[1], ..., R[n]}
|
alpar@1
|
199 |
and {C[0], C[1], ..., C[n]}; member R[k] is the set of active
|
alpar@1
|
200 |
rows of matrix V, which have k non-zeros, and member C[k] is
|
alpar@1
|
201 |
the set of active columns of V, which have k non-zeros in the
|
alpar@1
|
202 |
active submatrix (i.e. in the active rows); each set R[k] and
|
alpar@1
|
203 |
C[k] is implemented as a separate doubly linked list */
|
alpar@1
|
204 |
int *rs_head; /* int rs_head[1+n_max]; */
|
alpar@1
|
205 |
/* rs_head[k], 0 <= k <= n, is the number of first active row,
|
alpar@1
|
206 |
which has k non-zeros */
|
alpar@1
|
207 |
int *rs_prev; /* int rs_prev[1+n_max]; */
|
alpar@1
|
208 |
/* rs_prev[i], 1 <= i <= n, is the number of previous row, which
|
alpar@1
|
209 |
has the same number of non-zeros as i-th row */
|
alpar@1
|
210 |
int *rs_next; /* int rs_next[1+n_max]; */
|
alpar@1
|
211 |
/* rs_next[i], 1 <= i <= n, is the number of next row, which has
|
alpar@1
|
212 |
the same number of non-zeros as i-th row */
|
alpar@1
|
213 |
int *cs_head; /* int cs_head[1+n_max]; */
|
alpar@1
|
214 |
/* cs_head[k], 0 <= k <= n, is the number of first active column,
|
alpar@1
|
215 |
which has k non-zeros (in the active rows) */
|
alpar@1
|
216 |
int *cs_prev; /* int cs_prev[1+n_max]; */
|
alpar@1
|
217 |
/* cs_prev[j], 1 <= j <= n, is the number of previous column,
|
alpar@1
|
218 |
which has the same number of non-zeros (in the active rows) as
|
alpar@1
|
219 |
j-th column */
|
alpar@1
|
220 |
int *cs_next; /* int cs_next[1+n_max]; */
|
alpar@1
|
221 |
/* cs_next[j], 1 <= j <= n, is the number of next column, which
|
alpar@1
|
222 |
has the same number of non-zeros (in the active rows) as j-th
|
alpar@1
|
223 |
column */
|
alpar@1
|
224 |
/* (end of working segment) */
|
alpar@1
|
225 |
/*--------------------------------------------------------------*/
|
alpar@1
|
226 |
/* working arrays */
|
alpar@1
|
227 |
int *flag; /* int flag[1+n_max]; */
|
alpar@1
|
228 |
/* integer working array */
|
alpar@1
|
229 |
double *work; /* double work[1+n_max]; */
|
alpar@1
|
230 |
/* floating-point working array */
|
alpar@1
|
231 |
/*--------------------------------------------------------------*/
|
alpar@1
|
232 |
/* control parameters */
|
alpar@1
|
233 |
int new_sva;
|
alpar@1
|
234 |
/* new required size of the sparse vector area, in locations; set
|
alpar@1
|
235 |
automatically by the factorizing routine */
|
alpar@1
|
236 |
double piv_tol;
|
alpar@1
|
237 |
/* threshold pivoting tolerance, 0 < piv_tol < 1; element v[i,j]
|
alpar@1
|
238 |
of the active submatrix fits to be pivot if it satisfies to the
|
alpar@1
|
239 |
stability criterion |v[i,j]| >= piv_tol * max |v[i,*]|, i.e. if
|
alpar@1
|
240 |
it is not very small in the magnitude among other elements in
|
alpar@1
|
241 |
the same row; decreasing this parameter gives better sparsity
|
alpar@1
|
242 |
at the expense of numerical accuracy and vice versa */
|
alpar@1
|
243 |
int piv_lim;
|
alpar@1
|
244 |
/* maximal allowable number of pivot candidates to be considered;
|
alpar@1
|
245 |
if piv_lim pivot candidates have been considered, the pivoting
|
alpar@1
|
246 |
routine terminates the search with the best candidate found */
|
alpar@1
|
247 |
int suhl;
|
alpar@1
|
248 |
/* if this flag is set, the pivoting routine applies a heuristic
|
alpar@1
|
249 |
proposed by Uwe Suhl: if a column of the active submatrix has
|
alpar@1
|
250 |
no eligible pivot candidates (i.e. all its elements do not
|
alpar@1
|
251 |
satisfy to the stability criterion), the routine excludes it
|
alpar@1
|
252 |
from futher consideration until it becomes column singleton;
|
alpar@1
|
253 |
in many cases this allows reducing the time needed for pivot
|
alpar@1
|
254 |
searching */
|
alpar@1
|
255 |
double eps_tol;
|
alpar@1
|
256 |
/* epsilon tolerance; each element of the active submatrix, whose
|
alpar@1
|
257 |
magnitude is less than eps_tol, is replaced by exact zero */
|
alpar@1
|
258 |
double max_gro;
|
alpar@1
|
259 |
/* maximal allowable growth of elements of matrix V during all
|
alpar@1
|
260 |
the factorization process; if on some eliminaion step the ratio
|
alpar@1
|
261 |
big_v / max_a (see below) becomes greater than max_gro, matrix
|
alpar@1
|
262 |
A is considered as ill-conditioned (assuming that the pivoting
|
alpar@1
|
263 |
tolerance piv_tol has an appropriate value) */
|
alpar@1
|
264 |
/*--------------------------------------------------------------*/
|
alpar@1
|
265 |
/* some statistics */
|
alpar@1
|
266 |
int nnz_a;
|
alpar@1
|
267 |
/* the number of non-zeros in matrix A */
|
alpar@1
|
268 |
int nnz_f;
|
alpar@1
|
269 |
/* the number of non-zeros in matrix F (except diagonal elements,
|
alpar@1
|
270 |
which are not stored) */
|
alpar@1
|
271 |
int nnz_v;
|
alpar@1
|
272 |
/* the number of non-zeros in matrix V (except its pivot elements,
|
alpar@1
|
273 |
which are stored in a separate array) */
|
alpar@1
|
274 |
double max_a;
|
alpar@1
|
275 |
/* the largest magnitude of elements of matrix A */
|
alpar@1
|
276 |
double big_v;
|
alpar@1
|
277 |
/* the largest magnitude of elements of matrix V appeared in the
|
alpar@1
|
278 |
active submatrix during all the factorization process */
|
alpar@1
|
279 |
int rank;
|
alpar@1
|
280 |
/* estimated rank of matrix A */
|
alpar@1
|
281 |
};
|
alpar@1
|
282 |
|
alpar@1
|
283 |
/* return codes: */
|
alpar@1
|
284 |
#define LUF_ESING 1 /* singular matrix */
|
alpar@1
|
285 |
#define LUF_ECOND 2 /* ill-conditioned matrix */
|
alpar@1
|
286 |
|
alpar@1
|
287 |
#define luf_create_it _glp_luf_create_it
|
alpar@1
|
288 |
LUF *luf_create_it(void);
|
alpar@1
|
289 |
/* create LU-factorization */
|
alpar@1
|
290 |
|
alpar@1
|
291 |
#define luf_defrag_sva _glp_luf_defrag_sva
|
alpar@1
|
292 |
void luf_defrag_sva(LUF *luf);
|
alpar@1
|
293 |
/* defragment the sparse vector area */
|
alpar@1
|
294 |
|
alpar@1
|
295 |
#define luf_enlarge_row _glp_luf_enlarge_row
|
alpar@1
|
296 |
int luf_enlarge_row(LUF *luf, int i, int cap);
|
alpar@1
|
297 |
/* enlarge row capacity */
|
alpar@1
|
298 |
|
alpar@1
|
299 |
#define luf_enlarge_col _glp_luf_enlarge_col
|
alpar@1
|
300 |
int luf_enlarge_col(LUF *luf, int j, int cap);
|
alpar@1
|
301 |
/* enlarge column capacity */
|
alpar@1
|
302 |
|
alpar@1
|
303 |
#define luf_factorize _glp_luf_factorize
|
alpar@1
|
304 |
int luf_factorize(LUF *luf, int n, int (*col)(void *info, int j,
|
alpar@1
|
305 |
int ind[], double val[]), void *info);
|
alpar@1
|
306 |
/* compute LU-factorization */
|
alpar@1
|
307 |
|
alpar@1
|
308 |
#define luf_f_solve _glp_luf_f_solve
|
alpar@1
|
309 |
void luf_f_solve(LUF *luf, int tr, double x[]);
|
alpar@1
|
310 |
/* solve system F*x = b or F'*x = b */
|
alpar@1
|
311 |
|
alpar@1
|
312 |
#define luf_v_solve _glp_luf_v_solve
|
alpar@1
|
313 |
void luf_v_solve(LUF *luf, int tr, double x[]);
|
alpar@1
|
314 |
/* solve system V*x = b or V'*x = b */
|
alpar@1
|
315 |
|
alpar@1
|
316 |
#define luf_a_solve _glp_luf_a_solve
|
alpar@1
|
317 |
void luf_a_solve(LUF *luf, int tr, double x[]);
|
alpar@1
|
318 |
/* solve system A*x = b or A'*x = b */
|
alpar@1
|
319 |
|
alpar@1
|
320 |
#define luf_delete_it _glp_luf_delete_it
|
alpar@1
|
321 |
void luf_delete_it(LUF *luf);
|
alpar@1
|
322 |
/* delete LU-factorization */
|
alpar@1
|
323 |
|
alpar@1
|
324 |
#endif
|
alpar@1
|
325 |
|
alpar@1
|
326 |
/* eof */
|