/* ========================================================================= */1/* === AMD: approximate minimum degree ordering =========================== */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 finds a symmetric ordering P of a matrix A so that the Cholesky12* factorization of P*A*P' has fewer nonzeros and takes less work than the13* Cholesky factorization of A. If A is not symmetric, then it performs its14* ordering on the matrix A+A'. Two sets of user-callable routines are15* provided, one for "int" integers and the other for "long" integers.16*17* The method is based on the approximate minimum degree algorithm, discussed18* in Amestoy, Davis, and Duff, "An approximate degree ordering algorithm",19* SIAM Journal of Matrix Analysis and Applications, vol. 17, no. 4, pp.20* 886-905, 1996. This package can perform both the AMD ordering (with21* aggressive absorption), and the AMDBAR ordering (without aggressive22* absorption) discussed in the above paper. This package differs from the23* Fortran codes discussed in the paper:24*25* (1) it can ignore "dense" rows and columns, leading to faster run times26* (2) it computes the ordering of A+A' if A is not symmetric27* (3) it is followed by a depth-first post-ordering of the assembly tree28* (or supernodal elimination tree)29*30* For historical reasons, the Fortran versions, amd.f and amdbar.f, have31* been left (nearly) unchanged. They compute the identical ordering as32* described in the above paper.33*/3435#ifndef AMD_H36#define AMD_H3738int amd_order ( /* returns 0 if OK, negative value if error */39int n, /* A is n-by-n. n must be >= 0. */40const int Ap [ ], /* column pointers for A, of size n+1 */41const int Ai [ ], /* row indices of A, of size nz = Ap [n] */42int P [ ], /* output permutation, of size n */43double Control [ ], /* input Control settings, of size AMD_CONTROL */44double Info [ ] /* output Info statistics, of size AMD_INFO */45) ;4647long amd_l_order ( /* see above for description of arguments */48long n,49const long Ap [ ],50const long Ai [ ],51long P [ ],52double Control [ ],53double Info [ ]54) ;5556/* Input arguments (not modified):57*58* n: the matrix A is n-by-n.59* Ap: an int/long array of size n+1, containing the column pointers of A.60* Ai: an int/long array of size nz, containing the row indices of A,61* where nz = Ap [n].62* Control: a double array of size AMD_CONTROL, containing control63* parameters. Defaults are used if Control is NULL.64*65* Output arguments (not defined on input):66*67* P: an int/long array of size n, containing the output permutation. If68* row i is the kth pivot row, then P [k] = i. In MATLAB notation,69* the reordered matrix is A (P,P).70* Info: a double array of size AMD_INFO, containing statistical71* information. Ignored if Info is NULL.72*73* On input, the matrix A is stored in column-oriented form. The row indices74* of nonzero entries in column j are stored in Ai [Ap [j] ... Ap [j+1]-1].75* The row indices must appear in ascending order in each column, and there76* must not be any duplicate entries. Row indices must be in the range 0 to77* n-1. Ap [0] must be zero, and thus nz = Ap [n] is the number of nonzeros78* in A. The array Ap is of size n+1, and the array Ai is of size nz = Ap [n].79* The matrix does not need to be symmetric, and the diagonal does not need to80* be present (if diagonal entries are present, they are ignored except for81* the output statistic Info [AMD_NZDIAG]). The arrays Ai and Ap are not82* modified. This form of the Ap and Ai arrays to represent the nonzero83* pattern of the matrix A is the same as that used internally by MATLAB.84* If you wish to use a more flexible input structure, please see the85* umfpack_*_triplet_to_col routines in the UMFPACK package, at86* http://www.cise.ufl.edu/research/sparse/umfpack, or use the amd_preprocess87* routine discussed below.88*89* Restrictions: n >= 0. Ap [0] = 0. Ap [j] <= Ap [j+1] for all j in the90* range 0 to n-1. nz = Ap [n] >= 0. For all j in the range 0 to n-1,91* and for all p in the range Ap [j] to Ap [j+1]-2, Ai [p] < Ai [p+1] must92* hold. Ai [0..nz-1] must be in the range 0 to n-1. To avoid integer93* overflow, (2.4*nz + 8*n) < INT_MAX / sizeof (int) for must hold for the94* "int" version. (2.4*nz + 8*n) < LONG_MAX / sizeof (long) must hold95* for the "long" version. Finally, Ai, Ap, and P must not be NULL. If96* any of these restrictions are not met, AMD returns AMD_INVALID.97*98* AMD returns:99*100* AMD_OK if the matrix is valid and sufficient memory can be allocated to101* perform the ordering.102*103* AMD_OUT_OF_MEMORY if not enough memory can be allocated.104*105* AMD_INVALID if the input arguments n, Ap, Ai are invalid, or if P is106* NULL.107*108* The AMD routine first forms the pattern of the matrix A+A', and then109* computes a fill-reducing ordering, P. If P [k] = i, then row/column i of110* the original is the kth pivotal row. In MATLAB notation, the permuted111* matrix is A (P,P), except that 0-based indexing is used instead of the112* 1-based indexing in MATLAB.113*114* The Control array is used to set various parameters for AMD. If a NULL115* pointer is passed, default values are used. The Control array is not116* modified.117*118* Control [AMD_DENSE]: controls the threshold for "dense" rows/columns.119* A dense row/column in A+A' can cause AMD to spend a lot of time in120* ordering the matrix. If Control [AMD_DENSE] >= 0, rows/columns121* with more than Control [AMD_DENSE] * sqrt (n) entries are ignored122* during the ordering, and placed last in the output order. The123* default value of Control [AMD_DENSE] is 10. If negative, no124* rows/columns are treated as "dense". Rows/columns with 16 or125* fewer off-diagonal entries are never considered "dense".126*127* Control [AMD_AGGRESSIVE]: controls whether or not to use aggressive128* absorption, in which a prior element is absorbed into the current129* element if is a subset of the current element, even if it is not130* adjacent to the current pivot element (refer to Amestoy, Davis,131* & Duff, 1996, for more details). The default value is nonzero,132* which means to perform aggressive absorption. This nearly always133* leads to a better ordering (because the approximate degrees are134* more accurate) and a lower execution time. There are cases where135* it can lead to a slightly worse ordering, however. To turn it off,136* set Control [AMD_AGGRESSIVE] to 0.137*138* Control [2..4] are not used in the current version, but may be used in139* future versions.140*141* The Info array provides statistics about the ordering on output. If it is142* not present, the statistics are not returned. This is not an error143* condition.144*145* Info [AMD_STATUS]: the return value of AMD, either AMD_OK,146* AMD_OUT_OF_MEMORY, or AMD_INVALID.147*148* Info [AMD_N]: n, the size of the input matrix149*150* Info [AMD_NZ]: the number of nonzeros in A, nz = Ap [n]151*152* Info [AMD_SYMMETRY]: the symmetry of the matrix A. It is the number153* of "matched" off-diagonal entries divided by the total number of154* off-diagonal entries. An entry A(i,j) is matched if A(j,i) is also155* an entry, for any pair (i,j) for which i != j. In MATLAB notation,156* S = spones (A) ;157* B = tril (S, -1) + triu (S, 1) ;158* symmetry = nnz (B & B') / nnz (B) ;159*160* Info [AMD_NZDIAG]: the number of entries on the diagonal of A.161*162* Info [AMD_NZ_A_PLUS_AT]: the number of nonzeros in A+A', excluding the163* diagonal. If A is perfectly symmetric (Info [AMD_SYMMETRY] = 1)164* with a fully nonzero diagonal, then Info [AMD_NZ_A_PLUS_AT] = nz-n165* (the smallest possible value). If A is perfectly unsymmetric166* (Info [AMD_SYMMETRY] = 0, for an upper triangular matrix, for167* example) with no diagonal, then Info [AMD_NZ_A_PLUS_AT] = 2*nz168* (the largest possible value).169*170* Info [AMD_NDENSE]: the number of "dense" rows/columns of A+A' that were171* removed from A prior to ordering. These are placed last in the172* output order P.173*174* Info [AMD_MEMORY]: the amount of memory used by AMD, in bytes. In the175* current version, this is 1.2 * Info [AMD_NZ_A_PLUS_AT] + 9*n176* times the size of an integer. This is at most 2.4nz + 9n. This177* excludes the size of the input arguments Ai, Ap, and P, which have178* a total size of nz + 2*n + 1 integers.179*180* Info [AMD_NCMPA]: the number of garbage collections performed.181*182* Info [AMD_LNZ]: the number of nonzeros in L (excluding the diagonal).183* This is a slight upper bound because mass elimination is combined184* with the approximate degree update. It is a rough upper bound if185* there are many "dense" rows/columns. The rest of the statistics,186* below, are also slight or rough upper bounds, for the same reasons.187* The post-ordering of the assembly tree might also not exactly188* correspond to a true elimination tree postordering.189*190* Info [AMD_NDIV]: the number of divide operations for a subsequent LDL'191* or LU factorization of the permuted matrix A (P,P).192*193* Info [AMD_NMULTSUBS_LDL]: the number of multiply-subtract pairs for a194* subsequent LDL' factorization of A (P,P).195*196* Info [AMD_NMULTSUBS_LU]: the number of multiply-subtract pairs for a197* subsequent LU factorization of A (P,P), assuming that no numerical198* pivoting is required.199*200* Info [AMD_DMAX]: the maximum number of nonzeros in any column of L,201* including the diagonal.202*203* Info [14..19] are not used in the current version, but may be used in204* future versions.205*/206207/* ------------------------------------------------------------------------- */208/* AMD preprocess */209/* ------------------------------------------------------------------------- */210211/* amd_preprocess: sorts, removes duplicate entries, and transposes the212* nonzero pattern of a column-form matrix A, to obtain the matrix R.213*214* Alternatively, you can consider this routine as constructing a row-form215* matrix from a column-form matrix. Duplicate entries are allowed in A (and216* removed in R). The columns of R are sorted. Checks its input A for errors.217*218* On input, A can have unsorted columns, and can have duplicate entries.219* Ap [0] must still be zero, and Ap must be monotonically nondecreasing.220* Row indices must be in the range 0 to n-1.221*222* On output, if this routine returns AMD_OK, then the matrix R is a valid223* input matrix for AMD_order. It has sorted columns, with no duplicate224* entries in each column. Since AMD_order operates on the matrix A+A', it225* can just as easily use A or A', so the transpose has no significant effect226* (except for minor tie-breaking, which can lead to a minor effect in the227* quality of the ordering). As an example, compare the output of amd_demo.c228* and amd_demo2.c.229*230* This routine transposes A to get R because that's the simplest way to231* sort and remove duplicate entries from a matrix.232*233* Allocates 2*n integer work arrays, and free's them when done.234*235* If you wish to call amd_order, but do not know if your matrix has unsorted236* columns or duplicate entries, then you can use the following code, which is237* fairly efficient. amd_order will not allocate any internal matrix until238* it checks that the input matrix is valid, so the method below is memory-239* efficient as well. This code snippet assumes that Rp and Ri are already240* allocated, and are the same size as Ap and Ai respectively.241242result = amd_order (n, p, Ap, Ai, Control, Info) ;243if (result == AMD_INVALID)244{245if (amd_preprocess (n, Ap, Ai, Rp, Ri) == AMD_OK)246{247result = amd_order (n, p, Rp, Ri, Control, Info) ;248}249}250251* amd_preprocess will still return AMD_INVALID if any row index in Ai is out252* of range or if the Ap array is invalid. These errors are not corrected by253* amd_preprocess since they represent a more serious error that should be254* flagged with the AMD_INVALID error code.255*/256257int amd_preprocess258(259int n,260const int Ap [ ],261const int Ai [ ],262int Rp [ ],263int Ri [ ]264) ;265266long amd_l_preprocess267(268long n,269const long Ap [ ],270const long Ai [ ],271long Rp [ ],272long Ri [ ]273) ;274275/* Input arguments (not modified):276*277* n: the matrix A is n-by-n.278* Ap: an int/long array of size n+1, containing the column pointers of A.279* Ai: an int/long array of size nz, containing the row indices of A,280* where nz = Ap [n].281* The nonzero pattern of column j of A is in Ai [Ap [j] ... Ap [j+1]-1].282* Ap [0] must be zero, and Ap [j] <= Ap [j+1] must hold for all j in the283* range 0 to n-1. Row indices in Ai must be in the range 0 to n-1.284* The row indices in any one column need not be sorted, and duplicates285* may exist.286*287* Output arguments (not defined on input):288*289* Rp: an int/long array of size n+1, containing the column pointers of R.290* Ri: an int/long array of size rnz, containing the row indices of R,291* where rnz = Rp [n]. Note that Rp [n] will be less than Ap [n] if292* duplicates appear in A. In general, Rp [n] <= Ap [n].293* The data structure for R is the same as A, except that each column of294* R contains sorted row indices, and no duplicates appear in any column.295*296* amd_preprocess returns:297*298* AMD_OK if the matrix A is valid and sufficient memory can be allocated299* to perform the preprocessing.300*301* AMD_OUT_OF_MEMORY if not enough memory can be allocated.302*303* AMD_INVALID if the input arguments n, Ap, Ai are invalid, or if Rp or304* Ri are NULL.305*/306307/* ------------------------------------------------------------------------- */308/* AMD Control and Info arrays */309/* ------------------------------------------------------------------------- */310311/* amd_defaults: sets the default control settings */312void amd_defaults (double Control [ ]) ;313void amd_l_defaults (double Control [ ]) ;314315/* amd_control: prints the control settings */316void amd_control (double Control [ ]) ;317void amd_l_control (double Control [ ]) ;318319/* amd_info: prints the statistics */320void amd_info (double Info [ ]) ;321void amd_l_info (double Info [ ]) ;322323#define AMD_CONTROL 5 /* size of Control array */324#define AMD_INFO 20 /* size of Info array */325326/* contents of Control */327#define AMD_DENSE 0 /* "dense" if degree > Control [0] * sqrt (n) */328#define AMD_AGGRESSIVE 1 /* do aggressive absorption if Control [1] != 0 */329330/* default Control settings */331#define AMD_DEFAULT_DENSE 10.0 /* default "dense" degree 10*sqrt(n) */332#define AMD_DEFAULT_AGGRESSIVE 1 /* do aggressive absorption by default */333334/* contents of Info */335#define AMD_STATUS 0 /* return value of amd_order and amd_l_order */336#define AMD_N 1 /* A is n-by-n */337#define AMD_NZ 2 /* number of nonzeros in A */338#define AMD_SYMMETRY 3 /* symmetry of pattern (1 is sym., 0 is unsym.) */339#define AMD_NZDIAG 4 /* # of entries on diagonal */340#define AMD_NZ_A_PLUS_AT 5 /* nz in A+A' */341#define AMD_NDENSE 6 /* number of "dense" rows/columns in A */342#define AMD_MEMORY 7 /* amount of memory used by AMD */343#define AMD_NCMPA 8 /* number of garbage collections in AMD */344#define AMD_LNZ 9 /* approx. nz in L, excluding the diagonal */345#define AMD_NDIV 10 /* number of fl. point divides for LU and LDL' */346#define AMD_NMULTSUBS_LDL 11 /* number of fl. point (*,-) pairs for LDL' */347#define AMD_NMULTSUBS_LU 12 /* number of fl. point (*,-) pairs for LU */348#define AMD_DMAX 13 /* max nz. in any column of L, incl. diagonal */349350/* ------------------------------------------------------------------------- */351/* return values of AMD */352/* ------------------------------------------------------------------------- */353354#define AMD_OK 0 /* success */355#define AMD_OUT_OF_MEMORY -1 /* malloc failed */356#define AMD_INVALID -2 /* input arguments are not valid */357358#endif359360361