src/amd/amd_valid.c
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 05 Dec 2010 17:35:23 +0100
changeset 2 4c8956a7bdf4
permissions -rw-r--r--
Set up CMAKE build environment
alpar@1
     1
/* ========================================================================= */
alpar@1
     2
/* === AMD_valid =========================================================== */
alpar@1
     3
/* ========================================================================= */
alpar@1
     4
alpar@1
     5
/* ------------------------------------------------------------------------- */
alpar@1
     6
/* AMD, Copyright (c) Timothy A. Davis,                                      */
alpar@1
     7
/* Patrick R. Amestoy, and Iain S. Duff.  See ../README.txt for License.     */
alpar@1
     8
/* email: davis at cise.ufl.edu    CISE Department, Univ. of Florida.        */
alpar@1
     9
/* web: http://www.cise.ufl.edu/research/sparse/amd                          */
alpar@1
    10
/* ------------------------------------------------------------------------- */
alpar@1
    11
alpar@1
    12
/* Check if a column-form matrix is valid or not.  The matrix A is
alpar@1
    13
 * n_row-by-n_col.  The row indices of entries in column j are in
alpar@1
    14
 * Ai [Ap [j] ... Ap [j+1]-1].  Required conditions are:
alpar@1
    15
 *
alpar@1
    16
 *      n_row >= 0
alpar@1
    17
 *      n_col >= 0
alpar@1
    18
 *      nz = Ap [n_col] >= 0        number of entries in the matrix
alpar@1
    19
 *      Ap [0] == 0
alpar@1
    20
 *      Ap [j] <= Ap [j+1] for all j in the range 0 to n_col.
alpar@1
    21
 *      Ai [0 ... nz-1] must be in the range 0 to n_row-1.
alpar@1
    22
 *
alpar@1
    23
 * If any of the above conditions hold, AMD_INVALID is returned.  If the
alpar@1
    24
 * following condition holds, AMD_OK_BUT_JUMBLED is returned (a warning,
alpar@1
    25
 * not an error):
alpar@1
    26
 *
alpar@1
    27
 *      row indices in Ai [Ap [j] ... Ap [j+1]-1] are not sorted in ascending
alpar@1
    28
 *          order, and/or duplicate entries exist.
alpar@1
    29
 *
alpar@1
    30
 * Otherwise, AMD_OK is returned.
alpar@1
    31
 *
alpar@1
    32
 * In v1.2 and earlier, this function returned TRUE if the matrix was valid
alpar@1
    33
 * (now returns AMD_OK), or FALSE otherwise (now returns AMD_INVALID or
alpar@1
    34
 * AMD_OK_BUT_JUMBLED).
alpar@1
    35
 */
alpar@1
    36
alpar@1
    37
#include "amd_internal.h"
alpar@1
    38
alpar@1
    39
GLOBAL Int AMD_valid
alpar@1
    40
(
alpar@1
    41
    /* inputs, not modified on output: */
alpar@1
    42
    Int n_row,          /* A is n_row-by-n_col */
alpar@1
    43
    Int n_col,
alpar@1
    44
    const Int Ap [ ],   /* column pointers of A, of size n_col+1 */
alpar@1
    45
    const Int Ai [ ]    /* row indices of A, of size nz = Ap [n_col] */
alpar@1
    46
)
alpar@1
    47
{
alpar@1
    48
    Int nz, j, p1, p2, ilast, i, p, result = AMD_OK ;
alpar@1
    49
alpar@1
    50
    if (n_row < 0 || n_col < 0 || Ap == NULL || Ai == NULL)
alpar@1
    51
    {
alpar@1
    52
        return (AMD_INVALID) ;
alpar@1
    53
    }
alpar@1
    54
    nz = Ap [n_col] ;
alpar@1
    55
    if (Ap [0] != 0 || nz < 0)
alpar@1
    56
    {
alpar@1
    57
        /* column pointers must start at Ap [0] = 0, and Ap [n] must be >= 0 */
alpar@1
    58
        AMD_DEBUG0 (("column 0 pointer bad or nz < 0\n")) ;
alpar@1
    59
        return (AMD_INVALID) ;
alpar@1
    60
    }
alpar@1
    61
    for (j = 0 ; j < n_col ; j++)
alpar@1
    62
    {
alpar@1
    63
        p1 = Ap [j] ;
alpar@1
    64
        p2 = Ap [j+1] ;
alpar@1
    65
        AMD_DEBUG2 (("\nColumn: "ID" p1: "ID" p2: "ID"\n", j, p1, p2)) ;
alpar@1
    66
        if (p1 > p2)
alpar@1
    67
        {
alpar@1
    68
            /* column pointers must be ascending */
alpar@1
    69
            AMD_DEBUG0 (("column "ID" pointer bad\n", j)) ;
alpar@1
    70
            return (AMD_INVALID) ;
alpar@1
    71
        }
alpar@1
    72
        ilast = EMPTY ;
alpar@1
    73
        for (p = p1 ; p < p2 ; p++)
alpar@1
    74
        {
alpar@1
    75
            i = Ai [p] ;
alpar@1
    76
            AMD_DEBUG3 (("row: "ID"\n", i)) ;
alpar@1
    77
            if (i < 0 || i >= n_row)
alpar@1
    78
            {
alpar@1
    79
                /* row index out of range */
alpar@1
    80
                AMD_DEBUG0 (("index out of range, col "ID" row "ID"\n", j, i));
alpar@1
    81
                return (AMD_INVALID) ;
alpar@1
    82
            }
alpar@1
    83
            if (i <= ilast)
alpar@1
    84
            {
alpar@1
    85
                /* row index unsorted, or duplicate entry present */
alpar@1
    86
                AMD_DEBUG1 (("index unsorted/dupl col "ID" row "ID"\n", j, i));
alpar@1
    87
                result = AMD_OK_BUT_JUMBLED ;
alpar@1
    88
            }
alpar@1
    89
            ilast = i ;
alpar@1
    90
        }
alpar@1
    91
    }
alpar@1
    92
    return (result) ;
alpar@1
    93
}