alpar@1: /* glpmat.c */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpenv.h" alpar@1: #include "glpmat.h" alpar@1: #include "glpqmd.h" alpar@1: #include "amd/amd.h" alpar@1: #include "colamd/colamd.h" alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- check_fvs - check sparse vector in full-vector storage format. alpar@1: -- alpar@1: -- SYNOPSIS alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- int check_fvs(int n, int nnz, int ind[], double vec[]); alpar@1: -- alpar@1: -- DESCRIPTION alpar@1: -- alpar@1: -- The routine check_fvs checks if a given vector of dimension n in alpar@1: -- full-vector storage format has correct representation. alpar@1: -- alpar@1: -- RETURNS alpar@1: -- alpar@1: -- The routine returns one of the following codes: alpar@1: -- alpar@1: -- 0 - the vector is correct; alpar@1: -- 1 - the number of elements (n) is negative; alpar@1: -- 2 - the number of non-zero elements (nnz) is negative; alpar@1: -- 3 - some element index is out of range; alpar@1: -- 4 - some element index is duplicate; alpar@1: -- 5 - some non-zero element is out of pattern. */ alpar@1: alpar@1: int check_fvs(int n, int nnz, int ind[], double vec[]) alpar@1: { int i, t, ret, *flag = NULL; alpar@1: /* check the number of elements */ alpar@1: if (n < 0) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: /* check the number of non-zero elements */ alpar@1: if (nnz < 0) alpar@1: { ret = 2; alpar@1: goto done; alpar@1: } alpar@1: /* check vector indices */ alpar@1: flag = xcalloc(1+n, sizeof(int)); alpar@1: for (i = 1; i <= n; i++) flag[i] = 0; alpar@1: for (t = 1; t <= nnz; t++) alpar@1: { i = ind[t]; alpar@1: if (!(1 <= i && i <= n)) alpar@1: { ret = 3; alpar@1: goto done; alpar@1: } alpar@1: if (flag[i]) alpar@1: { ret = 4; alpar@1: goto done; alpar@1: } alpar@1: flag[i] = 1; alpar@1: } alpar@1: /* check vector elements */ alpar@1: for (i = 1; i <= n; i++) alpar@1: { if (!flag[i] && vec[i] != 0.0) alpar@1: { ret = 5; alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: /* the vector is ok */ alpar@1: ret = 0; alpar@1: done: if (flag != NULL) xfree(flag); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- check_pattern - check pattern of sparse matrix. alpar@1: -- alpar@1: -- SYNOPSIS alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- int check_pattern(int m, int n, int A_ptr[], int A_ind[]); alpar@1: -- alpar@1: -- DESCRIPTION alpar@1: -- alpar@1: -- The routine check_pattern checks the pattern of a given mxn matrix alpar@1: -- in storage-by-rows format. alpar@1: -- alpar@1: -- RETURNS alpar@1: -- alpar@1: -- The routine returns one of the following codes: alpar@1: -- alpar@1: -- 0 - the pattern is correct; alpar@1: -- 1 - the number of rows (m) is negative; alpar@1: -- 2 - the number of columns (n) is negative; alpar@1: -- 3 - A_ptr[1] is not 1; alpar@1: -- 4 - some column index is out of range; alpar@1: -- 5 - some column indices are duplicate. */ alpar@1: alpar@1: int check_pattern(int m, int n, int A_ptr[], int A_ind[]) alpar@1: { int i, j, ptr, ret, *flag = NULL; alpar@1: /* check the number of rows */ alpar@1: if (m < 0) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: /* check the number of columns */ alpar@1: if (n < 0) alpar@1: { ret = 2; alpar@1: goto done; alpar@1: } alpar@1: /* check location A_ptr[1] */ alpar@1: if (A_ptr[1] != 1) alpar@1: { ret = 3; alpar@1: goto done; alpar@1: } alpar@1: /* check row patterns */ alpar@1: flag = xcalloc(1+n, sizeof(int)); alpar@1: for (j = 1; j <= n; j++) flag[j] = 0; alpar@1: for (i = 1; i <= m; i++) alpar@1: { /* check pattern of row i */ alpar@1: for (ptr = A_ptr[i]; ptr < A_ptr[i+1]; ptr++) alpar@1: { j = A_ind[ptr]; alpar@1: /* check column index */ alpar@1: if (!(1 <= j && j <= n)) alpar@1: { ret = 4; alpar@1: goto done; alpar@1: } alpar@1: /* check for duplication */ alpar@1: if (flag[j]) alpar@1: { ret = 5; alpar@1: goto done; alpar@1: } alpar@1: flag[j] = 1; alpar@1: } alpar@1: /* clear flags */ alpar@1: for (ptr = A_ptr[i]; ptr < A_ptr[i+1]; ptr++) alpar@1: { j = A_ind[ptr]; alpar@1: flag[j] = 0; alpar@1: } alpar@1: } alpar@1: /* the pattern is ok */ alpar@1: ret = 0; alpar@1: done: if (flag != NULL) xfree(flag); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- transpose - transpose sparse matrix. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- void transpose(int m, int n, int A_ptr[], int A_ind[], alpar@1: -- double A_val[], int AT_ptr[], int AT_ind[], double AT_val[]); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- For a given mxn sparse matrix A the routine transpose builds a nxm alpar@1: -- sparse matrix A' which is a matrix transposed to A. alpar@1: -- alpar@1: -- The arrays A_ptr, A_ind, and A_val specify a given mxn matrix A to alpar@1: -- be transposed in storage-by-rows format. The parameter A_val can be alpar@1: -- NULL, in which case numeric values are not copied. The arrays A_ptr, alpar@1: -- A_ind, and A_val are not changed on exit. alpar@1: -- alpar@1: -- On entry the arrays AT_ptr, AT_ind, and AT_val must be allocated, alpar@1: -- but their content is ignored. On exit the routine stores a resultant alpar@1: -- nxm matrix A' in these arrays in storage-by-rows format. Note that alpar@1: -- if the parameter A_val is NULL, the array AT_val is not used. alpar@1: -- alpar@1: -- The routine transpose has a side effect that elements in rows of the alpar@1: -- resultant matrix A' follow in ascending their column indices. */ alpar@1: alpar@1: void transpose(int m, int n, int A_ptr[], int A_ind[], double A_val[], alpar@1: int AT_ptr[], int AT_ind[], double AT_val[]) alpar@1: { int i, j, t, beg, end, pos, len; alpar@1: /* determine row lengths of resultant matrix */ alpar@1: for (j = 1; j <= n; j++) AT_ptr[j] = 0; alpar@1: for (i = 1; i <= m; i++) alpar@1: { beg = A_ptr[i], end = A_ptr[i+1]; alpar@1: for (t = beg; t < end; t++) AT_ptr[A_ind[t]]++; alpar@1: } alpar@1: /* set up row pointers of resultant matrix */ alpar@1: pos = 1; alpar@1: for (j = 1; j <= n; j++) alpar@1: len = AT_ptr[j], pos += len, AT_ptr[j] = pos; alpar@1: AT_ptr[n+1] = pos; alpar@1: /* build resultant matrix */ alpar@1: for (i = m; i >= 1; i--) alpar@1: { beg = A_ptr[i], end = A_ptr[i+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: { pos = --AT_ptr[A_ind[t]]; alpar@1: AT_ind[pos] = i; alpar@1: if (A_val != NULL) AT_val[pos] = A_val[t]; alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- adat_symbolic - compute S = P*A*D*A'*P' (symbolic phase). alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- int *adat_symbolic(int m, int n, int P_per[], int A_ptr[], alpar@1: -- int A_ind[], int S_ptr[]); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine adat_symbolic implements the symbolic phase to compute alpar@1: -- symmetric matrix S = P*A*D*A'*P', where P is a permutation matrix, alpar@1: -- A is a given sparse matrix, D is a diagonal matrix, A' is a matrix alpar@1: -- transposed to A, P' is an inverse of P. alpar@1: -- alpar@1: -- The parameter m is the number of rows in A and the order of P. alpar@1: -- alpar@1: -- The parameter n is the number of columns in A and the order of D. alpar@1: -- alpar@1: -- The array P_per specifies permutation matrix P. It is not changed on alpar@1: -- exit. alpar@1: -- alpar@1: -- The arrays A_ptr and A_ind specify the pattern of matrix A. They are alpar@1: -- not changed on exit. alpar@1: -- alpar@1: -- On exit the routine stores the pattern of upper triangular part of alpar@1: -- matrix S without diagonal elements in the arrays S_ptr and S_ind in alpar@1: -- storage-by-rows format. The array S_ptr should be allocated on entry, alpar@1: -- however, its content is ignored. The array S_ind is allocated by the alpar@1: -- routine itself which returns a pointer to it. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine returns a pointer to the array S_ind. */ alpar@1: alpar@1: int *adat_symbolic(int m, int n, int P_per[], int A_ptr[], int A_ind[], alpar@1: int S_ptr[]) alpar@1: { int i, j, t, ii, jj, tt, k, size, len; alpar@1: int *S_ind, *AT_ptr, *AT_ind, *ind, *map, *temp; alpar@1: /* build the pattern of A', which is a matrix transposed to A, to alpar@1: efficiently access A in column-wise manner */ alpar@1: AT_ptr = xcalloc(1+n+1, sizeof(int)); alpar@1: AT_ind = xcalloc(A_ptr[m+1], sizeof(int)); alpar@1: transpose(m, n, A_ptr, A_ind, NULL, AT_ptr, AT_ind, NULL); alpar@1: /* allocate the array S_ind */ alpar@1: size = A_ptr[m+1] - 1; alpar@1: if (size < m) size = m; alpar@1: S_ind = xcalloc(1+size, sizeof(int)); alpar@1: /* allocate and initialize working arrays */ alpar@1: ind = xcalloc(1+m, sizeof(int)); alpar@1: map = xcalloc(1+m, sizeof(int)); alpar@1: for (jj = 1; jj <= m; jj++) map[jj] = 0; alpar@1: /* compute pattern of S; note that symbolically S = B*B', where alpar@1: B = P*A, B' is matrix transposed to B */ alpar@1: S_ptr[1] = 1; alpar@1: for (ii = 1; ii <= m; ii++) alpar@1: { /* compute pattern of ii-th row of S */ alpar@1: len = 0; alpar@1: i = P_per[ii]; /* i-th row of A = ii-th row of B */ alpar@1: for (t = A_ptr[i]; t < A_ptr[i+1]; t++) alpar@1: { k = A_ind[t]; alpar@1: /* walk through k-th column of A */ alpar@1: for (tt = AT_ptr[k]; tt < AT_ptr[k+1]; tt++) alpar@1: { j = AT_ind[tt]; alpar@1: jj = P_per[m+j]; /* j-th row of A = jj-th row of B */ alpar@1: /* a[i,k] != 0 and a[j,k] != 0 ergo s[ii,jj] != 0 */ alpar@1: if (ii < jj && !map[jj]) ind[++len] = jj, map[jj] = 1; alpar@1: } alpar@1: } alpar@1: /* now (ind) is pattern of ii-th row of S */ alpar@1: S_ptr[ii+1] = S_ptr[ii] + len; alpar@1: /* at least (S_ptr[ii+1] - 1) locations should be available in alpar@1: the array S_ind */ alpar@1: if (S_ptr[ii+1] - 1 > size) alpar@1: { temp = S_ind; alpar@1: size += size; alpar@1: S_ind = xcalloc(1+size, sizeof(int)); alpar@1: memcpy(&S_ind[1], &temp[1], (S_ptr[ii] - 1) * sizeof(int)); alpar@1: xfree(temp); alpar@1: } alpar@1: xassert(S_ptr[ii+1] - 1 <= size); alpar@1: /* (ii-th row of S) := (ind) */ alpar@1: memcpy(&S_ind[S_ptr[ii]], &ind[1], len * sizeof(int)); alpar@1: /* clear the row pattern map */ alpar@1: for (t = 1; t <= len; t++) map[ind[t]] = 0; alpar@1: } alpar@1: /* free working arrays */ alpar@1: xfree(AT_ptr); alpar@1: xfree(AT_ind); alpar@1: xfree(ind); alpar@1: xfree(map); alpar@1: /* reallocate the array S_ind to free unused locations */ alpar@1: temp = S_ind; alpar@1: size = S_ptr[m+1] - 1; alpar@1: S_ind = xcalloc(1+size, sizeof(int)); alpar@1: memcpy(&S_ind[1], &temp[1], size * sizeof(int)); alpar@1: xfree(temp); alpar@1: return S_ind; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- adat_numeric - compute S = P*A*D*A'*P' (numeric phase). alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- void adat_numeric(int m, int n, int P_per[], alpar@1: -- int A_ptr[], int A_ind[], double A_val[], double D_diag[], alpar@1: -- int S_ptr[], int S_ind[], double S_val[], double S_diag[]); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine adat_numeric implements the numeric phase to compute alpar@1: -- symmetric matrix S = P*A*D*A'*P', where P is a permutation matrix, alpar@1: -- A is a given sparse matrix, D is a diagonal matrix, A' is a matrix alpar@1: -- transposed to A, P' is an inverse of P. alpar@1: -- alpar@1: -- The parameter m is the number of rows in A and the order of P. alpar@1: -- alpar@1: -- The parameter n is the number of columns in A and the order of D. alpar@1: -- alpar@1: -- The matrix P is specified in the array P_per, which is not changed alpar@1: -- on exit. alpar@1: -- alpar@1: -- The matrix A is specified in the arrays A_ptr, A_ind, and A_val in alpar@1: -- storage-by-rows format. These arrays are not changed on exit. alpar@1: -- alpar@1: -- Diagonal elements of the matrix D are specified in the array D_diag, alpar@1: -- where D_diag[0] is not used, D_diag[i] = d[i,i] for i = 1, ..., n. alpar@1: -- The array D_diag is not changed on exit. alpar@1: -- alpar@1: -- The pattern of the upper triangular part of the matrix S without alpar@1: -- diagonal elements (previously computed by the routine adat_symbolic) alpar@1: -- is specified in the arrays S_ptr and S_ind, which are not changed on alpar@1: -- exit. Numeric values of non-diagonal elements of S are stored in alpar@1: -- corresponding locations of the array S_val, and values of diagonal alpar@1: -- elements of S are stored in locations S_diag[1], ..., S_diag[n]. */ alpar@1: alpar@1: void adat_numeric(int m, int n, int P_per[], alpar@1: int A_ptr[], int A_ind[], double A_val[], double D_diag[], alpar@1: int S_ptr[], int S_ind[], double S_val[], double S_diag[]) alpar@1: { int i, j, t, ii, jj, tt, beg, end, beg1, end1, k; alpar@1: double sum, *work; alpar@1: work = xcalloc(1+n, sizeof(double)); alpar@1: for (j = 1; j <= n; j++) work[j] = 0.0; alpar@1: /* compute S = B*D*B', where B = P*A, B' is a matrix transposed alpar@1: to B */ alpar@1: for (ii = 1; ii <= m; ii++) alpar@1: { i = P_per[ii]; /* i-th row of A = ii-th row of B */ alpar@1: /* (work) := (i-th row of A) */ alpar@1: beg = A_ptr[i], end = A_ptr[i+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: work[A_ind[t]] = A_val[t]; alpar@1: /* compute ii-th row of S */ alpar@1: beg = S_ptr[ii], end = S_ptr[ii+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: { jj = S_ind[t]; alpar@1: j = P_per[jj]; /* j-th row of A = jj-th row of B */ alpar@1: /* s[ii,jj] := sum a[i,k] * d[k,k] * a[j,k] */ alpar@1: sum = 0.0; alpar@1: beg1 = A_ptr[j], end1 = A_ptr[j+1]; alpar@1: for (tt = beg1; tt < end1; tt++) alpar@1: { k = A_ind[tt]; alpar@1: sum += work[k] * D_diag[k] * A_val[tt]; alpar@1: } alpar@1: S_val[t] = sum; alpar@1: } alpar@1: /* s[ii,ii] := sum a[i,k] * d[k,k] * a[i,k] */ alpar@1: sum = 0.0; alpar@1: beg = A_ptr[i], end = A_ptr[i+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: { k = A_ind[t]; alpar@1: sum += A_val[t] * D_diag[k] * A_val[t]; alpar@1: work[k] = 0.0; alpar@1: } alpar@1: S_diag[ii] = sum; alpar@1: } alpar@1: xfree(work); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- min_degree - minimum degree ordering. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- void min_degree(int n, int A_ptr[], int A_ind[], int P_per[]); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine min_degree uses the minimum degree ordering algorithm alpar@1: -- to find a permutation matrix P for a given sparse symmetric positive alpar@1: -- matrix A which minimizes the number of non-zeros in upper triangular alpar@1: -- factor U for Cholesky factorization P*A*P' = U'*U. alpar@1: -- alpar@1: -- The parameter n is the order of matrices A and P. alpar@1: -- alpar@1: -- The pattern of the given matrix A is specified on entry in the arrays alpar@1: -- A_ptr and A_ind in storage-by-rows format. Only the upper triangular alpar@1: -- part without diagonal elements (which all are assumed to be non-zero) alpar@1: -- should be specified as if A were upper triangular. The arrays A_ptr alpar@1: -- and A_ind are not changed on exit. alpar@1: -- alpar@1: -- The permutation matrix P is stored by the routine in the array P_per alpar@1: -- on exit. alpar@1: -- alpar@1: -- *Algorithm* alpar@1: -- alpar@1: -- The routine min_degree is based on some subroutines from the package alpar@1: -- SPARSPAK (see comments in the module glpqmd). */ alpar@1: alpar@1: void min_degree(int n, int A_ptr[], int A_ind[], int P_per[]) alpar@1: { int i, j, ne, t, pos, len; alpar@1: int *xadj, *adjncy, *deg, *marker, *rchset, *nbrhd, *qsize, alpar@1: *qlink, nofsub; alpar@1: /* determine number of non-zeros in complete pattern */ alpar@1: ne = A_ptr[n+1] - 1; alpar@1: ne += ne; alpar@1: /* allocate working arrays */ alpar@1: xadj = xcalloc(1+n+1, sizeof(int)); alpar@1: adjncy = xcalloc(1+ne, sizeof(int)); alpar@1: deg = xcalloc(1+n, sizeof(int)); alpar@1: marker = xcalloc(1+n, sizeof(int)); alpar@1: rchset = xcalloc(1+n, sizeof(int)); alpar@1: nbrhd = xcalloc(1+n, sizeof(int)); alpar@1: qsize = xcalloc(1+n, sizeof(int)); alpar@1: qlink = xcalloc(1+n, sizeof(int)); alpar@1: /* determine row lengths in complete pattern */ alpar@1: for (i = 1; i <= n; i++) xadj[i] = 0; alpar@1: for (i = 1; i <= n; i++) alpar@1: { for (t = A_ptr[i]; t < A_ptr[i+1]; t++) alpar@1: { j = A_ind[t]; alpar@1: xassert(i < j && j <= n); alpar@1: xadj[i]++, xadj[j]++; alpar@1: } alpar@1: } alpar@1: /* set up row pointers for complete pattern */ alpar@1: pos = 1; alpar@1: for (i = 1; i <= n; i++) alpar@1: len = xadj[i], pos += len, xadj[i] = pos; alpar@1: xadj[n+1] = pos; alpar@1: xassert(pos - 1 == ne); alpar@1: /* construct complete pattern */ alpar@1: for (i = 1; i <= n; i++) alpar@1: { for (t = A_ptr[i]; t < A_ptr[i+1]; t++) alpar@1: { j = A_ind[t]; alpar@1: adjncy[--xadj[i]] = j, adjncy[--xadj[j]] = i; alpar@1: } alpar@1: } alpar@1: /* call the main minimimum degree ordering routine */ alpar@1: genqmd(&n, xadj, adjncy, P_per, P_per + n, deg, marker, rchset, alpar@1: nbrhd, qsize, qlink, &nofsub); alpar@1: /* make sure that permutation matrix P is correct */ alpar@1: for (i = 1; i <= n; i++) alpar@1: { j = P_per[i]; alpar@1: xassert(1 <= j && j <= n); alpar@1: xassert(P_per[n+j] == i); alpar@1: } alpar@1: /* free working arrays */ alpar@1: xfree(xadj); alpar@1: xfree(adjncy); alpar@1: xfree(deg); alpar@1: xfree(marker); alpar@1: xfree(rchset); alpar@1: xfree(nbrhd); alpar@1: xfree(qsize); alpar@1: xfree(qlink); alpar@1: return; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: void amd_order1(int n, int A_ptr[], int A_ind[], int P_per[]) alpar@1: { /* approximate minimum degree ordering (AMD) */ alpar@1: int k, ret; alpar@1: double Control[AMD_CONTROL], Info[AMD_INFO]; alpar@1: /* get the default parameters */ alpar@1: amd_defaults(Control); alpar@1: #if 0 alpar@1: /* and print them */ alpar@1: amd_control(Control); alpar@1: #endif alpar@1: /* make all indices 0-based */ alpar@1: for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]--; alpar@1: for (k = 1; k <= n+1; k++) A_ptr[k]--; alpar@1: /* call the ordering routine */ alpar@1: ret = amd_order(n, &A_ptr[1], &A_ind[1], &P_per[1], Control, Info) alpar@1: ; alpar@1: #if 0 alpar@1: amd_info(Info); alpar@1: #endif alpar@1: xassert(ret == AMD_OK || ret == AMD_OK_BUT_JUMBLED); alpar@1: /* retsore 1-based indices */ alpar@1: for (k = 1; k <= n+1; k++) A_ptr[k]++; alpar@1: for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]++; alpar@1: /* patch up permutation matrix */ alpar@1: memset(&P_per[n+1], 0, n * sizeof(int)); alpar@1: for (k = 1; k <= n; k++) alpar@1: { P_per[k]++; alpar@1: xassert(1 <= P_per[k] && P_per[k] <= n); alpar@1: xassert(P_per[n+P_per[k]] == 0); alpar@1: P_per[n+P_per[k]] = k; alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: static void *allocate(size_t n, size_t size) alpar@1: { void *ptr; alpar@1: ptr = xcalloc(n, size); alpar@1: memset(ptr, 0, n * size); alpar@1: return ptr; alpar@1: } alpar@1: alpar@1: static void release(void *ptr) alpar@1: { xfree(ptr); alpar@1: return; alpar@1: } alpar@1: alpar@1: void symamd_ord(int n, int A_ptr[], int A_ind[], int P_per[]) alpar@1: { /* approximate minimum degree ordering (SYMAMD) */ alpar@1: int k, ok; alpar@1: int stats[COLAMD_STATS]; alpar@1: /* make all indices 0-based */ alpar@1: for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]--; alpar@1: for (k = 1; k <= n+1; k++) A_ptr[k]--; alpar@1: /* call the ordering routine */ alpar@1: ok = symamd(n, &A_ind[1], &A_ptr[1], &P_per[1], NULL, stats, alpar@1: allocate, release); alpar@1: #if 0 alpar@1: symamd_report(stats); alpar@1: #endif alpar@1: xassert(ok); alpar@1: /* restore 1-based indices */ alpar@1: for (k = 1; k <= n+1; k++) A_ptr[k]++; alpar@1: for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]++; alpar@1: /* patch up permutation matrix */ alpar@1: memset(&P_per[n+1], 0, n * sizeof(int)); alpar@1: for (k = 1; k <= n; k++) alpar@1: { P_per[k]++; alpar@1: xassert(1 <= P_per[k] && P_per[k] <= n); alpar@1: xassert(P_per[n+P_per[k]] == 0); alpar@1: P_per[n+P_per[k]] = k; alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- chol_symbolic - compute Cholesky factorization (symbolic phase). alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- int *chol_symbolic(int n, int A_ptr[], int A_ind[], int U_ptr[]); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine chol_symbolic implements the symbolic phase of Cholesky alpar@1: -- factorization A = U'*U, where A is a given sparse symmetric positive alpar@1: -- definite matrix, U is a resultant upper triangular factor, U' is a alpar@1: -- matrix transposed to U. alpar@1: -- alpar@1: -- The parameter n is the order of matrices A and U. alpar@1: -- alpar@1: -- The pattern of the given matrix A is specified on entry in the arrays alpar@1: -- A_ptr and A_ind in storage-by-rows format. Only the upper triangular alpar@1: -- part without diagonal elements (which all are assumed to be non-zero) alpar@1: -- should be specified as if A were upper triangular. The arrays A_ptr alpar@1: -- and A_ind are not changed on exit. alpar@1: -- alpar@1: -- The pattern of the matrix U without diagonal elements (which all are alpar@1: -- assumed to be non-zero) is stored on exit from the routine in the alpar@1: -- arrays U_ptr and U_ind in storage-by-rows format. The array U_ptr alpar@1: -- should be allocated on entry, however, its content is ignored. The alpar@1: -- array U_ind is allocated by the routine which returns a pointer to it alpar@1: -- on exit. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine returns a pointer to the array U_ind. alpar@1: -- alpar@1: -- *Method* alpar@1: -- alpar@1: -- The routine chol_symbolic computes the pattern of the matrix U in a alpar@1: -- row-wise manner. No pivoting is used. alpar@1: -- alpar@1: -- It is known that to compute the pattern of row k of the matrix U we alpar@1: -- need to merge the pattern of row k of the matrix A and the patterns alpar@1: -- of each row i of U, where u[i,k] is non-zero (these rows are already alpar@1: -- computed and placed above row k). alpar@1: -- alpar@1: -- However, to reduce the number of rows to be merged the routine uses alpar@1: -- an advanced algorithm proposed in: alpar@1: -- alpar@1: -- D.J.Rose, R.E.Tarjan, and G.S.Lueker. Algorithmic aspects of vertex alpar@1: -- elimination on graphs. SIAM J. Comput. 5, 1976, 266-83. alpar@1: -- alpar@1: -- The authors of the cited paper show that we have the same result if alpar@1: -- we merge row k of the matrix A and such rows of the matrix U (among alpar@1: -- rows 1, ..., k-1) whose leftmost non-diagonal non-zero element is alpar@1: -- placed in k-th column. This feature signficantly reduces the number alpar@1: -- of rows to be merged, especially on the final steps, where rows of alpar@1: -- the matrix U become quite dense. alpar@1: -- alpar@1: -- To determine rows, which should be merged on k-th step, for a fixed alpar@1: -- time the routine uses linked lists of row numbers of the matrix U. alpar@1: -- Location head[k] contains the number of a first row, whose leftmost alpar@1: -- non-diagonal non-zero element is placed in column k, and location alpar@1: -- next[i] contains the number of a next row with the same property as alpar@1: -- row i. */ alpar@1: alpar@1: int *chol_symbolic(int n, int A_ptr[], int A_ind[], int U_ptr[]) alpar@1: { int i, j, k, t, len, size, beg, end, min_j, *U_ind, *head, *next, alpar@1: *ind, *map, *temp; alpar@1: /* initially we assume that on computing the pattern of U fill-in alpar@1: will double the number of non-zeros in A */ alpar@1: size = A_ptr[n+1] - 1; alpar@1: if (size < n) size = n; alpar@1: size += size; alpar@1: U_ind = xcalloc(1+size, sizeof(int)); alpar@1: /* allocate and initialize working arrays */ alpar@1: head = xcalloc(1+n, sizeof(int)); alpar@1: for (i = 1; i <= n; i++) head[i] = 0; alpar@1: next = xcalloc(1+n, sizeof(int)); alpar@1: ind = xcalloc(1+n, sizeof(int)); alpar@1: map = xcalloc(1+n, sizeof(int)); alpar@1: for (j = 1; j <= n; j++) map[j] = 0; alpar@1: /* compute the pattern of matrix U */ alpar@1: U_ptr[1] = 1; alpar@1: for (k = 1; k <= n; k++) alpar@1: { /* compute the pattern of k-th row of U, which is the union of alpar@1: k-th row of A and those rows of U (among 1, ..., k-1) whose alpar@1: leftmost non-diagonal non-zero is placed in k-th column */ alpar@1: /* (ind) := (k-th row of A) */ alpar@1: len = A_ptr[k+1] - A_ptr[k]; alpar@1: memcpy(&ind[1], &A_ind[A_ptr[k]], len * sizeof(int)); alpar@1: for (t = 1; t <= len; t++) alpar@1: { j = ind[t]; alpar@1: xassert(k < j && j <= n); alpar@1: map[j] = 1; alpar@1: } alpar@1: /* walk through rows of U whose leftmost non-diagonal non-zero alpar@1: is placed in k-th column */ alpar@1: for (i = head[k]; i != 0; i = next[i]) alpar@1: { /* (ind) := (ind) union (i-th row of U) */ alpar@1: beg = U_ptr[i], end = U_ptr[i+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: { j = U_ind[t]; alpar@1: if (j > k && !map[j]) ind[++len] = j, map[j] = 1; alpar@1: } alpar@1: } alpar@1: /* now (ind) is the pattern of k-th row of U */ alpar@1: U_ptr[k+1] = U_ptr[k] + len; alpar@1: /* at least (U_ptr[k+1] - 1) locations should be available in alpar@1: the array U_ind */ alpar@1: if (U_ptr[k+1] - 1 > size) alpar@1: { temp = U_ind; alpar@1: size += size; alpar@1: U_ind = xcalloc(1+size, sizeof(int)); alpar@1: memcpy(&U_ind[1], &temp[1], (U_ptr[k] - 1) * sizeof(int)); alpar@1: xfree(temp); alpar@1: } alpar@1: xassert(U_ptr[k+1] - 1 <= size); alpar@1: /* (k-th row of U) := (ind) */ alpar@1: memcpy(&U_ind[U_ptr[k]], &ind[1], len * sizeof(int)); alpar@1: /* determine column index of leftmost non-diagonal non-zero in alpar@1: k-th row of U and clear the row pattern map */ alpar@1: min_j = n + 1; alpar@1: for (t = 1; t <= len; t++) alpar@1: { j = ind[t], map[j] = 0; alpar@1: if (min_j > j) min_j = j; alpar@1: } alpar@1: /* include k-th row into corresponding linked list */ alpar@1: if (min_j <= n) next[k] = head[min_j], head[min_j] = k; alpar@1: } alpar@1: /* free working arrays */ alpar@1: xfree(head); alpar@1: xfree(next); alpar@1: xfree(ind); alpar@1: xfree(map); alpar@1: /* reallocate the array U_ind to free unused locations */ alpar@1: temp = U_ind; alpar@1: size = U_ptr[n+1] - 1; alpar@1: U_ind = xcalloc(1+size, sizeof(int)); alpar@1: memcpy(&U_ind[1], &temp[1], size * sizeof(int)); alpar@1: xfree(temp); alpar@1: return U_ind; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- chol_numeric - compute Cholesky factorization (numeric phase). alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- int chol_numeric(int n, alpar@1: -- int A_ptr[], int A_ind[], double A_val[], double A_diag[], alpar@1: -- int U_ptr[], int U_ind[], double U_val[], double U_diag[]); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine chol_symbolic implements the numeric phase of Cholesky alpar@1: -- factorization A = U'*U, where A is a given sparse symmetric positive alpar@1: -- definite matrix, U is a resultant upper triangular factor, U' is a alpar@1: -- matrix transposed to U. alpar@1: -- alpar@1: -- The parameter n is the order of matrices A and U. alpar@1: -- alpar@1: -- Upper triangular part of the matrix A without diagonal elements is alpar@1: -- specified in the arrays A_ptr, A_ind, and A_val in storage-by-rows alpar@1: -- format. Diagonal elements of A are specified in the array A_diag, alpar@1: -- where A_diag[0] is not used, A_diag[i] = a[i,i] for i = 1, ..., n. alpar@1: -- The arrays A_ptr, A_ind, A_val, and A_diag are not changed on exit. alpar@1: -- alpar@1: -- The pattern of the matrix U without diagonal elements (previously alpar@1: -- computed with the routine chol_symbolic) is specified in the arrays alpar@1: -- U_ptr and U_ind, which are not changed on exit. Numeric values of alpar@1: -- non-diagonal elements of U are stored in corresponding locations of alpar@1: -- the array U_val, and values of diagonal elements of U are stored in alpar@1: -- locations U_diag[1], ..., U_diag[n]. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine returns the number of non-positive diagonal elements of alpar@1: -- the matrix U which have been replaced by a huge positive number (see alpar@1: -- the method description below). Zero return code means the matrix A alpar@1: -- has been successfully factorized. alpar@1: -- alpar@1: -- *Method* alpar@1: -- alpar@1: -- The routine chol_numeric computes the matrix U in a row-wise manner alpar@1: -- using standard gaussian elimination technique. No pivoting is used. alpar@1: -- alpar@1: -- Initially the routine sets U = A, and before k-th elimination step alpar@1: -- the matrix U is the following: alpar@1: -- alpar@1: -- 1 k n alpar@1: -- 1 x x x x x x x x x x alpar@1: -- . x x x x x x x x x alpar@1: -- . . x x x x x x x x alpar@1: -- . . . x x x x x x x alpar@1: -- k . . . . * * * * * * alpar@1: -- . . . . * * * * * * alpar@1: -- . . . . * * * * * * alpar@1: -- . . . . * * * * * * alpar@1: -- . . . . * * * * * * alpar@1: -- n . . . . * * * * * * alpar@1: -- alpar@1: -- where 'x' are elements of already computed rows, '*' are elements of alpar@1: -- the active submatrix. (Note that the lower triangular part of the alpar@1: -- active submatrix being symmetric is not stored and diagonal elements alpar@1: -- are stored separately in the array U_diag.) alpar@1: -- alpar@1: -- The matrix A is assumed to be positive definite. However, if it is alpar@1: -- close to semi-definite, on some elimination step a pivot u[k,k] may alpar@1: -- happen to be non-positive due to round-off errors. In this case the alpar@1: -- routine uses a technique proposed in: alpar@1: -- alpar@1: -- S.J.Wright. The Cholesky factorization in interior-point and barrier alpar@1: -- methods. Preprint MCS-P600-0596, Mathematics and Computer Science alpar@1: -- Division, Argonne National Laboratory, Argonne, Ill., May 1996. alpar@1: -- alpar@1: -- The routine just replaces non-positive u[k,k] by a huge positive alpar@1: -- number. This involves non-diagonal elements in k-th row of U to be alpar@1: -- close to zero that, in turn, involves k-th component of a solution alpar@1: -- vector to be close to zero. Note, however, that this technique works alpar@1: -- only if the system A*x = b is consistent. */ alpar@1: alpar@1: int chol_numeric(int n, alpar@1: int A_ptr[], int A_ind[], double A_val[], double A_diag[], alpar@1: int U_ptr[], int U_ind[], double U_val[], double U_diag[]) alpar@1: { int i, j, k, t, t1, beg, end, beg1, end1, count = 0; alpar@1: double ukk, uki, *work; alpar@1: work = xcalloc(1+n, sizeof(double)); alpar@1: for (j = 1; j <= n; j++) work[j] = 0.0; alpar@1: /* U := (upper triangle of A) */ alpar@1: /* note that the upper traingle of A is a subset of U */ alpar@1: for (i = 1; i <= n; i++) alpar@1: { beg = A_ptr[i], end = A_ptr[i+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: j = A_ind[t], work[j] = A_val[t]; alpar@1: beg = U_ptr[i], end = U_ptr[i+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: j = U_ind[t], U_val[t] = work[j], work[j] = 0.0; alpar@1: U_diag[i] = A_diag[i]; alpar@1: } alpar@1: /* main elimination loop */ alpar@1: for (k = 1; k <= n; k++) alpar@1: { /* transform k-th row of U */ alpar@1: ukk = U_diag[k]; alpar@1: if (ukk > 0.0) alpar@1: U_diag[k] = ukk = sqrt(ukk); alpar@1: else alpar@1: U_diag[k] = ukk = DBL_MAX, count++; alpar@1: /* (work) := (transformed k-th row) */ alpar@1: beg = U_ptr[k], end = U_ptr[k+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: work[U_ind[t]] = (U_val[t] /= ukk); alpar@1: /* transform other rows of U */ alpar@1: for (t = beg; t < end; t++) alpar@1: { i = U_ind[t]; alpar@1: xassert(i > k); alpar@1: /* (i-th row) := (i-th row) - u[k,i] * (k-th row) */ alpar@1: uki = work[i]; alpar@1: beg1 = U_ptr[i], end1 = U_ptr[i+1]; alpar@1: for (t1 = beg1; t1 < end1; t1++) alpar@1: U_val[t1] -= uki * work[U_ind[t1]]; alpar@1: U_diag[i] -= uki * uki; alpar@1: } alpar@1: /* (work) := 0 */ alpar@1: for (t = beg; t < end; t++) alpar@1: work[U_ind[t]] = 0.0; alpar@1: } alpar@1: xfree(work); alpar@1: return count; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- u_solve - solve upper triangular system U*x = b. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- void u_solve(int n, int U_ptr[], int U_ind[], double U_val[], alpar@1: -- double U_diag[], double x[]); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine u_solve solves an linear system U*x = b, where U is an alpar@1: -- upper triangular matrix. alpar@1: -- alpar@1: -- The parameter n is the order of matrix U. alpar@1: -- alpar@1: -- The matrix U without diagonal elements is specified in the arrays alpar@1: -- U_ptr, U_ind, and U_val in storage-by-rows format. Diagonal elements alpar@1: -- of U are specified in the array U_diag, where U_diag[0] is not used, alpar@1: -- U_diag[i] = u[i,i] for i = 1, ..., n. All these four arrays are not alpar@1: -- changed on exit. alpar@1: -- alpar@1: -- The right-hand side vector b is specified on entry in the array x, alpar@1: -- where x[0] is not used, and x[i] = b[i] for i = 1, ..., n. On exit alpar@1: -- the routine stores computed components of the vector of unknowns x alpar@1: -- in the array x in the same manner. */ alpar@1: alpar@1: void u_solve(int n, int U_ptr[], int U_ind[], double U_val[], alpar@1: double U_diag[], double x[]) alpar@1: { int i, t, beg, end; alpar@1: double temp; alpar@1: for (i = n; i >= 1; i--) alpar@1: { temp = x[i]; alpar@1: beg = U_ptr[i], end = U_ptr[i+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: temp -= U_val[t] * x[U_ind[t]]; alpar@1: xassert(U_diag[i] != 0.0); alpar@1: x[i] = temp / U_diag[i]; alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- ut_solve - solve lower triangular system U'*x = b. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmat.h" alpar@1: -- void ut_solve(int n, int U_ptr[], int U_ind[], double U_val[], alpar@1: -- double U_diag[], double x[]); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine ut_solve solves an linear system U'*x = b, where U is a alpar@1: -- matrix transposed to an upper triangular matrix. alpar@1: -- alpar@1: -- The parameter n is the order of matrix U. alpar@1: -- alpar@1: -- The matrix U without diagonal elements is specified in the arrays alpar@1: -- U_ptr, U_ind, and U_val in storage-by-rows format. Diagonal elements alpar@1: -- of U are specified in the array U_diag, where U_diag[0] is not used, alpar@1: -- U_diag[i] = u[i,i] for i = 1, ..., n. All these four arrays are not alpar@1: -- changed on exit. alpar@1: -- alpar@1: -- The right-hand side vector b is specified on entry in the array x, alpar@1: -- where x[0] is not used, and x[i] = b[i] for i = 1, ..., n. On exit alpar@1: -- the routine stores computed components of the vector of unknowns x alpar@1: -- in the array x in the same manner. */ alpar@1: alpar@1: void ut_solve(int n, int U_ptr[], int U_ind[], double U_val[], alpar@1: double U_diag[], double x[]) alpar@1: { int i, t, beg, end; alpar@1: double temp; alpar@1: for (i = 1; i <= n; i++) alpar@1: { xassert(U_diag[i] != 0.0); alpar@1: temp = (x[i] /= U_diag[i]); alpar@1: if (temp == 0.0) continue; alpar@1: beg = U_ptr[i], end = U_ptr[i+1]; alpar@1: for (t = beg; t < end; t++) alpar@1: x[U_ind[t]] -= U_val[t] * temp; alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /* eof */