1 /* ========================================================================= */
2 /* === AMD_1 =============================================================== */
3 /* ========================================================================= */
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 /* ------------------------------------------------------------------------- */
12 /* AMD_1: Construct A+A' for a sparse matrix A and perform the AMD ordering.
14 * The n-by-n sparse matrix A can be unsymmetric. It is stored in MATLAB-style
15 * compressed-column form, with sorted row indices in each column, and no
16 * duplicate entries. Diagonal entries may be present, but they are ignored.
17 * Row indices of column j of A are stored in Ai [Ap [j] ... Ap [j+1]-1].
18 * Ap [0] must be zero, and nz = Ap [n] is the number of entries in A. The
19 * size of the matrix, n, must be greater than or equal to zero.
21 * This routine must be preceded by a call to AMD_aat, which computes the
22 * number of entries in each row/column in A+A', excluding the diagonal.
23 * Len [j], on input, is the number of entries in row/column j of A+A'. This
24 * routine constructs the matrix A+A' and then calls AMD_2. No error checking
25 * is performed (this was done in AMD_valid).
28 #include "amd_internal.h"
33 const Int Ap [ ], /* input of size n+1, not modified */
34 const Int Ai [ ], /* input of size nz = Ap [n], not modified */
35 Int P [ ], /* size n output permutation */
36 Int Pinv [ ], /* size n output inverse permutation */
37 Int Len [ ], /* size n input, undefined on output */
38 Int slen, /* slen >= sum (Len [0..n-1]) + 7n,
39 * ideally slen = 1.2 * sum (Len) + 8n */
40 Int S [ ], /* size slen workspace */
41 double Control [ ], /* input array of size AMD_CONTROL */
42 double Info [ ] /* output array of size AMD_INFO */
45 Int i, j, k, p, pfree, iwlen, pj, p1, p2, pj2, *Iw, *Pe, *Nv, *Head,
46 *Elen, *Degree, *s, *W, *Sp, *Tp ;
48 /* --------------------------------------------------------------------- */
49 /* construct the matrix for AMD_2 */
50 /* --------------------------------------------------------------------- */
64 ASSERT (AMD_valid (n, n, Ap, Ai) == AMD_OK) ;
66 /* construct the pointers for A+A' */
67 Sp = Nv ; /* use Nv and W as workspace for Sp and Tp [ */
70 for (j = 0 ; j < n ; j++)
77 /* Note that this restriction on iwlen is slightly more restrictive than
78 * what is strictly required in AMD_2. AMD_2 can operate with no elbow
79 * room at all, but it will be very slow. For better performance, at
80 * least size-n elbow room is enforced. */
81 ASSERT (iwlen >= pfree + n) ;
84 for (p = 0 ; p < iwlen ; p++) Iw [p] = EMPTY ;
87 for (k = 0 ; k < n ; k++)
89 AMD_DEBUG1 (("Construct row/column k= "ID" of A+A'\n", k)) ;
94 for (p = p1 ; p < p2 ; )
96 /* scan the upper triangular part of A */
98 ASSERT (j >= 0 && j < n) ;
101 /* entry A (j,k) in the strictly upper triangular part */
102 ASSERT (Sp [j] < (j == n-1 ? pfree : Pe [j+1])) ;
103 ASSERT (Sp [k] < (k == n-1 ? pfree : Pe [k+1])) ;
110 /* skip the diagonal */
116 /* first entry below the diagonal */
119 /* scan lower triangular part of A, in column j until reaching
120 * row k. Start where last scan left off. */
121 ASSERT (Ap [j] <= Tp [j] && Tp [j] <= Ap [j+1]) ;
123 for (pj = Tp [j] ; pj < pj2 ; )
126 ASSERT (i >= 0 && i < n) ;
129 /* A (i,j) is only in the lower part, not in upper */
130 ASSERT (Sp [i] < (i == n-1 ? pfree : Pe [i+1])) ;
131 ASSERT (Sp [j] < (j == n-1 ? pfree : Pe [j+1])) ;
138 /* entry A (k,j) in lower part and A (j,k) in upper */
144 /* consider this entry later, when k advances to i */
153 /* clean up, for remaining mismatched entries */
154 for (j = 0 ; j < n ; j++)
156 for (pj = Tp [j] ; pj < Ap [j+1] ; pj++)
159 ASSERT (i >= 0 && i < n) ;
160 /* A (i,j) is only in the lower part, not in upper */
161 ASSERT (Sp [i] < (i == n-1 ? pfree : Pe [i+1])) ;
162 ASSERT (Sp [j] < (j == n-1 ? pfree : Pe [j+1])) ;
169 for (j = 0 ; j < n-1 ; j++) ASSERT (Sp [j] == Pe [j+1]) ;
170 ASSERT (Sp [n-1] == pfree) ;
173 /* Tp and Sp no longer needed ] */
175 /* --------------------------------------------------------------------- */
176 /* order the matrix */
177 /* --------------------------------------------------------------------- */
179 AMD_2 (n, Pe, Iw, Len, iwlen, pfree,
180 Nv, Pinv, P, Head, Elen, Degree, W, Control, Info) ;