File: RBdemo.c

package info (click to toggle)
suitesparse 1%3A5.8.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 152,716 kB
  • sloc: ansic: 774,385; cpp: 24,213; makefile: 6,310; fortran: 1,927; java: 1,826; csh: 1,686; ruby: 725; sh: 535; perl: 225; python: 209; sed: 164; awk: 60
file content (252 lines) | stat: -rw-r--r-- 7,897 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* ========================================================================== */
/* === RBio/Demo/RBdemo.c: C demo =========================================== */
/* ========================================================================== */

/* Copyright 2009, Timothy A. Davis, All Rights Reserved.
   Refer to RBio/Doc/license.txt for the RBio license. */

/* This program reads a Rutherford/Boeing file from stdin and uses it
   to test the RBio C-callable functions. */

#include "RBio.h"
#define Long SuiteSparse_long
#define ID "%ld"

#define SLEN 4096

int main (int argc, char **argv)
{
    double xr, xz, xmin, xmax ;
    double *Ax, *Az ;
    Long nrow, ncol, mkind, skind, *Ap, *Ai, i, *Zp, *Zi, asize, mkind2, skind2,
        znz, j, p, status, njumbled, nzeros, build_upper, zero_handling, fem,
        xsize, nelnz, nnz ;
    char title [73], key [9], mtype [4], mtype2 [4], *filename ;

    /* initialize the memory allocation functions */
    SuiteSparse_start ( ) ;

    /* read from stdin, or from file given by first argument */
    filename = (argc > 1) ? argv [1] : NULL ;

    title [0] = '\0' ;
    build_upper = 1 ;
    zero_handling = 2 ;     /* extract zeros */
    status = RBread (filename, build_upper, zero_handling, title, key, mtype,
        &nrow, &ncol, &mkind, &skind, &asize, &znz,
        &Ap, &Ai, &Ax, &Az, &Zp, &Zi) ;

    printf ("\n===========================================================\n") ;
    printf ("title: [%s]\n", title) ;
    printf ("status " ID "\n", status) ;
    if (status != RBIO_OK)
    {
        printf ("RBread Error! "ID"\n", status) ;
        SuiteSparse_free (Ap) ;
        SuiteSparse_free (Ai) ;
        SuiteSparse_free (Ax) ;
        SuiteSparse_free (Az) ;
        SuiteSparse_free (Zp) ;
        SuiteSparse_free (Zi) ;

        if (filename != NULL)
        {
            status = RBreadraw (filename, title, key, mtype, &nrow, &ncol, &nnz,
                &nelnz, &mkind, &skind, &fem, &xsize, &Ap, &Ai, &Ax) ;
            if (status != RBIO_OK)
            {
                printf ("RBreadraw error! status "ID"\n", status) ;
                return (1) ;
            }
            printf ("raw nrow "ID" ncol "ID" nnz "ID" nelnz "ID" xsize "ID"\n",
                nrow, ncol, nnz, nelnz, xsize) ;
            SuiteSparse_free (Ap) ;
            SuiteSparse_free (Ai) ;
            SuiteSparse_free (Ax) ;
            return (0) ;
        }
        return (1) ;
    }

    printf ("key [%s] nrow "ID" ncol "ID "\n", key, nrow, ncol) ;
    printf ("mkind "ID" skind "ID" asize "ID" znz "ID"\n",
        mkind, skind, asize, znz) ;
    printf ("mtype from file:               [%s]\n", mtype) ;

    if (Ap [ncol] < 20)
    {
        for (j = 0 ; j < ncol ; j++)
        {
            printf ("\ncolumn "ID":\n", j) ;
            for (p = Ap [j] ; p < Ap [j+1] ; p++)
            {
                i = Ai [p] ;
                RBget_entry (mkind, Ax, Az, p, &xr, &xz) ;
                printf ("  "ID" : %g", i, xr) ;
                if (xz != 0) printf (" + (%g)i\n", xz) ;
                printf ("\n") ;
            }
        }
    }

    status = RBok (nrow, ncol, asize, Ap, Ai, Ax, Az, NULL, mkind,
        &njumbled, &nzeros);
    printf ("(A) RBok status "ID" "ID" "ID"\n", status, njumbled, nzeros) ;
    if (status != RBIO_OK || njumbled != 0 || nzeros != 0)
    {
        printf ("RBok Error!\n") ;
        return (1) ;
    }

    status = RBok (nrow, ncol, asize, Zp, Zi, NULL, NULL, NULL, 1,
        &njumbled, &nzeros) ;
    printf ("(Z) RBok status "ID" "ID" "ID"\n", status, njumbled, nzeros) ;
    if (status != RBIO_OK || njumbled != 0 || nzeros != 0)
    {
        printf ("RBok Error (Z)!\n") ;
        return (1) ;
    }

    status = RBwrite ("temp.rb", title, key, nrow, ncol, Ap, Ai, Ax, Az, Zp, Zi,
        mkind, mtype2) ;
    if (status != RBIO_OK)
    {
        printf ("RBwrite error!\n") ;
    }

    status = RBkind (nrow, ncol, Ap, Ai, Ax, Az, mkind, &mkind2, &skind2,
        mtype2, &xmin, &xmax, NULL) ;
    if (status != RBIO_OK)
    {
        printf ("RBkind error!\n") ;
        return (1) ;
    }

    printf ("mtype from examining matrix:   [%s]\n", mtype2) ;

    SuiteSparse_free (Ap) ;
    SuiteSparse_free (Ai) ;
    SuiteSparse_free (Ax) ;
    SuiteSparse_free (Az) ;
    SuiteSparse_free (Zp) ;
    SuiteSparse_free (Zi) ;

    zero_handling = 1 ;     /* prune zeros */
    status = RBread ("temp.rb", build_upper, zero_handling, title, key, mtype,
        &nrow, &ncol, &mkind, &skind, &asize, &znz,
        &Ap, &Ai, &Ax, NULL, NULL, NULL) ;
    if (status != 0)
    {
        SuiteSparse_free (Ap) ;
        SuiteSparse_free (Ai) ;
        SuiteSparse_free (Ax) ;
        printf ("RBread Error! "ID"\n", status) ;
        return (1) ;
    }

    status = RBkind (nrow, ncol, Ap, Ai, Ax, NULL, mkind, &mkind2, &skind2,
        mtype2, &xmin, &xmax, NULL) ;
    if (status != RBIO_OK)
    {
        printf ("RBkind error!\n") ;
    }
    printf ("mtype after reading from file: [%s]\n", mtype2) ;

    SuiteSparse_free (Ap) ;
    SuiteSparse_free (Ai) ;
    SuiteSparse_free (Ax) ;

    status = RBreadraw ("temp.rb", title, key, mtype, &nrow, &ncol, &nnz,
        &nelnz, &mkind, &skind, &fem, &xsize, &Ap, &Ai, &Ax) ;
    if (status != RBIO_OK)
    {
        printf ("RBreadraw error!\n") ;
        return (1) ;
    }
    printf ("raw nrow "ID" ncol "ID" nnz "ID" nelnz "ID" xsize "ID"\n",
        nrow, ncol, nnz, nelnz, xsize) ;

    if (fem)
    {
        printf ("FEM matrix\n") ;
    }
    else
    {
        status = RBkind (nrow, ncol, Ap, Ai, Ax, NULL, mkind, &mkind2, &skind2,
            mtype2, &xmin, &xmax, NULL) ;
        if (status != RBIO_OK)
        {
            printf ("RBkind error!\n") ;
        }
        printf ("mtype from raw read from file: [%s]\n", mtype2) ;
    }

    SuiteSparse_free (Ap) ;
    SuiteSparse_free (Ai) ;
    SuiteSparse_free (Ax) ;

    /* read in just the pattern */
    status = RBread ("temp.rb", build_upper, zero_handling, title, key, mtype,
        &nrow, &ncol, &mkind, &skind, &asize, &znz,
        &Ap, &Ai, NULL, NULL, NULL, NULL) ;

    status = RBok (nrow, ncol, asize, Ap, Ai, Ax, Az, NULL, mkind,
        &njumbled, &nzeros);
    printf ("(P) RBok status "ID" "ID" "ID"\n", status, njumbled, nzeros) ;
    if (status != RBIO_OK || njumbled != 0 || nzeros != 0)
    {
        printf ("RBok Error!\n") ;
        return (1) ;
    }

    status = RBkind (nrow, ncol, Ap, Ai, NULL, NULL, mkind, &mkind2, &skind2,
        mtype2, &xmin, &xmax, NULL) ;
    if (status != RBIO_OK)
    {
        printf ("RBkind error!\n") ;
        return (1) ;
    }
    printf ("mtype of pattern:              [%s]\n", mtype2) ;


    SuiteSparse_free (Ap) ;
    SuiteSparse_free (Ai) ;

    /* read in just the lower triangular part for symmetric case */
    build_upper = 0 ;
    zero_handling = 0 ;     /* ignore zeros */
    status = RBread ("temp.rb", build_upper, zero_handling, title, key, mtype,
        &nrow, &ncol, &mkind, &skind, &asize, &znz,
        &Ap, &Ai, &Ax, NULL, NULL, NULL) ;
    if (status != RBIO_OK)
    {
        printf ("RBread error! "ID"\n", status) ;
        return (1) ;
    }

    status = RBkind (nrow, ncol, Ap, Ai, Ax, NULL, mkind, &mkind2, &skind2,
        mtype2, &xmin, &xmax, NULL) ;
    if (status != RBIO_OK)
    {
        printf ("RBkind error!\n") ;
        return (1) ;
    }
    printf ("mtype of tril(A):              [%s]\n", mtype2) ;

    status = RBok (nrow, ncol, asize, Ap, Ai, Ax, NULL, NULL, mkind,
        &njumbled, &nzeros);
    printf ("(L) RBok status "ID" "ID" "ID"\n", status, njumbled, nzeros) ;
    if (status != RBIO_OK || njumbled != 0)
    {
        printf ("RBok Error!\n") ;
        return (1) ;
    }

    SuiteSparse_free (Ap) ;
    SuiteSparse_free (Ai) ;
    SuiteSparse_free (Ax) ;

    printf ("RBdemo OK\n") ;
    SuiteSparse_finish ( ) ;
    return (0) ;
}