3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
29 #include "colamd/colamd.h"
31 /*----------------------------------------------------------------------
32 -- check_fvs - check sparse vector in full-vector storage format.
36 -- #include "glpmat.h"
37 -- int check_fvs(int n, int nnz, int ind[], double vec[]);
41 -- The routine check_fvs checks if a given vector of dimension n in
42 -- full-vector storage format has correct representation.
46 -- The routine returns one of the following codes:
48 -- 0 - the vector is correct;
49 -- 1 - the number of elements (n) is negative;
50 -- 2 - the number of non-zero elements (nnz) is negative;
51 -- 3 - some element index is out of range;
52 -- 4 - some element index is duplicate;
53 -- 5 - some non-zero element is out of pattern. */
55 int check_fvs(int n, int nnz, int ind[], double vec[])
56 { int i, t, ret, *flag = NULL;
57 /* check the number of elements */
62 /* check the number of non-zero elements */
67 /* check vector indices */
68 flag = xcalloc(1+n, sizeof(int));
69 for (i = 1; i <= n; i++) flag[i] = 0;
70 for (t = 1; t <= nnz; t++)
72 if (!(1 <= i && i <= n))
82 /* check vector elements */
83 for (i = 1; i <= n; i++)
84 { if (!flag[i] && vec[i] != 0.0)
89 /* the vector is ok */
91 done: if (flag != NULL) xfree(flag);
95 /*----------------------------------------------------------------------
96 -- check_pattern - check pattern of sparse matrix.
100 -- #include "glpmat.h"
101 -- int check_pattern(int m, int n, int A_ptr[], int A_ind[]);
105 -- The routine check_pattern checks the pattern of a given mxn matrix
106 -- in storage-by-rows format.
110 -- The routine returns one of the following codes:
112 -- 0 - the pattern is correct;
113 -- 1 - the number of rows (m) is negative;
114 -- 2 - the number of columns (n) is negative;
115 -- 3 - A_ptr[1] is not 1;
116 -- 4 - some column index is out of range;
117 -- 5 - some column indices are duplicate. */
119 int check_pattern(int m, int n, int A_ptr[], int A_ind[])
120 { int i, j, ptr, ret, *flag = NULL;
121 /* check the number of rows */
126 /* check the number of columns */
131 /* check location A_ptr[1] */
136 /* check row patterns */
137 flag = xcalloc(1+n, sizeof(int));
138 for (j = 1; j <= n; j++) flag[j] = 0;
139 for (i = 1; i <= m; i++)
140 { /* check pattern of row i */
141 for (ptr = A_ptr[i]; ptr < A_ptr[i+1]; ptr++)
143 /* check column index */
144 if (!(1 <= j && j <= n))
148 /* check for duplication */
156 for (ptr = A_ptr[i]; ptr < A_ptr[i+1]; ptr++)
161 /* the pattern is ok */
163 done: if (flag != NULL) xfree(flag);
167 /*----------------------------------------------------------------------
168 -- transpose - transpose sparse matrix.
172 -- #include "glpmat.h"
173 -- void transpose(int m, int n, int A_ptr[], int A_ind[],
174 -- double A_val[], int AT_ptr[], int AT_ind[], double AT_val[]);
178 -- For a given mxn sparse matrix A the routine transpose builds a nxm
179 -- sparse matrix A' which is a matrix transposed to A.
181 -- The arrays A_ptr, A_ind, and A_val specify a given mxn matrix A to
182 -- be transposed in storage-by-rows format. The parameter A_val can be
183 -- NULL, in which case numeric values are not copied. The arrays A_ptr,
184 -- A_ind, and A_val are not changed on exit.
186 -- On entry the arrays AT_ptr, AT_ind, and AT_val must be allocated,
187 -- but their content is ignored. On exit the routine stores a resultant
188 -- nxm matrix A' in these arrays in storage-by-rows format. Note that
189 -- if the parameter A_val is NULL, the array AT_val is not used.
191 -- The routine transpose has a side effect that elements in rows of the
192 -- resultant matrix A' follow in ascending their column indices. */
194 void transpose(int m, int n, int A_ptr[], int A_ind[], double A_val[],
195 int AT_ptr[], int AT_ind[], double AT_val[])
196 { int i, j, t, beg, end, pos, len;
197 /* determine row lengths of resultant matrix */
198 for (j = 1; j <= n; j++) AT_ptr[j] = 0;
199 for (i = 1; i <= m; i++)
200 { beg = A_ptr[i], end = A_ptr[i+1];
201 for (t = beg; t < end; t++) AT_ptr[A_ind[t]]++;
203 /* set up row pointers of resultant matrix */
205 for (j = 1; j <= n; j++)
206 len = AT_ptr[j], pos += len, AT_ptr[j] = pos;
208 /* build resultant matrix */
209 for (i = m; i >= 1; i--)
210 { beg = A_ptr[i], end = A_ptr[i+1];
211 for (t = beg; t < end; t++)
212 { pos = --AT_ptr[A_ind[t]];
214 if (A_val != NULL) AT_val[pos] = A_val[t];
220 /*----------------------------------------------------------------------
221 -- adat_symbolic - compute S = P*A*D*A'*P' (symbolic phase).
225 -- #include "glpmat.h"
226 -- int *adat_symbolic(int m, int n, int P_per[], int A_ptr[],
227 -- int A_ind[], int S_ptr[]);
231 -- The routine adat_symbolic implements the symbolic phase to compute
232 -- symmetric matrix S = P*A*D*A'*P', where P is a permutation matrix,
233 -- A is a given sparse matrix, D is a diagonal matrix, A' is a matrix
234 -- transposed to A, P' is an inverse of P.
236 -- The parameter m is the number of rows in A and the order of P.
238 -- The parameter n is the number of columns in A and the order of D.
240 -- The array P_per specifies permutation matrix P. It is not changed on
243 -- The arrays A_ptr and A_ind specify the pattern of matrix A. They are
244 -- not changed on exit.
246 -- On exit the routine stores the pattern of upper triangular part of
247 -- matrix S without diagonal elements in the arrays S_ptr and S_ind in
248 -- storage-by-rows format. The array S_ptr should be allocated on entry,
249 -- however, its content is ignored. The array S_ind is allocated by the
250 -- routine itself which returns a pointer to it.
254 -- The routine returns a pointer to the array S_ind. */
256 int *adat_symbolic(int m, int n, int P_per[], int A_ptr[], int A_ind[],
258 { int i, j, t, ii, jj, tt, k, size, len;
259 int *S_ind, *AT_ptr, *AT_ind, *ind, *map, *temp;
260 /* build the pattern of A', which is a matrix transposed to A, to
261 efficiently access A in column-wise manner */
262 AT_ptr = xcalloc(1+n+1, sizeof(int));
263 AT_ind = xcalloc(A_ptr[m+1], sizeof(int));
264 transpose(m, n, A_ptr, A_ind, NULL, AT_ptr, AT_ind, NULL);
265 /* allocate the array S_ind */
266 size = A_ptr[m+1] - 1;
267 if (size < m) size = m;
268 S_ind = xcalloc(1+size, sizeof(int));
269 /* allocate and initialize working arrays */
270 ind = xcalloc(1+m, sizeof(int));
271 map = xcalloc(1+m, sizeof(int));
272 for (jj = 1; jj <= m; jj++) map[jj] = 0;
273 /* compute pattern of S; note that symbolically S = B*B', where
274 B = P*A, B' is matrix transposed to B */
276 for (ii = 1; ii <= m; ii++)
277 { /* compute pattern of ii-th row of S */
279 i = P_per[ii]; /* i-th row of A = ii-th row of B */
280 for (t = A_ptr[i]; t < A_ptr[i+1]; t++)
282 /* walk through k-th column of A */
283 for (tt = AT_ptr[k]; tt < AT_ptr[k+1]; tt++)
285 jj = P_per[m+j]; /* j-th row of A = jj-th row of B */
286 /* a[i,k] != 0 and a[j,k] != 0 ergo s[ii,jj] != 0 */
287 if (ii < jj && !map[jj]) ind[++len] = jj, map[jj] = 1;
290 /* now (ind) is pattern of ii-th row of S */
291 S_ptr[ii+1] = S_ptr[ii] + len;
292 /* at least (S_ptr[ii+1] - 1) locations should be available in
294 if (S_ptr[ii+1] - 1 > size)
297 S_ind = xcalloc(1+size, sizeof(int));
298 memcpy(&S_ind[1], &temp[1], (S_ptr[ii] - 1) * sizeof(int));
301 xassert(S_ptr[ii+1] - 1 <= size);
302 /* (ii-th row of S) := (ind) */
303 memcpy(&S_ind[S_ptr[ii]], &ind[1], len * sizeof(int));
304 /* clear the row pattern map */
305 for (t = 1; t <= len; t++) map[ind[t]] = 0;
307 /* free working arrays */
312 /* reallocate the array S_ind to free unused locations */
314 size = S_ptr[m+1] - 1;
315 S_ind = xcalloc(1+size, sizeof(int));
316 memcpy(&S_ind[1], &temp[1], size * sizeof(int));
321 /*----------------------------------------------------------------------
322 -- adat_numeric - compute S = P*A*D*A'*P' (numeric phase).
326 -- #include "glpmat.h"
327 -- void adat_numeric(int m, int n, int P_per[],
328 -- int A_ptr[], int A_ind[], double A_val[], double D_diag[],
329 -- int S_ptr[], int S_ind[], double S_val[], double S_diag[]);
333 -- The routine adat_numeric implements the numeric phase to compute
334 -- symmetric matrix S = P*A*D*A'*P', where P is a permutation matrix,
335 -- A is a given sparse matrix, D is a diagonal matrix, A' is a matrix
336 -- transposed to A, P' is an inverse of P.
338 -- The parameter m is the number of rows in A and the order of P.
340 -- The parameter n is the number of columns in A and the order of D.
342 -- The matrix P is specified in the array P_per, which is not changed
345 -- The matrix A is specified in the arrays A_ptr, A_ind, and A_val in
346 -- storage-by-rows format. These arrays are not changed on exit.
348 -- Diagonal elements of the matrix D are specified in the array D_diag,
349 -- where D_diag[0] is not used, D_diag[i] = d[i,i] for i = 1, ..., n.
350 -- The array D_diag is not changed on exit.
352 -- The pattern of the upper triangular part of the matrix S without
353 -- diagonal elements (previously computed by the routine adat_symbolic)
354 -- is specified in the arrays S_ptr and S_ind, which are not changed on
355 -- exit. Numeric values of non-diagonal elements of S are stored in
356 -- corresponding locations of the array S_val, and values of diagonal
357 -- elements of S are stored in locations S_diag[1], ..., S_diag[n]. */
359 void adat_numeric(int m, int n, int P_per[],
360 int A_ptr[], int A_ind[], double A_val[], double D_diag[],
361 int S_ptr[], int S_ind[], double S_val[], double S_diag[])
362 { int i, j, t, ii, jj, tt, beg, end, beg1, end1, k;
364 work = xcalloc(1+n, sizeof(double));
365 for (j = 1; j <= n; j++) work[j] = 0.0;
366 /* compute S = B*D*B', where B = P*A, B' is a matrix transposed
368 for (ii = 1; ii <= m; ii++)
369 { i = P_per[ii]; /* i-th row of A = ii-th row of B */
370 /* (work) := (i-th row of A) */
371 beg = A_ptr[i], end = A_ptr[i+1];
372 for (t = beg; t < end; t++)
373 work[A_ind[t]] = A_val[t];
374 /* compute ii-th row of S */
375 beg = S_ptr[ii], end = S_ptr[ii+1];
376 for (t = beg; t < end; t++)
378 j = P_per[jj]; /* j-th row of A = jj-th row of B */
379 /* s[ii,jj] := sum a[i,k] * d[k,k] * a[j,k] */
381 beg1 = A_ptr[j], end1 = A_ptr[j+1];
382 for (tt = beg1; tt < end1; tt++)
384 sum += work[k] * D_diag[k] * A_val[tt];
388 /* s[ii,ii] := sum a[i,k] * d[k,k] * a[i,k] */
390 beg = A_ptr[i], end = A_ptr[i+1];
391 for (t = beg; t < end; t++)
393 sum += A_val[t] * D_diag[k] * A_val[t];
402 /*----------------------------------------------------------------------
403 -- min_degree - minimum degree ordering.
407 -- #include "glpmat.h"
408 -- void min_degree(int n, int A_ptr[], int A_ind[], int P_per[]);
412 -- The routine min_degree uses the minimum degree ordering algorithm
413 -- to find a permutation matrix P for a given sparse symmetric positive
414 -- matrix A which minimizes the number of non-zeros in upper triangular
415 -- factor U for Cholesky factorization P*A*P' = U'*U.
417 -- The parameter n is the order of matrices A and P.
419 -- The pattern of the given matrix A is specified on entry in the arrays
420 -- A_ptr and A_ind in storage-by-rows format. Only the upper triangular
421 -- part without diagonal elements (which all are assumed to be non-zero)
422 -- should be specified as if A were upper triangular. The arrays A_ptr
423 -- and A_ind are not changed on exit.
425 -- The permutation matrix P is stored by the routine in the array P_per
430 -- The routine min_degree is based on some subroutines from the package
431 -- SPARSPAK (see comments in the module glpqmd). */
433 void min_degree(int n, int A_ptr[], int A_ind[], int P_per[])
434 { int i, j, ne, t, pos, len;
435 int *xadj, *adjncy, *deg, *marker, *rchset, *nbrhd, *qsize,
437 /* determine number of non-zeros in complete pattern */
440 /* allocate working arrays */
441 xadj = xcalloc(1+n+1, sizeof(int));
442 adjncy = xcalloc(1+ne, sizeof(int));
443 deg = xcalloc(1+n, sizeof(int));
444 marker = xcalloc(1+n, sizeof(int));
445 rchset = xcalloc(1+n, sizeof(int));
446 nbrhd = xcalloc(1+n, sizeof(int));
447 qsize = xcalloc(1+n, sizeof(int));
448 qlink = xcalloc(1+n, sizeof(int));
449 /* determine row lengths in complete pattern */
450 for (i = 1; i <= n; i++) xadj[i] = 0;
451 for (i = 1; i <= n; i++)
452 { for (t = A_ptr[i]; t < A_ptr[i+1]; t++)
454 xassert(i < j && j <= n);
455 xadj[i]++, xadj[j]++;
458 /* set up row pointers for complete pattern */
460 for (i = 1; i <= n; i++)
461 len = xadj[i], pos += len, xadj[i] = pos;
463 xassert(pos - 1 == ne);
464 /* construct complete pattern */
465 for (i = 1; i <= n; i++)
466 { for (t = A_ptr[i]; t < A_ptr[i+1]; t++)
468 adjncy[--xadj[i]] = j, adjncy[--xadj[j]] = i;
471 /* call the main minimimum degree ordering routine */
472 genqmd(&n, xadj, adjncy, P_per, P_per + n, deg, marker, rchset,
473 nbrhd, qsize, qlink, &nofsub);
474 /* make sure that permutation matrix P is correct */
475 for (i = 1; i <= n; i++)
477 xassert(1 <= j && j <= n);
478 xassert(P_per[n+j] == i);
480 /* free working arrays */
492 /**********************************************************************/
494 void amd_order1(int n, int A_ptr[], int A_ind[], int P_per[])
495 { /* approximate minimum degree ordering (AMD) */
497 double Control[AMD_CONTROL], Info[AMD_INFO];
498 /* get the default parameters */
499 amd_defaults(Control);
502 amd_control(Control);
504 /* make all indices 0-based */
505 for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]--;
506 for (k = 1; k <= n+1; k++) A_ptr[k]--;
507 /* call the ordering routine */
508 ret = amd_order(n, &A_ptr[1], &A_ind[1], &P_per[1], Control, Info)
513 xassert(ret == AMD_OK || ret == AMD_OK_BUT_JUMBLED);
514 /* retsore 1-based indices */
515 for (k = 1; k <= n+1; k++) A_ptr[k]++;
516 for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]++;
517 /* patch up permutation matrix */
518 memset(&P_per[n+1], 0, n * sizeof(int));
519 for (k = 1; k <= n; k++)
521 xassert(1 <= P_per[k] && P_per[k] <= n);
522 xassert(P_per[n+P_per[k]] == 0);
523 P_per[n+P_per[k]] = k;
528 /**********************************************************************/
530 static void *allocate(size_t n, size_t size)
532 ptr = xcalloc(n, size);
533 memset(ptr, 0, n * size);
537 static void release(void *ptr)
542 void symamd_ord(int n, int A_ptr[], int A_ind[], int P_per[])
543 { /* approximate minimum degree ordering (SYMAMD) */
545 int stats[COLAMD_STATS];
546 /* make all indices 0-based */
547 for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]--;
548 for (k = 1; k <= n+1; k++) A_ptr[k]--;
549 /* call the ordering routine */
550 ok = symamd(n, &A_ind[1], &A_ptr[1], &P_per[1], NULL, stats,
553 symamd_report(stats);
556 /* restore 1-based indices */
557 for (k = 1; k <= n+1; k++) A_ptr[k]++;
558 for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]++;
559 /* patch up permutation matrix */
560 memset(&P_per[n+1], 0, n * sizeof(int));
561 for (k = 1; k <= n; k++)
563 xassert(1 <= P_per[k] && P_per[k] <= n);
564 xassert(P_per[n+P_per[k]] == 0);
565 P_per[n+P_per[k]] = k;
570 /*----------------------------------------------------------------------
571 -- chol_symbolic - compute Cholesky factorization (symbolic phase).
575 -- #include "glpmat.h"
576 -- int *chol_symbolic(int n, int A_ptr[], int A_ind[], int U_ptr[]);
580 -- The routine chol_symbolic implements the symbolic phase of Cholesky
581 -- factorization A = U'*U, where A is a given sparse symmetric positive
582 -- definite matrix, U is a resultant upper triangular factor, U' is a
583 -- matrix transposed to U.
585 -- The parameter n is the order of matrices A and U.
587 -- The pattern of the given matrix A is specified on entry in the arrays
588 -- A_ptr and A_ind in storage-by-rows format. Only the upper triangular
589 -- part without diagonal elements (which all are assumed to be non-zero)
590 -- should be specified as if A were upper triangular. The arrays A_ptr
591 -- and A_ind are not changed on exit.
593 -- The pattern of the matrix U without diagonal elements (which all are
594 -- assumed to be non-zero) is stored on exit from the routine in the
595 -- arrays U_ptr and U_ind in storage-by-rows format. The array U_ptr
596 -- should be allocated on entry, however, its content is ignored. The
597 -- array U_ind is allocated by the routine which returns a pointer to it
602 -- The routine returns a pointer to the array U_ind.
606 -- The routine chol_symbolic computes the pattern of the matrix U in a
607 -- row-wise manner. No pivoting is used.
609 -- It is known that to compute the pattern of row k of the matrix U we
610 -- need to merge the pattern of row k of the matrix A and the patterns
611 -- of each row i of U, where u[i,k] is non-zero (these rows are already
612 -- computed and placed above row k).
614 -- However, to reduce the number of rows to be merged the routine uses
615 -- an advanced algorithm proposed in:
617 -- D.J.Rose, R.E.Tarjan, and G.S.Lueker. Algorithmic aspects of vertex
618 -- elimination on graphs. SIAM J. Comput. 5, 1976, 266-83.
620 -- The authors of the cited paper show that we have the same result if
621 -- we merge row k of the matrix A and such rows of the matrix U (among
622 -- rows 1, ..., k-1) whose leftmost non-diagonal non-zero element is
623 -- placed in k-th column. This feature signficantly reduces the number
624 -- of rows to be merged, especially on the final steps, where rows of
625 -- the matrix U become quite dense.
627 -- To determine rows, which should be merged on k-th step, for a fixed
628 -- time the routine uses linked lists of row numbers of the matrix U.
629 -- Location head[k] contains the number of a first row, whose leftmost
630 -- non-diagonal non-zero element is placed in column k, and location
631 -- next[i] contains the number of a next row with the same property as
634 int *chol_symbolic(int n, int A_ptr[], int A_ind[], int U_ptr[])
635 { int i, j, k, t, len, size, beg, end, min_j, *U_ind, *head, *next,
637 /* initially we assume that on computing the pattern of U fill-in
638 will double the number of non-zeros in A */
639 size = A_ptr[n+1] - 1;
640 if (size < n) size = n;
642 U_ind = xcalloc(1+size, sizeof(int));
643 /* allocate and initialize working arrays */
644 head = xcalloc(1+n, sizeof(int));
645 for (i = 1; i <= n; i++) head[i] = 0;
646 next = xcalloc(1+n, sizeof(int));
647 ind = xcalloc(1+n, sizeof(int));
648 map = xcalloc(1+n, sizeof(int));
649 for (j = 1; j <= n; j++) map[j] = 0;
650 /* compute the pattern of matrix U */
652 for (k = 1; k <= n; k++)
653 { /* compute the pattern of k-th row of U, which is the union of
654 k-th row of A and those rows of U (among 1, ..., k-1) whose
655 leftmost non-diagonal non-zero is placed in k-th column */
656 /* (ind) := (k-th row of A) */
657 len = A_ptr[k+1] - A_ptr[k];
658 memcpy(&ind[1], &A_ind[A_ptr[k]], len * sizeof(int));
659 for (t = 1; t <= len; t++)
661 xassert(k < j && j <= n);
664 /* walk through rows of U whose leftmost non-diagonal non-zero
665 is placed in k-th column */
666 for (i = head[k]; i != 0; i = next[i])
667 { /* (ind) := (ind) union (i-th row of U) */
668 beg = U_ptr[i], end = U_ptr[i+1];
669 for (t = beg; t < end; t++)
671 if (j > k && !map[j]) ind[++len] = j, map[j] = 1;
674 /* now (ind) is the pattern of k-th row of U */
675 U_ptr[k+1] = U_ptr[k] + len;
676 /* at least (U_ptr[k+1] - 1) locations should be available in
678 if (U_ptr[k+1] - 1 > size)
681 U_ind = xcalloc(1+size, sizeof(int));
682 memcpy(&U_ind[1], &temp[1], (U_ptr[k] - 1) * sizeof(int));
685 xassert(U_ptr[k+1] - 1 <= size);
686 /* (k-th row of U) := (ind) */
687 memcpy(&U_ind[U_ptr[k]], &ind[1], len * sizeof(int));
688 /* determine column index of leftmost non-diagonal non-zero in
689 k-th row of U and clear the row pattern map */
691 for (t = 1; t <= len; t++)
692 { j = ind[t], map[j] = 0;
693 if (min_j > j) min_j = j;
695 /* include k-th row into corresponding linked list */
696 if (min_j <= n) next[k] = head[min_j], head[min_j] = k;
698 /* free working arrays */
703 /* reallocate the array U_ind to free unused locations */
705 size = U_ptr[n+1] - 1;
706 U_ind = xcalloc(1+size, sizeof(int));
707 memcpy(&U_ind[1], &temp[1], size * sizeof(int));
712 /*----------------------------------------------------------------------
713 -- chol_numeric - compute Cholesky factorization (numeric phase).
717 -- #include "glpmat.h"
718 -- int chol_numeric(int n,
719 -- int A_ptr[], int A_ind[], double A_val[], double A_diag[],
720 -- int U_ptr[], int U_ind[], double U_val[], double U_diag[]);
724 -- The routine chol_symbolic implements the numeric phase of Cholesky
725 -- factorization A = U'*U, where A is a given sparse symmetric positive
726 -- definite matrix, U is a resultant upper triangular factor, U' is a
727 -- matrix transposed to U.
729 -- The parameter n is the order of matrices A and U.
731 -- Upper triangular part of the matrix A without diagonal elements is
732 -- specified in the arrays A_ptr, A_ind, and A_val in storage-by-rows
733 -- format. Diagonal elements of A are specified in the array A_diag,
734 -- where A_diag[0] is not used, A_diag[i] = a[i,i] for i = 1, ..., n.
735 -- The arrays A_ptr, A_ind, A_val, and A_diag are not changed on exit.
737 -- The pattern of the matrix U without diagonal elements (previously
738 -- computed with the routine chol_symbolic) is specified in the arrays
739 -- U_ptr and U_ind, which are not changed on exit. Numeric values of
740 -- non-diagonal elements of U are stored in corresponding locations of
741 -- the array U_val, and values of diagonal elements of U are stored in
742 -- locations U_diag[1], ..., U_diag[n].
746 -- The routine returns the number of non-positive diagonal elements of
747 -- the matrix U which have been replaced by a huge positive number (see
748 -- the method description below). Zero return code means the matrix A
749 -- has been successfully factorized.
753 -- The routine chol_numeric computes the matrix U in a row-wise manner
754 -- using standard gaussian elimination technique. No pivoting is used.
756 -- Initially the routine sets U = A, and before k-th elimination step
757 -- the matrix U is the following:
760 -- 1 x x x x x x x x x x
761 -- . x x x x x x x x x
762 -- . . x x x x x x x x
763 -- . . . x x x x x x x
764 -- k . . . . * * * * * *
765 -- . . . . * * * * * *
766 -- . . . . * * * * * *
767 -- . . . . * * * * * *
768 -- . . . . * * * * * *
769 -- n . . . . * * * * * *
771 -- where 'x' are elements of already computed rows, '*' are elements of
772 -- the active submatrix. (Note that the lower triangular part of the
773 -- active submatrix being symmetric is not stored and diagonal elements
774 -- are stored separately in the array U_diag.)
776 -- The matrix A is assumed to be positive definite. However, if it is
777 -- close to semi-definite, on some elimination step a pivot u[k,k] may
778 -- happen to be non-positive due to round-off errors. In this case the
779 -- routine uses a technique proposed in:
781 -- S.J.Wright. The Cholesky factorization in interior-point and barrier
782 -- methods. Preprint MCS-P600-0596, Mathematics and Computer Science
783 -- Division, Argonne National Laboratory, Argonne, Ill., May 1996.
785 -- The routine just replaces non-positive u[k,k] by a huge positive
786 -- number. This involves non-diagonal elements in k-th row of U to be
787 -- close to zero that, in turn, involves k-th component of a solution
788 -- vector to be close to zero. Note, however, that this technique works
789 -- only if the system A*x = b is consistent. */
791 int chol_numeric(int n,
792 int A_ptr[], int A_ind[], double A_val[], double A_diag[],
793 int U_ptr[], int U_ind[], double U_val[], double U_diag[])
794 { int i, j, k, t, t1, beg, end, beg1, end1, count = 0;
795 double ukk, uki, *work;
796 work = xcalloc(1+n, sizeof(double));
797 for (j = 1; j <= n; j++) work[j] = 0.0;
798 /* U := (upper triangle of A) */
799 /* note that the upper traingle of A is a subset of U */
800 for (i = 1; i <= n; i++)
801 { beg = A_ptr[i], end = A_ptr[i+1];
802 for (t = beg; t < end; t++)
803 j = A_ind[t], work[j] = A_val[t];
804 beg = U_ptr[i], end = U_ptr[i+1];
805 for (t = beg; t < end; t++)
806 j = U_ind[t], U_val[t] = work[j], work[j] = 0.0;
807 U_diag[i] = A_diag[i];
809 /* main elimination loop */
810 for (k = 1; k <= n; k++)
811 { /* transform k-th row of U */
814 U_diag[k] = ukk = sqrt(ukk);
816 U_diag[k] = ukk = DBL_MAX, count++;
817 /* (work) := (transformed k-th row) */
818 beg = U_ptr[k], end = U_ptr[k+1];
819 for (t = beg; t < end; t++)
820 work[U_ind[t]] = (U_val[t] /= ukk);
821 /* transform other rows of U */
822 for (t = beg; t < end; t++)
825 /* (i-th row) := (i-th row) - u[k,i] * (k-th row) */
827 beg1 = U_ptr[i], end1 = U_ptr[i+1];
828 for (t1 = beg1; t1 < end1; t1++)
829 U_val[t1] -= uki * work[U_ind[t1]];
830 U_diag[i] -= uki * uki;
833 for (t = beg; t < end; t++)
834 work[U_ind[t]] = 0.0;
840 /*----------------------------------------------------------------------
841 -- u_solve - solve upper triangular system U*x = b.
845 -- #include "glpmat.h"
846 -- void u_solve(int n, int U_ptr[], int U_ind[], double U_val[],
847 -- double U_diag[], double x[]);
851 -- The routine u_solve solves an linear system U*x = b, where U is an
852 -- upper triangular matrix.
854 -- The parameter n is the order of matrix U.
856 -- The matrix U without diagonal elements is specified in the arrays
857 -- U_ptr, U_ind, and U_val in storage-by-rows format. Diagonal elements
858 -- of U are specified in the array U_diag, where U_diag[0] is not used,
859 -- U_diag[i] = u[i,i] for i = 1, ..., n. All these four arrays are not
862 -- The right-hand side vector b is specified on entry in the array x,
863 -- where x[0] is not used, and x[i] = b[i] for i = 1, ..., n. On exit
864 -- the routine stores computed components of the vector of unknowns x
865 -- in the array x in the same manner. */
867 void u_solve(int n, int U_ptr[], int U_ind[], double U_val[],
868 double U_diag[], double x[])
869 { int i, t, beg, end;
871 for (i = n; i >= 1; i--)
873 beg = U_ptr[i], end = U_ptr[i+1];
874 for (t = beg; t < end; t++)
875 temp -= U_val[t] * x[U_ind[t]];
876 xassert(U_diag[i] != 0.0);
877 x[i] = temp / U_diag[i];
882 /*----------------------------------------------------------------------
883 -- ut_solve - solve lower triangular system U'*x = b.
887 -- #include "glpmat.h"
888 -- void ut_solve(int n, int U_ptr[], int U_ind[], double U_val[],
889 -- double U_diag[], double x[]);
893 -- The routine ut_solve solves an linear system U'*x = b, where U is a
894 -- matrix transposed to an upper triangular matrix.
896 -- The parameter n is the order of matrix U.
898 -- The matrix U without diagonal elements is specified in the arrays
899 -- U_ptr, U_ind, and U_val in storage-by-rows format. Diagonal elements
900 -- of U are specified in the array U_diag, where U_diag[0] is not used,
901 -- U_diag[i] = u[i,i] for i = 1, ..., n. All these four arrays are not
904 -- The right-hand side vector b is specified on entry in the array x,
905 -- where x[0] is not used, and x[i] = b[i] for i = 1, ..., n. On exit
906 -- the routine stores computed components of the vector of unknowns x
907 -- in the array x in the same manner. */
909 void ut_solve(int n, int U_ptr[], int U_ind[], double U_val[],
910 double U_diag[], double x[])
911 { int i, t, beg, end;
913 for (i = 1; i <= n; i++)
914 { xassert(U_diag[i] != 0.0);
915 temp = (x[i] /= U_diag[i]);
916 if (temp == 0.0) continue;
917 beg = U_ptr[i], end = U_ptr[i+1];
918 for (t = beg; t < end; t++)
919 x[U_ind[t]] -= U_val[t] * temp;