File: mread.c

package info (click to toggle)
suitesparse 1%3A5.12.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 176,720 kB
  • sloc: ansic: 1,193,914; cpp: 31,704; makefile: 6,638; fortran: 1,927; java: 1,826; csh: 765; ruby: 725; sh: 529; python: 333; perl: 225; sed: 164; awk: 35
file content (216 lines) | stat: -rw-r--r-- 6,083 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
/* ========================================================================== */
/* === CHOLMOD/MATLAB/mread mexFunction ===================================== */
/* ========================================================================== */

/* -----------------------------------------------------------------------------
 * CHOLMOD/MATLAB Module.  Copyright (C) 2005-2006, Timothy A. Davis
 * http://www.suitesparse.com
 * MATLAB(tm) is a Trademark of The MathWorks, Inc.
 * -------------------------------------------------------------------------- */

/* [A Z] = mread (filename, prefer_binary)
 *
 * Read a sparse or dense matrix from a file in Matrix Market format.
 *
 * All MatrixMarket formats are supported.
 * The Matrix Market "integer" format is converted into real, but the values
 * are preserved.  The "pattern" format is converted into real.  If a pattern
 * matrix is unsymmetric, all of its values are equal to one.  If a pattern is
 * symmetric, the kth diagonal entry is set to one plus the number of
 * off-diagonal nonzeros in row/column k, and off-diagonal entries are set to
 * -1.
 *
 * Explicit zero entries are returned as the binary pattern of the matrix Z.
 */

#include "cholmod_matlab.h"

/* maximum file length */
#define MAXLEN 1030

void mexFunction
(
    int	nargout,
    mxArray *pargout [ ],
    int	nargin,
    const mxArray *pargin [ ]
)
{
    void *G ;
    cholmod_dense *X = NULL ;
    cholmod_sparse *A = NULL, *Z = NULL ;
    cholmod_common Common, *cm ;
    Long *Ap = NULL, *Ai ;
    double *Ax, *Az = NULL ;
    char filename [MAXLEN] ;
    Long nz, k, is_complex = FALSE, nrow = 0, ncol = 0, allzero ;
    int mtype ;

    /* ---------------------------------------------------------------------- */
    /* start CHOLMOD and set parameters */ 
    /* ---------------------------------------------------------------------- */

    cm = &Common ;
    cholmod_l_start (cm) ;
    sputil_config (SPUMONI, cm) ;

    /* ---------------------------------------------------------------------- */
    /* get inputs */
    /* ---------------------------------------------------------------------- */

    if (nargin < 1 || nargin > 2 || nargout > 2)
    {
	mexErrMsgTxt ("usage: [A Z] = mread (filename, prefer_binary)") ;
    }
    if (!mxIsChar (pargin [0]))
    {
	mexErrMsgTxt ("mread requires a filename") ;
    }
    mxGetString (pargin [0], filename, MAXLEN) ;
    sputil_file = fopen (filename, "r") ;
    if (sputil_file == NULL)
    {
	mexErrMsgTxt ("cannot open file") ;
    }
    if (nargin > 1)
    {
	cm->prefer_binary = (mxGetScalar (pargin [1]) != 0) ;
    }

    /* ---------------------------------------------------------------------- */
    /* read the matrix, as either a dense or sparse matrix */
    /* ---------------------------------------------------------------------- */

    G = cholmod_l_read_matrix (sputil_file, 1, &mtype, cm) ;
    fclose (sputil_file) ;
    sputil_file = NULL ;
    if (G == NULL)
    {
	mexErrMsgTxt ("could not read file") ;
    }

    /* get the specific matrix (A or X), and change to ZOMPLEX if needed */
    if (mtype == CHOLMOD_SPARSE)
    {
	A = (cholmod_sparse *) G ;
	nrow = A->nrow ;
	ncol = A->ncol ;
	is_complex = (A->xtype == CHOLMOD_COMPLEX) ;
	Ap = A->p ;
	Ai = A->i ;
	if (is_complex)
	{
	    /* if complex, ensure A is ZOMPLEX */
	    cholmod_l_sparse_xtype (CHOLMOD_ZOMPLEX, A, cm) ;
	}
	Ax = A->x ;
	Az = A->z ;
    }
    else if (mtype == CHOLMOD_DENSE)
    {
	X = (cholmod_dense *) G ;
	nrow = X->nrow ;
	ncol = X->ncol ;
	is_complex = (X->xtype == CHOLMOD_COMPLEX) ;
	if (is_complex)
	{
	    /* if complex, ensure X is ZOMPLEX */
	    cholmod_l_dense_xtype (CHOLMOD_ZOMPLEX, X, cm) ;
	}
	Ax = X->x ;
	Az = X->z ;
    }
    else
    {
	mexErrMsgTxt ("invalid file") ;
    }

    /* ---------------------------------------------------------------------- */
    /* if requested, extract the zero entries and place them in Z */
    /* ---------------------------------------------------------------------- */

    if (nargout > 1)
    {
	if (mtype == CHOLMOD_SPARSE)
	{
	    /* A is a sparse real/zomplex double matrix */
	    Z = sputil_extract_zeros (A, cm) ;
	}
	else
	{
	    /* input is full; just return an empty Z matrix */
	    Z = cholmod_l_spzeros (nrow, ncol, 0, CHOLMOD_REAL, cm) ;
	}
    }

    /* ---------------------------------------------------------------------- */
    /* prune the zero entries from A and set nzmax(A) to nnz(A) */
    /* ---------------------------------------------------------------------- */

    if (mtype == CHOLMOD_SPARSE)
    {
	sputil_drop_zeros (A) ;
	cholmod_l_reallocate_sparse (cholmod_l_nnz (A, cm), A, cm) ;
    }

    /* ---------------------------------------------------------------------- */
    /* change a complex matrix to real if its imaginary part is all zero */
    /* ---------------------------------------------------------------------- */

    if (is_complex)
    {
	if (mtype == CHOLMOD_SPARSE)
	{
	    nz = Ap [ncol] ;
	}
	else
	{
	    nz = nrow * ncol ;
	}
	allzero = TRUE ;
	for (k = 0 ; k < nz ; k++)
	{
	    if (Az [k] != 0)
	    {
		allzero = FALSE ;
		break ;
	    }
	}
	if (allzero)
	{
	    /* discard the all-zero imaginary part */
	    if (mtype == CHOLMOD_SPARSE)
	    {
		cholmod_l_sparse_xtype (CHOLMOD_REAL, A, cm) ;
	    }
	    else
	    {
		cholmod_l_dense_xtype (CHOLMOD_REAL, X, cm) ;
	    }
	}
    }

    /* ---------------------------------------------------------------------- */
    /* return results to MATLAB */
    /* ---------------------------------------------------------------------- */

    if (mtype == CHOLMOD_SPARSE)
    {
	pargout [0] = sputil_put_sparse (&A, cm) ;
    }
    else
    {
	pargout [0] = sputil_put_dense (&X, cm) ;
    }
    if (nargout > 1)
    {
	pargout [1] = sputil_put_sparse (&Z, cm) ;
    }

    /* ---------------------------------------------------------------------- */
    /* free workspace */
    /* ---------------------------------------------------------------------- */

    cholmod_l_finish (cm) ;
    cholmod_l_print_common (" ", cm) ;
}