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