/* ========================================================================= */1/* === AMD_aat ============================================================= */2/* ========================================================================= */34/* ------------------------------------------------------------------------- */5/* AMD Version 1.1 (Jan. 21, 2004), Copyright (c) 2004 by Timothy A. Davis, */6/* Patrick R. Amestoy, and Iain S. Duff. See ../README for License. */7/* email: [email protected] CISE Department, Univ. of Florida. */8/* web: http://www.cise.ufl.edu/research/sparse/amd */9/* ------------------------------------------------------------------------- */1011/* AMD_aat: compute the symmetry of the pattern of A, and count the number of12* nonzeros each column of A+A' (excluding the diagonal). Assume the input13* matrix has no errors.14*/1516#include "amd_internal.h"1718GLOBAL Int AMD_aat /* returns nz in A+A' */19(20Int n,21const Int Ap [ ],22const Int Ai [ ],23Int Len [ ], /* Len [j]: length of column j of A+A', excl diagonal*/24Int Tp [ ], /* workspace of size n */25double Info [ ]26)27{28Int p1, p2, p, i, j, pj, pj2, k, nzdiag, nzboth, nz, nzaat ;29double sym ;3031#ifndef NDEBUG32AMD_debug_init ("AMD AAT") ;33for (k = 0 ; k < n ; k++) Tp [k] = EMPTY ;34ASSERT (AMD_valid (n, n, Ap, Ai)) ;35#endif3637if (Info != (double *) NULL)38{39/* clear the Info array, if it exists */40for (i = 0 ; i < AMD_INFO ; i++)41{42Info [i] = EMPTY ;43}44Info [AMD_STATUS] = AMD_OK ;45}4647for (k = 0 ; k < n ; k++)48{49Len [k] = 0 ;50}5152nzdiag = 0 ;53nzboth = 0 ;54nz = Ap [n] ;5556for (k = 0 ; k < n ; k++)57{58p1 = Ap [k] ;59p2 = Ap [k+1] ;60AMD_DEBUG2 (("\nAAT Column: "ID" p1: "ID" p2: "ID"\n", k, p1, p2)) ;6162/* construct A+A' */63for (p = p1 ; p < p2 ; )64{65/* scan the upper triangular part of A */66j = Ai [p] ;67if (j < k)68{69/* entry A (j,k) is in the strictly upper triangular part,70* add both A (j,k) and A (k,j) to the matrix A+A' */71Len [j]++ ;72Len [k]++ ;73AMD_DEBUG3 ((" upper ("ID","ID") ("ID","ID")\n", j,k, k,j));74p++ ;75}76else if (j == k)77{78/* skip the diagonal */79p++ ;80nzdiag++ ;81break ;82}83else /* j > k */84{85/* first entry below the diagonal */86break ;87}88/* scan lower triangular part of A, in column j until reaching89* row k. Start where last scan left off. */90ASSERT (Tp [j] != EMPTY) ;91ASSERT (Ap [j] <= Tp [j] && Tp [j] <= Ap [j+1]) ;92pj2 = Ap [j+1] ;93for (pj = Tp [j] ; pj < pj2 ; )94{95i = Ai [pj] ;96if (i < k)97{98/* A (i,j) is only in the lower part, not in upper.99* add both A (i,j) and A (j,i) to the matrix A+A' */100Len [i]++ ;101Len [j]++ ;102AMD_DEBUG3 ((" lower ("ID","ID") ("ID","ID")\n",103i,j, j,i)) ;104pj++ ;105}106else if (i == k)107{108/* entry A (k,j) in lower part and A (j,k) in upper */109pj++ ;110nzboth++ ;111break ;112}113else /* i > k */114{115/* consider this entry later, when k advances to i */116break ;117}118}119Tp [j] = pj ;120}121/* Tp [k] points to the entry just below the diagonal in column k */122Tp [k] = p ;123}124125/* clean up, for remaining mismatched entries */126for (j = 0 ; j < n ; j++)127{128for (pj = Tp [j] ; pj < Ap [j+1] ; pj++)129{130i = Ai [pj] ;131/* A (i,j) is only in the lower part, not in upper.132* add both A (i,j) and A (j,i) to the matrix A+A' */133Len [i]++ ;134Len [j]++ ;135AMD_DEBUG3 ((" lower cleanup ("ID","ID") ("ID","ID")\n",136i,j, j,i)) ;137}138}139140/* --------------------------------------------------------------------- */141/* compute the symmetry of the nonzero pattern of A */142/* --------------------------------------------------------------------- */143144/* Given a matrix A, the symmetry of A is:145* B = tril (spones (A), -1) + triu (spones (A), 1) ;146* sym = nnz (B & B') / nnz (B) ;147* or 1 if nnz (B) is zero.148*/149150if (nz == nzdiag)151{152sym = 1 ;153}154else155{156sym = ((double) (2 * nzboth)) / ((double) (nz - nzdiag)) ;157}158159nzaat = 0 ;160for (k = 0 ; k < n ; k++)161{162nzaat += Len [k] ;163}164AMD_DEBUG1 (("AMD nz in A+A', excluding diagonal (nzaat) = "ID"\n",nzaat));165AMD_DEBUG1 ((" nzboth: "ID" nz: "ID" nzdiag: "ID" symmetry: %g\n",166nzboth, nz, nzdiag, sym)) ;167168if (Info != (double *) NULL)169{170Info [AMD_STATUS] = AMD_OK ;171Info [AMD_N] = n ;172Info [AMD_NZ] = nz ;173Info [AMD_SYMMETRY] = sym ; /* symmetry of pattern of A */174Info [AMD_NZDIAG] = nzdiag ; /* nonzeros on diagonal of A */175Info [AMD_NZ_A_PLUS_AT] = nzaat ; /* nonzeros in A+A' */176}177178return (nzaat) ;179}180181182