File: spok.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 503; asm: 369; python: 125; awk: 10
file content (137 lines) | stat: -rw-r--r-- 4,004 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "spok.h"

/* check the validity of a built-in sparse matrix */

/* Copyright 2008-2011, Timothy A. Davis, http://suitesparse.com */
/* SPDX-License-Identifier: Apache-2.0 */

SPOK_INT spok
(
    /* inputs, not modified */
    SPOK_INT m,             /* number of rows */
    SPOK_INT n,             /* number of columns */
    SPOK_INT nzmax,         /* max # of entries */
    SPOK_INT *Ap,           /* size n+1, column pointers */
    SPOK_INT *Ai,           /* size nz = Ap [n], row indices */
    double *Ax,             /* double matrices always have Ax */
    double *Az,             /* imaginary matrices always have Az */
    char *As,               /* logical matrices always have As */

    /* outputs, not defined on input */
    SPOK_INT *p_njumbled,   /* # of jumbled row indices (-1 if not computed) */
    SPOK_INT *p_nzeros      /* number of explicit zeros (-1 if not computed) */
)
{
    double x, z ;
    SPOK_INT i, j, p, pend, njumbled, nzeros, ilast ;
    char s ;

    /* ---------------------------------------------------------------------- */
    /* in case of early return */
    /* ---------------------------------------------------------------------- */

    if (p_njumbled != NULL)
    {
        *p_njumbled = -1 ;
    }
    if (p_nzeros != NULL)
    {
        *p_nzeros = -1 ;
    }

    /* ---------------------------------------------------------------------- */
    /* check the dimensions */
    /* ---------------------------------------------------------------------- */

    if (m < 0)
    {
        return (SPOK_FATAL_M) ;
    }
    if (n < 0)
    {
        return (SPOK_FATAL_N) ;
    }
    if (nzmax < 1) 
    {
        /* note that nzmax cannot be zero */
        return (SPOK_FATAL_NZMAX) ;
    }

    /* ---------------------------------------------------------------------- */
    /* check the column pointers */
    /* ---------------------------------------------------------------------- */

    if (Ap == NULL || Ap [0] != 0)
    {
        /* column pointers invalid */
        return (SPOK_FATAL_P) ;
    }
    for (j = 0 ; j < n ; j++)
    {
        p = Ap [j] ;
        pend = Ap [j+1] ;
        if (pend < p || pend > nzmax)
        {
            /* column pointers not monotonically non-decreasing */
            return (SPOK_FATAL_P) ;
        }
    }

    /* ---------------------------------------------------------------------- */
    /* check the row indices and numerical values */
    /* ---------------------------------------------------------------------- */

    if (Ai == NULL)
    {
        /* row indices not present */
        return (SPOK_FATAL_I) ;
    }

    njumbled = 0 ;
    nzeros = 0 ;

    for (j = 0 ; j < n ; j++)
    {
        ilast = -1 ;
        // printf ("column %lld [%lld : %lld]\n", j, Ap [j], Ap [j+1]-1) ;
        for (p = Ap [j] ; p < Ap [j+1] ; p++)
        {
            i = Ai [p] ;
            // printf ("row %lld value: ", i) ;
            if (i < 0 || i >= m)
            {
                /* row indices out of range */
                return (SPOK_FATAL_I) ;
            }
            if (i <= ilast)
            {
                /* row indices unsorted, or duplicates present */
                njumbled++ ;
            }
            s = (As == NULL) ? 0 : As [p] ;
            x = (Ax == NULL) ? 0 : Ax [p] ;
            z = (Az == NULL) ? 0 : Az [p] ;
            // printf (" %d %g %g\n", s, x, z) ;
            if (s == 0 && x == 0 && z == 0)
            {
                /* an explicit zero is present */
                nzeros++ ;
            }
            ilast = i ;
        }
    }

    /* ---------------------------------------------------------------------- */
    /* return results */
    /* ---------------------------------------------------------------------- */

    if (p_njumbled != NULL)
    {
        *p_njumbled = njumbled ;
    }
    if (p_nzeros != NULL)
    {
        *p_nzeros = nzeros ;
    }
    return ((njumbled > 0 || nzeros > 0) ? SPOK_WARNING : SPOK_OK) ;
}