Matrix r5059
Loading...
Searching...
No Matches
cs-etc.c
Go to the documentation of this file.
1#include "Mdefines.h"
2#include "cs-etc.h"
3
4int Matrix_cs_xtype; /* flag indicating use of cs_di_* or cs_ci_* */
5
6Matrix_cs *M2CXS(SEXP obj, int values)
7{
8 const char *class = Matrix_class(obj, valid_sparse_compressed, 6, __func__);
9 values = values && (class[0] == 'd' || class[0] == 'z');
10 char trans = (class[2] == 'C') ? 'N' : 'C';
11 Matrix_cs *A = (Matrix_cs *) R_alloc(1, sizeof(Matrix_cs));
12 memset(A, 0, sizeof(Matrix_cs));
13 SEXP dim = PROTECT(GET_SLOT(obj, Matrix_DimSym)),
14 p = PROTECT(GET_SLOT(obj, Matrix_pSym)),
15 i = PROTECT(GET_SLOT(obj, (trans == 'N') ? Matrix_iSym : Matrix_jSym));
16 A->nzmax = LENGTH(i);
17 A->m = INTEGER(dim)[(trans == 'N') ? 0 : 1];
18 A->n = INTEGER(dim)[(trans == 'N') ? 1 : 1];
19 A->p = INTEGER(p);
20 A->i = INTEGER(i);
21 A->nz = -1;
23 if (values) {
24 SEXP x = GET_SLOT(obj, Matrix_xSym);
25 switch (class[0]) {
26 case 'd':
27 A->x = REAL(x);
29 break;
30 case 'z':
31 A->x = COMPLEX(x);
33 break;
34 default:
35 break;
36 }
37 }
38 UNPROTECT(3);
39 return A;
40}
41
42SEXP CXS2M(Matrix_cs *A, int values, char shape)
43{
44 values = values && (A->xtype == CXSPARSE_REAL || A->xtype == CXSPARSE_COMPLEX);
45 char class[] = "..CMatrix";
46 class[0] = (!values) ? 'n' : ((A->xtype == CXSPARSE_REAL) ? 'd' : 'z');
47 class[1] = shape;
48 int nnz = A->p[A->n];
49 SEXP obj = PROTECT(newObject(class)),
50 dim = PROTECT(GET_SLOT(obj, Matrix_DimSym)),
51 p = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) A->n + 1)),
52 i = PROTECT(Rf_allocVector(INTSXP, nnz));
53 INTEGER(dim)[0] = A->m;
54 INTEGER(dim)[1] = A->n;
55 memcpy(INTEGER(p), A->p, sizeof(int) * ((size_t) A->n + 1));
56 memcpy(INTEGER(i), A->i, sizeof(int) * (size_t) nnz);
57 SET_SLOT(obj, Matrix_pSym, p);
58 SET_SLOT(obj, Matrix_iSym, i);
59 if (values) {
60 SEXP x;
61 if (A->xtype == CXSPARSE_REAL) {
62 PROTECT(x = Rf_allocVector(REALSXP, nnz));
63 memcpy(REAL(x), A->x, sizeof(double) * (size_t) nnz);
64 } else {
65 PROTECT(x = Rf_allocVector(CPLXSXP, nnz));
66 memcpy(COMPLEX(x), A->x, sizeof(Rcomplex) * (size_t) nnz);
67 }
68 SET_SLOT(obj, Matrix_xSym, x);
69 UNPROTECT(1);
70 }
71 UNPROTECT(4);
72 return obj;
73}
74
75/* Wrappers for the functions that we use at least once : */
76
78{
79#ifdef CXSPARSE
81 return (Matrix_csd *) cs_ci_dfree((cs_cid *) D);
82 else
83 return (Matrix_csd *) cs_di_dfree((cs_did *) D);
84#else
85 return (Matrix_csd *) cs_dfree((csd *) D);
86#endif
87}
88
90{
91#ifdef CXSPARSE
93 return (Matrix_csd *) cs_ci_dmperm((cs_ci *) A, seed);
94 else
95 return (Matrix_csd *) cs_di_dmperm((cs_di *) A, seed);
96#else
97 return (Matrix_csd *) cs_dmperm((cs *) A, seed);
98#endif
99}
100
102{
103#ifdef CXSPARSE
105 return cs_ci_dropzeros((cs_ci *) A);
106 else
107 return cs_di_dropzeros((cs_di *) A);
108#else
109 return cs_dropzeros((cs *) A);
110#endif
111}
112
113void *Matrix_cs_free(void *p)
114{
115#ifdef CXSPARSE
117 return cs_ci_free(p);
118 else
119 return cs_di_free(p);
120#else
121 return cs_free(p);
122#endif
123}
124
125int Matrix_cs_happly(const Matrix_cs *V, int i, double beta, void *x)
126{
127#ifdef CXSPARSE
129 return cs_ci_happly((cs_ci *) V, i, beta, (double _Complex *) x);
130 else
131 return cs_di_happly((cs_di *) V, i, beta, (double *) x);
132#else
133 return cs_happly((cs *) V, i, beta, (double *) x);
134#endif
135}
136
137int Matrix_cs_ipvec(const int *p, const void *b, void *x, int n)
138{
139#ifdef CXSPARSE
141 return cs_ci_ipvec(p, (double _Complex *) b, (double _Complex *) x, n);
142 else
143 return cs_di_ipvec(p, (double *) b, (double *) x, n);
144#else
145 return cs_ipvec(p, (double *) b, (double *) x, n);
146#endif
147}
148
149int Matrix_cs_lsolve(const Matrix_cs *L, void *x)
150{
151#ifdef CXSPARSE
153 return cs_ci_lsolve((cs_ci *) L, (double _Complex *) x);
154 else
155 return cs_di_lsolve((cs_di *) L, (double *) x);
156#else
157 return cs_lsolve((cs *) L, (double *) x);
158#endif
159}
160
161Matrix_csn *Matrix_cs_lu(const Matrix_cs *A, const Matrix_css *S, double tol)
162{
163#ifdef CXSPARSE
165 return (Matrix_csn *) cs_ci_lu((cs_ci *) A, (cs_cis *) S, tol);
166 else
167 return (Matrix_csn *) cs_di_lu((cs_di *) A, (cs_dis *) S, tol);
168#else
169 return (Matrix_csn *) cs_lu((cs *) A, (css *) S, tol);
170#endif
171}
172
173int Matrix_cs_lusol(int order, const Matrix_cs *A, void *b, double tol)
174{
175#ifdef CXSPARSE
177 return cs_ci_lusol(order, (cs_ci *) A, (double _Complex *) b, tol);
178 else
179 return cs_di_lusol(order, (cs_di *) A, (double *) b, tol);
180#else
181 return cs_lusol(order, (cs *) A, (double *) b, tol);
182#endif
183}
184
186{
187#ifdef CXSPARSE
189 return (Matrix_csn *) cs_ci_nfree((cs_cin *) N);
190 else
191 return (Matrix_csn *) cs_di_nfree((cs_din *) N);
192#else
193 return (Matrix_csn *) cs_nfree((csn *) N);
194#endif
195}
196
197Matrix_cs *Matrix_cs_permute(const Matrix_cs *A, const int *pinv, const int *q, int values)
198{
199 Matrix_cs *B = NULL;
200#ifdef CXSPARSE
202 cs_ci *tmp = cs_ci_permute((cs_ci *) A, pinv, q, values);
203 B = (Matrix_cs *) cs_ci_calloc(1, sizeof(Matrix_cs));
204 memcpy(B, tmp, sizeof(cs_ci));
205 tmp = cs_ci_free(tmp);
206 } else {
207 cs_di *tmp = cs_di_permute((cs_di *) A, pinv, q, values);
208 B = (Matrix_cs *) cs_di_calloc(1, sizeof(Matrix_cs));
209 memcpy(B, tmp, sizeof(cs_di));
210 tmp = cs_di_free(tmp);
211 }
212#else
213 cs *tmp = cs_permute((cs *) A, pinv, q, values);
214 B = (Matrix_cs *) cs_calloc(1, sizeof(Matrix_cs));
215 memcpy(B, tmp, sizeof(cs ));
216 tmp = cs_free(tmp);
217#endif
219 return B;
220}
221
222int *Matrix_cs_pinv(const int *p, int n)
223{
224#ifdef CXSPARSE
226 return cs_ci_pinv(p, n);
227 else
228 return cs_di_pinv(p, n);
229#else
230 return cs_pinv(p, n);
231#endif
232}
233
234int Matrix_cs_pvec(const int *p, const void *b, void *x, int n)
235{
236#ifdef CXSPARSE
238 return cs_ci_pvec(p, (double _Complex *) b, (double _Complex *) x, n);
239 else
240 return cs_di_pvec(p, (double *) b, (double *) x, n);
241#else
242 return cs_pvec(p, (double *) b, (double *) x, n);
243#endif
244}
245
247{
248#ifdef CXSPARSE
250 return (Matrix_csn *) cs_ci_qr((cs_ci *) A, (cs_cis *) S);
251 else
252 return (Matrix_csn *) cs_di_qr((cs_di *) A, (cs_dis *) S);
253#else
254 return (Matrix_csn *) cs_qr((cs *) A, (css *) S);
255#endif
256}
257
258int Matrix_cs_qrsol(int order, const Matrix_cs *A, void *b)
259{
260#ifdef CXSPARSE
262 return cs_ci_qrsol(order, (cs_ci *) A, (double _Complex *) b);
263 else
264 return cs_di_qrsol(order, (cs_di *) A, (double *) b);
265#else
266 return cs_qrsol(order, (cs *) A, (double *) b);
267#endif
268}
269
271{
272#ifdef CXSPARSE
274 return (Matrix_css *) cs_ci_sfree((cs_cis *) S);
275 else
276 return (Matrix_css *) cs_di_sfree((cs_dis *) S);
277#else
278 return (Matrix_css *) cs_sfree((css *) S);
279#endif
280}
281
282Matrix_cs *Matrix_cs_spalloc(int m, int n, int nzmax, int values, int triplet)
283{
284 Matrix_cs *B = NULL;
285#ifdef CXSPARSE
287 cs_ci *tmp = cs_ci_spalloc(m, n, nzmax, values, triplet);
288 B = (Matrix_cs *) cs_ci_calloc(1, sizeof(Matrix_cs));
289 memcpy(B, tmp, sizeof(cs_ci));
290 tmp = cs_ci_free(tmp);
291 } else {
292 cs_di *tmp = cs_di_spalloc(m, n, nzmax, values, triplet);
293 B = (Matrix_cs *) cs_di_calloc(1, sizeof(Matrix_cs));
294 memcpy(B, tmp, sizeof(cs_di));
295 tmp = cs_di_free(tmp);
296 }
297#else
298 cs *tmp = cs_spalloc(m, n, nzmax, values, triplet);
299 B = (Matrix_cs *) cs_calloc(1, sizeof(Matrix_cs));
300 memcpy(B, tmp, sizeof(cs ));
301 tmp = cs_free(tmp);
302#endif
304 return B;
305}
306
307Matrix_cs *Matrix_cs_speye(int m, int n, int values, int triplet)
308{
309 int k, d = (m < n) ? m : n;
310 Matrix_cs *B = Matrix_cs_spalloc(m, n, d, values, triplet);
311 if (!B)
312 return NULL;
313 int *B__p = B->p, *B__i = B->i;
314 for (k = 0; k < d; ++k)
315 B__p[k] = B__i[k] = k;
316 if (!triplet)
317 for (k = d; k <= n; ++k)
318 B__p[k] = d;
319 if (values) {
320#ifdef CXSPARSE
322 double _Complex *B__x = (double _Complex *) B->x;
323 for (k = 0; k < d; ++k)
324 B__x[k] = 1.0;
325 } else {
326#endif
327 double *B__x = (double *) B->x;
328 for (k = 0; k < d; ++k)
329 B__x[k] = 1.0;
330#ifdef CXSPARSE
331 }
332#endif
333 }
334 return B;
335}
336
338{
339#ifdef CXSPARSE
341 return (Matrix_cs *) cs_ci_spfree((cs_ci *) A);
342 else
343 return (Matrix_cs *) cs_di_spfree((cs_di *) A);
344#else
345 return (Matrix_cs *) cs_spfree((cs *) A);
346#endif
347}
348
350{
351#ifdef CXSPARSE
353 return cs_ci_sprealloc((cs_ci *) A, nzmax);
354 else
355 return cs_di_sprealloc((cs_di *) A, nzmax);
356#else
357 return cs_sprealloc((cs *) A, nzmax);
358#endif
359}
360
361int Matrix_cs_spsolve(Matrix_cs *L, const Matrix_cs *B, int k, int *xi, void *x, const int *pinv, int lo)
362{
363#ifdef CXSPARSE
365 return cs_ci_spsolve((cs_ci *) L, (cs_ci *) B, k, xi, (double _Complex *) x, pinv, lo);
366 else
367 return cs_di_spsolve((cs_di *) L, (cs_di *) B, k, xi, (double *) x, pinv, lo);
368#else
369 return cs_spsolve((cs *) L, (cs *) B, k, xi, (double *) x, pinv, lo);
370#endif
371}
372
373Matrix_css *Matrix_cs_sqr(int order, const Matrix_cs *A, int qr)
374{
375#ifdef CXSPARSE
377 return (Matrix_css *) cs_ci_sqr(order, (cs_ci *) A, qr);
378 else
379 return (Matrix_css *) cs_di_sqr(order, (cs_di *) A, qr);
380#else
381 return (Matrix_css *) cs_sqr(order, (cs *) A, qr);
382#endif
383}
384
386{
387 Matrix_cs *B = NULL;
388#ifdef CXSPARSE
390 cs_ci *tmp = cs_ci_transpose((cs_ci *) A, values);
391 B = (Matrix_cs *) cs_ci_calloc(1, sizeof(Matrix_cs));
392 memcpy(B, tmp, sizeof(cs_ci));
393 tmp = cs_ci_free(tmp);
394 } else {
395 cs_di *tmp = cs_di_transpose((cs_di *) A, values);
396 B = (Matrix_cs *) cs_di_calloc(1, sizeof(Matrix_cs));
397 memcpy(B, tmp, sizeof(cs_di));
398 tmp = cs_di_free(tmp);
399 }
400#else
401 cs *tmp = cs_transpose((cs *) A, values);
402 B = (Matrix_cs *) cs_calloc(1, sizeof(Matrix_cs));
403 memcpy(B, tmp, sizeof(cs ));
404 tmp = cs_free(tmp);
405#endif
407 return B;
408}
409
410int Matrix_cs_usolve(const Matrix_cs *U, void *x)
411{
412#ifdef CXSPARSE
414 return cs_ci_usolve((cs_ci *) U, (double _Complex *) x);
415 else
416 return cs_di_usolve((cs_di *) U, (double *) x);
417#else
418 return cs_usolve((cs *) U, (double *) x);
419#endif
420}
const char * Matrix_class(SEXP, const char **, int, const char *)
Definition objects.c:112
#define GET_SLOT(x, name)
Definition Mdefines.h:72
SEXP newObject(const char *)
Definition objects.c:13
const char * valid_sparse_compressed[]
Definition Mdefines.h:329
#define SET_SLOT(x, name, value)
Definition Mdefines.h:73
int Matrix_cs_lsolve(const Matrix_cs *L, void *x)
Definition cs-etc.c:149
Matrix_cs * Matrix_cs_transpose(const Matrix_cs *A, int values)
Definition cs-etc.c:385
Matrix_cs * Matrix_cs_spfree(Matrix_cs *A)
Definition cs-etc.c:337
Matrix_csn * Matrix_cs_lu(const Matrix_cs *A, const Matrix_css *S, double tol)
Definition cs-etc.c:161
int Matrix_cs_usolve(const Matrix_cs *U, void *x)
Definition cs-etc.c:410
int Matrix_cs_qrsol(int order, const Matrix_cs *A, void *b)
Definition cs-etc.c:258
void * Matrix_cs_free(void *p)
Definition cs-etc.c:113
int Matrix_cs_ipvec(const int *p, const void *b, void *x, int n)
Definition cs-etc.c:137
Matrix_cs * Matrix_cs_permute(const Matrix_cs *A, const int *pinv, const int *q, int values)
Definition cs-etc.c:197
Matrix_csn * Matrix_cs_qr(const Matrix_cs *A, const Matrix_css *S)
Definition cs-etc.c:246
Matrix_csd * Matrix_cs_dmperm(const Matrix_cs *A, int seed)
Definition cs-etc.c:89
SEXP CXS2M(Matrix_cs *A, int values, char shape)
Definition cs-etc.c:42
int Matrix_cs_lusol(int order, const Matrix_cs *A, void *b, double tol)
Definition cs-etc.c:173
Matrix_csd * Matrix_cs_dfree(Matrix_csd *D)
Definition cs-etc.c:77
int Matrix_cs_happly(const Matrix_cs *V, int i, double beta, void *x)
Definition cs-etc.c:125
Matrix_css * Matrix_cs_sfree(Matrix_css *S)
Definition cs-etc.c:270
Matrix_cs * Matrix_cs_speye(int m, int n, int values, int triplet)
Definition cs-etc.c:307
int * Matrix_cs_pinv(const int *p, int n)
Definition cs-etc.c:222
int Matrix_cs_xtype
Definition cs-etc.c:4
int Matrix_cs_sprealloc(Matrix_cs *A, int nzmax)
Definition cs-etc.c:349
Matrix_cs * Matrix_cs_spalloc(int m, int n, int nzmax, int values, int triplet)
Definition cs-etc.c:282
int Matrix_cs_dropzeros(Matrix_cs *A)
Definition cs-etc.c:101
int Matrix_cs_spsolve(Matrix_cs *L, const Matrix_cs *B, int k, int *xi, void *x, const int *pinv, int lo)
Definition cs-etc.c:361
Matrix_css * Matrix_cs_sqr(int order, const Matrix_cs *A, int qr)
Definition cs-etc.c:373
Matrix_cs * M2CXS(SEXP obj, int values)
Definition cs-etc.c:6
Matrix_csn * Matrix_cs_nfree(Matrix_csn *N)
Definition cs-etc.c:185
int Matrix_cs_pvec(const int *p, const void *b, void *x, int n)
Definition cs-etc.c:234
#define CXSPARSE_XTYPE_GET()
Definition cs-etc.h:11
#define CXSPARSE_REAL
Definition cs-etc.h:8
#define CXSPARSE_PATTERN
Definition cs-etc.h:7
#define CXSPARSE_COMPLEX
Definition cs-etc.h:9
SEXP Matrix_DimSym
Definition init.c:598
SEXP Matrix_xSym
Definition init.c:635
SEXP Matrix_iSym
Definition init.c:607
SEXP Matrix_jSym
Definition init.c:610
SEXP Matrix_pSym
Definition init.c:622