Amesos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
amesos_amd_l_aat.c
Go to the documentation of this file.
1 /* ========================================================================= */
2 /* === AMD_aat ============================================================= */
3 /* ========================================================================= */
4 
5 /* ------------------------------------------------------------------------- */
6 /* AMD, Copyright (c) Timothy A. Davis, */
7 /* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
8 /* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
9 /* web: http://www.cise.ufl.edu/research/sparse/amd */
10 /* ------------------------------------------------------------------------- */
11 
12 /* AMD_aat: compute the symmetry of the pattern of A, and count the number of
13  * nonzeros each column of A+A' (excluding the diagonal). Assumes the input
14  * matrix has no errors, with sorted columns and no duplicates
15  * (AMD_valid (n, n, Ap, Ai) must be AMD_OK, but this condition is not
16  * checked).
17  */
18 
19 /* This file should make the long int version of AMD */
20 #define DLONG 1
21 
22 #include "amesos_amd_internal.h"
23 
24 GLOBAL size_t AMD_aat /* returns nz in A+A' */
25 (
26  Int n,
27  const Int Ap [ ],
28  const Int Ai [ ],
29  Int Len [ ], /* Len [j]: length of column j of A+A', excl diagonal*/
30  Int Tp [ ], /* workspace of size n */
31  double Info [ ]
32 )
33 {
34  Int p1, p2, p, i, j, pj, pj2, k, nzdiag, nzboth, nz ;
35  double sym ;
36  size_t nzaat ;
37 
38 #ifndef NDEBUG
39  AMD_debug_init ("AMD AAT") ;
40  for (k = 0 ; k < n ; k++) Tp [k] = EMPTY ;
41  ASSERT (AMD_valid (n, n, Ap, Ai) == AMD_OK) ;
42 #endif
43 
44  if (Info != (double *) NULL)
45  {
46  /* clear the Info array, if it exists */
47  for (i = 0 ; i < AMD_INFO ; i++)
48  {
49  Info [i] = EMPTY ;
50  }
51  Info [AMD_STATUS] = AMD_OK ;
52  }
53 
54  for (k = 0 ; k < n ; k++)
55  {
56  Len [k] = 0 ;
57  }
58 
59  nzdiag = 0 ;
60  nzboth = 0 ;
61  nz = Ap [n] ;
62 
63  for (k = 0 ; k < n ; k++)
64  {
65  p1 = Ap [k] ;
66  p2 = Ap [k+1] ;
67  AMD_DEBUG2 (("\nAAT Column: "ID" p1: "ID" p2: "ID"\n", k, p1, p2)) ;
68 
69  /* construct A+A' */
70  for (p = p1 ; p < p2 ; )
71  {
72  /* scan the upper triangular part of A */
73  j = Ai [p] ;
74  if (j < k)
75  {
76  /* entry A (j,k) is in the strictly upper triangular part,
77  * add both A (j,k) and A (k,j) to the matrix A+A' */
78  Len [j]++ ;
79  Len [k]++ ;
80  AMD_DEBUG3 ((" upper ("ID","ID") ("ID","ID")\n", j,k, k,j));
81  p++ ;
82  }
83  else if (j == k)
84  {
85  /* skip the diagonal */
86  p++ ;
87  nzdiag++ ;
88  break ;
89  }
90  else /* j > k */
91  {
92  /* first entry below the diagonal */
93  break ;
94  }
95  /* scan lower triangular part of A, in column j until reaching
96  * row k. Start where last scan left off. */
97  ASSERT (Tp [j] != EMPTY) ;
98  ASSERT (Ap [j] <= Tp [j] && Tp [j] <= Ap [j+1]) ;
99  pj2 = Ap [j+1] ;
100  for (pj = Tp [j] ; pj < pj2 ; )
101  {
102  i = Ai [pj] ;
103  if (i < k)
104  {
105  /* A (i,j) is only in the lower part, not in upper.
106  * add both A (i,j) and A (j,i) to the matrix A+A' */
107  Len [i]++ ;
108  Len [j]++ ;
109  AMD_DEBUG3 ((" lower ("ID","ID") ("ID","ID")\n",
110  i,j, j,i)) ;
111  pj++ ;
112  }
113  else if (i == k)
114  {
115  /* entry A (k,j) in lower part and A (j,k) in upper */
116  pj++ ;
117  nzboth++ ;
118  break ;
119  }
120  else /* i > k */
121  {
122  /* consider this entry later, when k advances to i */
123  break ;
124  }
125  }
126  Tp [j] = pj ;
127  }
128  /* Tp [k] points to the entry just below the diagonal in column k */
129  Tp [k] = p ;
130  }
131 
132  /* clean up, for remaining mismatched entries */
133  for (j = 0 ; j < n ; j++)
134  {
135  for (pj = Tp [j] ; pj < Ap [j+1] ; pj++)
136  {
137  i = Ai [pj] ;
138  /* A (i,j) is only in the lower part, not in upper.
139  * add both A (i,j) and A (j,i) to the matrix A+A' */
140  Len [i]++ ;
141  Len [j]++ ;
142  AMD_DEBUG3 ((" lower cleanup ("ID","ID") ("ID","ID")\n",
143  i,j, j,i)) ;
144  }
145  }
146 
147  /* --------------------------------------------------------------------- */
148  /* compute the symmetry of the nonzero pattern of A */
149  /* --------------------------------------------------------------------- */
150 
151  /* Given a matrix A, the symmetry of A is:
152  * B = tril (spones (A), -1) + triu (spones (A), 1) ;
153  * sym = nnz (B & B') / nnz (B) ;
154  * or 1 if nnz (B) is zero.
155  */
156 
157  if (nz == nzdiag)
158  {
159  sym = 1 ;
160  }
161  else
162  {
163  sym = (2 * (double) nzboth) / ((double) (nz - nzdiag)) ;
164  }
165 
166  nzaat = 0 ;
167  for (k = 0 ; k < n ; k++)
168  {
169  nzaat += Len [k] ;
170  }
171 
172  AMD_DEBUG1 (("AMD nz in A+A', excluding diagonal (nzaat) = %g\n",
173  (double) nzaat)) ;
174  AMD_DEBUG1 ((" nzboth: "ID" nz: "ID" nzdiag: "ID" symmetry: %g\n",
175  nzboth, nz, nzdiag, sym)) ;
176 
177  if (Info != (double *) NULL)
178  {
179  Info [AMD_STATUS] = AMD_OK ;
180  Info [AMD_N] = n ;
181  Info [AMD_NZ] = nz ;
182  Info [AMD_SYMMETRY] = sym ; /* symmetry of pattern of A */
183  Info [AMD_NZDIAG] = nzdiag ; /* nonzeros on diagonal of A */
184  Info [AMD_NZ_A_PLUS_AT] = nzaat ; /* nonzeros in A+A' */
185  }
186 
187  return (nzaat) ;
188 }
#define EMPTY
#define GLOBAL
#define AMD_DEBUG1(params)
#define AMD_DEBUG3(params)
#define Int
#define AMD_NZ_A_PLUS_AT
Definition: amesos_amd.h:357
#define AMD_STATUS
Definition: amesos_amd.h:352
#define NULL
#define AMD_NZDIAG
Definition: amesos_amd.h:356
#define ASSERT(expression)
#define ID
#define AMD_DEBUG2(params)
#define AMD_OK
Definition: amesos_amd.h:371
#define AMD_valid
#define AMD_N
Definition: amesos_amd.h:353
#define AMD_NZ
Definition: amesos_amd.h:354
GLOBAL size_t AMD_aat(Int n, const Int Ap[], const Int Ai[], Int Len[], Int Tp[], double Info[])
#define AMD_SYMMETRY
Definition: amesos_amd.h:355
int n
#define AMD_debug_init
#define AMD_INFO
Definition: amesos_amd.h:341