src/amd/amd_valid.c
changeset 1 c445c931472f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/amd/amd_valid.c	Mon Dec 06 13:09:21 2010 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* ========================================================================= */
     1.5 +/* === AMD_valid =========================================================== */
     1.6 +/* ========================================================================= */
     1.7 +
     1.8 +/* ------------------------------------------------------------------------- */
     1.9 +/* AMD, Copyright (c) Timothy A. Davis,                                      */
    1.10 +/* Patrick R. Amestoy, and Iain S. Duff.  See ../README.txt for License.     */
    1.11 +/* email: davis at cise.ufl.edu    CISE Department, Univ. of Florida.        */
    1.12 +/* web: http://www.cise.ufl.edu/research/sparse/amd                          */
    1.13 +/* ------------------------------------------------------------------------- */
    1.14 +
    1.15 +/* Check if a column-form matrix is valid or not.  The matrix A is
    1.16 + * n_row-by-n_col.  The row indices of entries in column j are in
    1.17 + * Ai [Ap [j] ... Ap [j+1]-1].  Required conditions are:
    1.18 + *
    1.19 + *      n_row >= 0
    1.20 + *      n_col >= 0
    1.21 + *      nz = Ap [n_col] >= 0        number of entries in the matrix
    1.22 + *      Ap [0] == 0
    1.23 + *      Ap [j] <= Ap [j+1] for all j in the range 0 to n_col.
    1.24 + *      Ai [0 ... nz-1] must be in the range 0 to n_row-1.
    1.25 + *
    1.26 + * If any of the above conditions hold, AMD_INVALID is returned.  If the
    1.27 + * following condition holds, AMD_OK_BUT_JUMBLED is returned (a warning,
    1.28 + * not an error):
    1.29 + *
    1.30 + *      row indices in Ai [Ap [j] ... Ap [j+1]-1] are not sorted in ascending
    1.31 + *          order, and/or duplicate entries exist.
    1.32 + *
    1.33 + * Otherwise, AMD_OK is returned.
    1.34 + *
    1.35 + * In v1.2 and earlier, this function returned TRUE if the matrix was valid
    1.36 + * (now returns AMD_OK), or FALSE otherwise (now returns AMD_INVALID or
    1.37 + * AMD_OK_BUT_JUMBLED).
    1.38 + */
    1.39 +
    1.40 +#include "amd_internal.h"
    1.41 +
    1.42 +GLOBAL Int AMD_valid
    1.43 +(
    1.44 +    /* inputs, not modified on output: */
    1.45 +    Int n_row,          /* A is n_row-by-n_col */
    1.46 +    Int n_col,
    1.47 +    const Int Ap [ ],   /* column pointers of A, of size n_col+1 */
    1.48 +    const Int Ai [ ]    /* row indices of A, of size nz = Ap [n_col] */
    1.49 +)
    1.50 +{
    1.51 +    Int nz, j, p1, p2, ilast, i, p, result = AMD_OK ;
    1.52 +
    1.53 +    if (n_row < 0 || n_col < 0 || Ap == NULL || Ai == NULL)
    1.54 +    {
    1.55 +        return (AMD_INVALID) ;
    1.56 +    }
    1.57 +    nz = Ap [n_col] ;
    1.58 +    if (Ap [0] != 0 || nz < 0)
    1.59 +    {
    1.60 +        /* column pointers must start at Ap [0] = 0, and Ap [n] must be >= 0 */
    1.61 +        AMD_DEBUG0 (("column 0 pointer bad or nz < 0\n")) ;
    1.62 +        return (AMD_INVALID) ;
    1.63 +    }
    1.64 +    for (j = 0 ; j < n_col ; j++)
    1.65 +    {
    1.66 +        p1 = Ap [j] ;
    1.67 +        p2 = Ap [j+1] ;
    1.68 +        AMD_DEBUG2 (("\nColumn: "ID" p1: "ID" p2: "ID"\n", j, p1, p2)) ;
    1.69 +        if (p1 > p2)
    1.70 +        {
    1.71 +            /* column pointers must be ascending */
    1.72 +            AMD_DEBUG0 (("column "ID" pointer bad\n", j)) ;
    1.73 +            return (AMD_INVALID) ;
    1.74 +        }
    1.75 +        ilast = EMPTY ;
    1.76 +        for (p = p1 ; p < p2 ; p++)
    1.77 +        {
    1.78 +            i = Ai [p] ;
    1.79 +            AMD_DEBUG3 (("row: "ID"\n", i)) ;
    1.80 +            if (i < 0 || i >= n_row)
    1.81 +            {
    1.82 +                /* row index out of range */
    1.83 +                AMD_DEBUG0 (("index out of range, col "ID" row "ID"\n", j, i));
    1.84 +                return (AMD_INVALID) ;
    1.85 +            }
    1.86 +            if (i <= ilast)
    1.87 +            {
    1.88 +                /* row index unsorted, or duplicate entry present */
    1.89 +                AMD_DEBUG1 (("index unsorted/dupl col "ID" row "ID"\n", j, i));
    1.90 +                result = AMD_OK_BUT_JUMBLED ;
    1.91 +            }
    1.92 +            ilast = i ;
    1.93 +        }
    1.94 +    }
    1.95 +    return (result) ;
    1.96 +}