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