2
/* check MATLAB input argument */
3
void cs_mex_check (CS_INT nel, CS_INT m, CS_INT n, int square, int sparse,
4
int values, const mxArray *A)
6
CS_INT nnel, mm = mxGetM (A), nn = mxGetN (A) ;
10
if (mxIsComplex (A)) mexErrMsgTxt ("complex matrices not supported") ;
13
if (sparse && !mxIsSparse (A)) mexErrMsgTxt ("matrix must be sparse") ;
16
if (mxIsSparse (A)) mexErrMsgTxt ("matrix must be full") ;
17
if (values && !mxIsDouble (A)) mexErrMsgTxt ("matrix must be double") ;
21
/* check number of elements */
22
nnel = mxGetNumberOfElements (A) ;
23
if (m >= 0 && n >= 0 && m*n != nnel) mexErrMsgTxt ("wrong length") ;
27
/* check row and/or column dimensions */
28
if (m >= 0 && m != mm) mexErrMsgTxt ("wrong dimension") ;
29
if (n >= 0 && n != nn) mexErrMsgTxt ("wrong dimension") ;
31
if (square && mm != nn) mexErrMsgTxt ("matrix must be square") ;
34
/* get a real (or pattern) MATLAB sparse matrix and convert to cs_dl */
35
cs_dl *cs_dl_mex_get_sparse (cs_dl *A, int square, int values,
36
const mxArray *Amatlab)
38
cs_mex_check (0, -1, -1, square, 1, values, Amatlab) ;
39
A->m = mxGetM (Amatlab) ;
40
A->n = mxGetN (Amatlab) ;
41
A->p = (CS_INT *) mxGetJc (Amatlab) ;
42
A->i = (CS_INT *) mxGetIr (Amatlab) ;
43
A->x = values ? mxGetPr (Amatlab) : NULL ;
44
A->nzmax = mxGetNzmax (Amatlab) ;
45
A->nz = -1 ; /* denotes a compressed-col matrix, instead of triplet */
50
/* return a real sparse matrix to MATLAB */
51
mxArray *cs_dl_mex_put_sparse (cs_dl **Ahandle)
56
if (!A) mexErrMsgTxt ("failed") ;
57
Amatlab = mxCreateSparse (0, 0, 0, mxREAL) ;
58
mxSetM (Amatlab, A->m) ;
59
mxSetN (Amatlab, A->n) ;
60
mxSetNzmax (Amatlab, A->nzmax) ;
61
cs_free (mxGetJc (Amatlab)) ;
62
cs_free (mxGetIr (Amatlab)) ;
63
cs_free (mxGetPr (Amatlab)) ;
64
mxSetJc (Amatlab, (void *) (A->p)) ; /* assign A->p pointer to MATLAB A */
65
mxSetIr (Amatlab, (void *) (A->i)) ;
66
mxSetPr (Amatlab, A->x) ;
67
cs_free (A) ; /* frees A struct only, not A->p, etc */
72
/* get a real MATLAB dense column vector */
73
double *cs_dl_mex_get_double (CS_INT n, const mxArray *X)
75
cs_mex_check (0, n, 1, 0, 0, 1, X) ;
76
return (mxGetPr (X)) ;
79
/* return a double vector to MATLAB */
80
double *cs_dl_mex_put_double (CS_INT n, const double *b, mxArray **X)
84
*X = mxCreateDoubleMatrix (n, 1, mxREAL) ; /* create x */
86
for (k = 0 ; k < n ; k++) x [k] = b [k] ; /* copy x = b */
90
/* get a MATLAB flint array and convert to CS_INT */
91
CS_INT *cs_dl_mex_get_int (CS_INT n, const mxArray *Imatlab, CS_INT *imax,
95
CS_INT i, k, *C = cs_dl_malloc (n, sizeof (CS_INT)) ;
96
cs_mex_check (1, n, 1, 0, 0, 1, Imatlab) ;
97
if (mxIsComplex (Imatlab))
99
mexErrMsgTxt ("integer input cannot be complex") ;
101
p = mxGetPr (Imatlab) ;
103
for (k = 0 ; k < n ; k++)
107
if (i < lo) mexErrMsgTxt ("index out of bounds") ;
108
*imax = CS_MAX (*imax, i) ;
113
/* return an CS_INT array to MATLAB as a flint row vector */
114
mxArray *cs_dl_mex_put_int (CS_INT *p, CS_INT n, CS_INT offset, int do_free)
116
mxArray *X = mxCreateDoubleMatrix (1, n, mxREAL) ;
117
double *x = mxGetPr (X) ;
119
for (k = 0 ; k < n ; k++) x [k] = (p ? p [k] : k) + offset ;
120
if (do_free) cs_free (p) ;
126
/* copy a MATLAB real or complex vector into a cs_cl complex vector */
127
static cs_complex_t *cs_cl_get_vector (CS_INT n, CS_INT size,
128
const mxArray *Xmatlab)
133
X = mxGetPr (Xmatlab) ;
134
Z = (mxIsComplex (Xmatlab)) ? mxGetPi (Xmatlab) : NULL ;
135
Y = cs_dl_malloc (size, sizeof (cs_complex_t)) ;
136
for (p = 0 ; p < n ; p++)
138
Y [p] = X [p] + I * (Z ? Z [p] : 0) ;
143
/* get a real or complex MATLAB sparse matrix and convert to cs_cl */
144
cs_cl *cs_cl_mex_get_sparse (cs_cl *A, int square, const mxArray *Amatlab)
146
cs_mex_check (0, -1, -1, square, 1, 1, Amatlab) ;
147
A->m = mxGetM (Amatlab) ;
148
A->n = mxGetN (Amatlab) ;
149
A->p = (CS_INT *) mxGetJc (Amatlab) ;
150
A->i = (CS_INT *) mxGetIr (Amatlab) ;
151
A->nzmax = mxGetNzmax (Amatlab) ;
152
A->x = cs_cl_get_vector (A->p [A->n], A->nzmax, Amatlab) ;
153
A->nz = -1 ; /* denotes a compressed-col matrix, instead of triplet */
157
/* return a complex sparse matrix to MATLAB */
158
mxArray *cs_cl_mex_put_sparse (cs_cl **Ahandle)
166
if (!A) mexErrMsgTxt ("failed") ;
167
Amatlab = mxCreateSparse (0, 0, 0, mxCOMPLEX) ;
168
mxSetM (Amatlab, A->m) ;
169
mxSetN (Amatlab, A->n) ;
170
mxSetNzmax (Amatlab, A->nzmax) ;
171
cs_cl_free (mxGetJc (Amatlab)) ;
172
cs_cl_free (mxGetIr (Amatlab)) ;
173
cs_cl_free (mxGetPr (Amatlab)) ;
174
cs_cl_free (mxGetPi (Amatlab)) ;
175
mxSetJc (Amatlab, (void *) (A->p)) ; /* assign A->p pointer to MATLAB A */
176
mxSetIr (Amatlab, (void *) (A->i)) ;
177
x = cs_dl_malloc (A->nzmax, sizeof (double)) ;
178
z = cs_dl_malloc (A->nzmax, sizeof (double)) ;
179
for (k = 0 ; k < A->nzmax ; k++)
181
x [k] = creal (A->x [k]) ; /* copy and split numerical values */
182
z [k] = cimag (A->x [k]) ;
184
cs_cl_free (A->x) ; /* free copy of complex values */
185
mxSetPr (Amatlab, x) ;
186
mxSetPi (Amatlab, z) ;
187
cs_cl_free (A) ; /* frees A struct only, not A->p, etc */
192
/* get a real or complex MATLAB dense column vector, and copy to cs_complex_t */
193
cs_complex_t *cs_cl_mex_get_double (CS_INT n, const mxArray *X)
196
cs_mex_check (0, n, 1, 0, 0, 1, X) ;
197
return (cs_cl_get_vector (n, n, X)) ;
200
/* copy a complex vector back to MATLAB and free it */
201
mxArray *cs_cl_mex_put_double (CS_INT n, cs_complex_t *b)
206
X = mxCreateDoubleMatrix (n, 1, mxCOMPLEX) ; /* create x */
209
for (k = 0 ; k < n ; k++)
211
x [k] = creal (b [k]) ; /* copy x = b */
212
z [k] = cimag (b [k]) ;