amd_valid.c 3.3 KB

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