File: spok.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (135 lines) | stat: -rw-r--r-- 3,873 bytes parent folder | download | duplicates (2)
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
// SuiteSparse/MATLAB_Tools/spok/spok.c
// SPOK, Copyright (c) 2008-2011, Timothy A Davis. All Rights Reserved.
// SPDX-License-Identifier: BSD-3-clause

#include "spok.h"

/* check the validity of a MATLAB sparse matrix */

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 ;
        for (p = Ap [j] ; p < Ap [j+1] ; p++)
        {
            i = Ai [p] ;
            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] ;
            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) ;
}