~logan/ubuntu/trusty/suitesparse/4.2.1-3ubuntu1

« back to all changes in this revision

Viewing changes to CXSparse_newfiles/MATLAB/CSparse/cs_mex.c

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2007-05-29 09:36:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070529093629-zowquo0b7slkk6nc
Tags: 3.0.0-2
* suitesparse builds properly twice in a row
* Bug fix: "suitesparse - FTBFS: Broken build depens: libgfortran1-dev",
  thanks to Bastian Blank (Closes: #426349).
* Bug fix: "suitesparse_3.0.0-1: FTBFS: build-depends on
  libgfortran1-dev", thanks to Steve Langasek (Closes: #426354).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "cs_mex.h"
 
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)
 
5
{
 
6
    CS_INT nnel, mm = mxGetM (A), nn = mxGetN (A) ;
 
7
#ifdef NCOMPLEX
 
8
    if (values)
 
9
    {
 
10
        if (mxIsComplex (A)) mexErrMsgTxt ("complex matrices not supported") ;
 
11
    }
 
12
#endif
 
13
    if (sparse && !mxIsSparse (A)) mexErrMsgTxt ("matrix must be sparse") ;
 
14
    if (!sparse)
 
15
    {
 
16
        if (mxIsSparse (A)) mexErrMsgTxt ("matrix must be full") ;
 
17
        if (values && !mxIsDouble (A)) mexErrMsgTxt ("matrix must be double") ;
 
18
    }
 
19
    if (nel)
 
20
    {
 
21
        /* check number of elements */
 
22
        nnel = mxGetNumberOfElements (A) ;
 
23
        if (m >= 0 && n >= 0 && m*n != nnel) mexErrMsgTxt ("wrong length") ;
 
24
    }
 
25
    else
 
26
    {
 
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") ;
 
30
    }
 
31
    if (square && mm != nn) mexErrMsgTxt ("matrix must be square") ;
 
32
}
 
33
 
 
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)
 
37
{
 
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 */
 
46
    return (A) ;
 
47
}
 
48
 
 
49
 
 
50
/* return a real sparse matrix to MATLAB */
 
51
mxArray *cs_dl_mex_put_sparse (cs_dl **Ahandle)
 
52
{
 
53
    cs_dl *A ;
 
54
    mxArray *Amatlab ;
 
55
    A = *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 */
 
68
    *Ahandle = NULL ;
 
69
    return (Amatlab) ;
 
70
}
 
71
 
 
72
/* get a real MATLAB dense column vector */
 
73
double *cs_dl_mex_get_double (CS_INT n, const mxArray *X)
 
74
{
 
75
    cs_mex_check (0, n, 1, 0, 0, 1, X) ;
 
76
    return (mxGetPr (X)) ;
 
77
}
 
78
 
 
79
/* return a double vector to MATLAB */
 
80
double *cs_dl_mex_put_double (CS_INT n, const double *b, mxArray **X)
 
81
{
 
82
    double *x ;
 
83
    CS_INT k ;
 
84
    *X = mxCreateDoubleMatrix (n, 1, mxREAL) ;      /* create x */
 
85
    x = mxGetPr (*X) ;
 
86
    for (k = 0 ; k < n ; k++) x [k] = b [k] ;       /* copy x = b */
 
87
    return (x) ;
 
88
}
 
89
 
 
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,
 
92
    int lo)
 
93
{
 
94
    double *p ;
 
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))
 
98
    {
 
99
        mexErrMsgTxt ("integer input cannot be complex") ;
 
100
    }
 
101
    p = mxGetPr (Imatlab) ;
 
102
    *imax = 0 ;
 
103
    for (k = 0 ; k < n ; k++)
 
104
    {
 
105
        i = p [k] ;
 
106
        C [k] = i - 1 ;
 
107
        if (i < lo) mexErrMsgTxt ("index out of bounds") ;
 
108
        *imax = CS_MAX (*imax, i) ;
 
109
    }
 
110
    return (C) ;
 
111
}
 
112
 
 
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)
 
115
{
 
116
    mxArray *X = mxCreateDoubleMatrix (1, n, mxREAL) ;
 
117
    double *x = mxGetPr (X) ;
 
118
    CS_INT k ;
 
119
    for (k = 0 ; k < n ; k++) x [k] = (p ? p [k] : k) + offset ;
 
120
    if (do_free) cs_free (p) ;
 
121
    return (X) ;
 
122
}
 
123
 
 
124
#ifndef NCOMPLEX
 
125
 
 
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)
 
129
{
 
130
    CS_INT p ;
 
131
    double *X, *Z ;
 
132
    cs_complex_t *Y ;
 
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++)
 
137
    {
 
138
        Y [p] = X [p] + I * (Z ? Z [p] : 0) ;
 
139
    }
 
140
    return (Y) ;
 
141
}
 
142
 
 
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)
 
145
{
 
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 */
 
154
    return (A) ;
 
155
}
 
156
 
 
157
/* return a complex sparse matrix to MATLAB */
 
158
mxArray *cs_cl_mex_put_sparse (cs_cl **Ahandle)
 
159
{
 
160
    cs_cl *A ;
 
161
    double *x, *z ;
 
162
    mxArray *Amatlab ;
 
163
    CS_INT k ;
 
164
 
 
165
    A = *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++)
 
180
    {
 
181
        x [k] = creal (A->x [k]) ;      /* copy and split numerical values */
 
182
        z [k] = cimag (A->x [k]) ;
 
183
    }
 
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 */
 
188
    *Ahandle = NULL ;
 
189
    return (Amatlab) ;
 
190
}
 
191
 
 
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)
 
194
{
 
195
    CS_INT k ;
 
196
    cs_mex_check (0, n, 1, 0, 0, 1, X) ;
 
197
    return (cs_cl_get_vector (n, n, X)) ;
 
198
}
 
199
 
 
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)
 
202
{
 
203
    double *x, *z ;
 
204
    mxArray *X ;
 
205
    CS_INT k ;
 
206
    X = mxCreateDoubleMatrix (n, 1, mxCOMPLEX) ;    /* create x */
 
207
    x = mxGetPr (X) ;
 
208
    z = mxGetPi (X) ;
 
209
    for (k = 0 ; k < n ; k++)
 
210
    {
 
211
        x [k] = creal (b [k]) ;     /* copy x = b */
 
212
        z [k] = cimag (b [k]) ;
 
213
    }
 
214
    cs_cl_free (b) ;
 
215
    return (X) ;
 
216
}
 
217
#endif