The R Project SVN R-packages

Rev

Rev 4530 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4530 Rev 4558
Line 38... Line 38...
38
		    strictly = FALSE;
38
		    strictly = FALSE;
39
	    }
39
	    }
40
    }
40
    }
41
    if (!sorted) {
41
    if (!sorted) {
42
	CHM_SP chx = AS_CHM_SP(x);
42
	CHM_SP chx = AS_CHM_SP(x);
-
 
43
	R_CheckStack();
43
 
44
 
44
	cholmod_sort(chx, &c);
45
	cholmod_sort(chx, &c);
45
	/* Now re-check that row indices are *strictly* increasing
46
	/* Now re-check that row indices are *strictly* increasing
46
	 * (and not just increasing) within each column : */
47
	 * (and not just increasing) within each column : */
47
	for (j = 0; j < ncol; j++) {
48
	for (j = 0; j < ncol; j++) {
Line 65... Line 66...
65
    /* This loses the symmetry property, since cholmod_dense has none,
66
    /* This loses the symmetry property, since cholmod_dense has none,
66
     * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
67
     * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
67
     * to numeric (CHOLMOD_REAL) ones : */
68
     * to numeric (CHOLMOD_REAL) ones : */
68
    CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
69
    CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
69
    int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
70
    int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
-
 
71
    R_CheckStack();
70
 
72
 
71
    return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));
73
    return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));
72
}
74
}
73
 
75
 
74
SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)
76
SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)
75
{
77
{
76
    CHM_SP chxs = AS_CHM_SP(x);
78
    CHM_SP chxs = AS_CHM_SP(x);
77
    CHM_SP chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
79
    CHM_SP chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
78
    int tr = asLogical(tri);
80
    int tr = asLogical(tri);
-
 
81
    R_CheckStack();
79
 
82
 
80
    return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,
83
    return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,
81
			      tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
84
			      tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
82
			      0, tr ? diag_P(x) : "",
85
			      0, tr ? diag_P(x) : "",
83
			      GET_SLOT(x, Matrix_DimNamesSym));
86
			      GET_SLOT(x, Matrix_DimNamesSym));
Line 93... Line 96...
93
{
96
{
94
    CHM_SP chxs = AS_CHM_SP(x);
97
    CHM_SP chxs = AS_CHM_SP(x);
95
    CHM_TR chxt = cholmod_sparse_to_triplet(chxs, &c);
98
    CHM_TR chxt = cholmod_sparse_to_triplet(chxs, &c);
96
    int tr = asLogical(tri);
99
    int tr = asLogical(tri);
97
    int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
100
    int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
-
 
101
    R_CheckStack();
98
 
102
 
99
    return chm_triplet_to_SEXP(chxt, 1,
103
    return chm_triplet_to_SEXP(chxt, 1,
100
			       tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
104
			       tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
101
			       Rkind, tr ? diag_P(x) : "",
105
			       Rkind, tr ? diag_P(x) : "",
102
			       GET_SLOT(x, Matrix_DimNamesSym));
106
			       GET_SLOT(x, Matrix_DimNamesSym));
Line 105... Line 109...
105
/* this used to be called  sCMatrix_to_gCMatrix(..)   [in ./dsCMatrix.c ]: */
109
/* this used to be called  sCMatrix_to_gCMatrix(..)   [in ./dsCMatrix.c ]: */
106
SEXP Csparse_symmetric_to_general(SEXP x)
110
SEXP Csparse_symmetric_to_general(SEXP x)
107
{
111
{
108
    CHM_SP chx = AS_CHM_SP(x), chgx;
112
    CHM_SP chx = AS_CHM_SP(x), chgx;
109
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
113
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
-
 
114
    R_CheckStack();
110
 
115
 
111
    if (!(chx->stype))
116
    if (!(chx->stype))
112
	error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
117
	error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
113
    chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
118
    chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
114
    /* xtype: pattern, "real", complex or .. */
119
    /* xtype: pattern, "real", complex or .. */
Line 119... Line 124...
119
SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
124
SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
120
{
125
{
121
    CHM_SP chx = AS_CHM_SP(x), chgx;
126
    CHM_SP chx = AS_CHM_SP(x), chgx;
122
    int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;
127
    int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;
123
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
128
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
-
 
129
    R_CheckStack();
124
 
130
 
125
    chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
131
    chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
126
    /* xtype: pattern, "real", complex or .. */
132
    /* xtype: pattern, "real", complex or .. */
127
    return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
133
    return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
128
			      GET_SLOT(x, Matrix_DimNamesSym));
134
			      GET_SLOT(x, Matrix_DimNamesSym));
Line 135... Line 141...
135
    CHM_SP chx = AS_CHM_SP(x);
141
    CHM_SP chx = AS_CHM_SP(x);
136
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
142
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
137
    CHM_SP chxt = cholmod_transpose(chx, chx->xtype, &c);
143
    CHM_SP chxt = cholmod_transpose(chx, chx->xtype, &c);
138
    SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
144
    SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
139
    int tr = asLogical(tri);
145
    int tr = asLogical(tri);
-
 
146
    R_CheckStack();
140
 
147
 
141
    tmp = VECTOR_ELT(dn, 0);	/* swap the dimnames */
148
    tmp = VECTOR_ELT(dn, 0);	/* swap the dimnames */
142
    SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));
149
    SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));
143
    SET_VECTOR_ELT(dn, 1, tmp);
150
    SET_VECTOR_ELT(dn, 1, tmp);
144
    UNPROTECT(1);
151
    UNPROTECT(1);
Line 150... Line 157...
150
SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
157
SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
151
{
158
{
152
    CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b);
159
    CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b);
153
    CHM_SP chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);
160
    CHM_SP chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);
154
    SEXP dn = allocVector(VECSXP, 2);
161
    SEXP dn = allocVector(VECSXP, 2);
-
 
162
    R_CheckStack();
155
 
163
 
156
    SET_VECTOR_ELT(dn, 0,	/* establish dimnames */
164
    SET_VECTOR_ELT(dn, 0,	/* establish dimnames */
157
		   duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
165
		   duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
158
    SET_VECTOR_ELT(dn, 1,
166
    SET_VECTOR_ELT(dn, 1,
159
		   duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));
167
		   duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));
Line 163... Line 171...
163
SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
171
SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
164
{
172
{
165
    int tr = asLogical(trans);
173
    int tr = asLogical(trans);
166
    CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b), chTr, chc;
174
    CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b), chTr, chc;
167
    SEXP dn = allocVector(VECSXP, 2);
175
    SEXP dn = allocVector(VECSXP, 2);
-
 
176
    R_CheckStack();
168
 
177
 
169
    chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
178
    chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
170
    chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
179
    chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
171
			 0, cha->xtype, 1, &c);
180
			 0, cha->xtype, 1, &c);
172
    cholmod_free_sparse(&chTr, &c);
181
    cholmod_free_sparse(&chTr, &c);
Line 185... Line 194...
185
    CHM_DN chb = AS_CHM_DN(b_M);
194
    CHM_DN chb = AS_CHM_DN(b_M);
186
    CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,
195
    CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,
187
					chb->xtype, &c);
196
					chb->xtype, &c);
188
    SEXP dn = PROTECT(allocVector(VECSXP, 2));
197
    SEXP dn = PROTECT(allocVector(VECSXP, 2));
189
    double one[] = {1,0}, zero[] = {0,0};
198
    double one[] = {1,0}, zero[] = {0,0};
-
 
199
    R_CheckStack();
190
 
200
 
191
    cholmod_sdmult(cha, 0, one, zero, chb, chc, &c);
201
    cholmod_sdmult(cha, 0, one, zero, chb, chc, &c);
192
    SET_VECTOR_ELT(dn, 0,	/* establish dimnames */
202
    SET_VECTOR_ELT(dn, 0,	/* establish dimnames */
193
		   duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
203
		   duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
194
    SET_VECTOR_ELT(dn, 1,
204
    SET_VECTOR_ELT(dn, 1,
Line 204... Line 214...
204
    CHM_DN chb = AS_CHM_DN(b_M);
214
    CHM_DN chb = AS_CHM_DN(b_M);
205
    CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,
215
    CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,
206
					chb->xtype, &c);
216
					chb->xtype, &c);
207
    SEXP dn = PROTECT(allocVector(VECSXP, 2));
217
    SEXP dn = PROTECT(allocVector(VECSXP, 2));
208
    double one[] = {1,0}, zero[] = {0,0};
218
    double one[] = {1,0}, zero[] = {0,0};
-
 
219
    R_CheckStack();
209
 
220
 
210
    cholmod_sdmult(cha, 1, one, zero, chb, chc, &c);
221
    cholmod_sdmult(cha, 1, one, zero, chb, chc, &c);
211
    SET_VECTOR_ELT(dn, 0,	/* establish dimnames */
222
    SET_VECTOR_ELT(dn, 0,	/* establish dimnames */
212
		   duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));
223
		   duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));
213
    SET_VECTOR_ELT(dn, 1,
224
    SET_VECTOR_ELT(dn, 1,
Line 223... Line 234...
223
	tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */
234
	tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */
224
    CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL;
235
    CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL;
225
    CHM_SP chcp, chxt,
236
    CHM_SP chcp, chxt,
226
	chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) : AS_CHM_SP(x);
237
	chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) : AS_CHM_SP(x);
227
    SEXP dn = PROTECT(allocVector(VECSXP, 2));
238
    SEXP dn = PROTECT(allocVector(VECSXP, 2));
-
 
239
    R_CheckStack();
228
 
240
 
229
    if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
241
    if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
230
    chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);
242
    chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);
231
    if(!chcp) error(_("Csparse_crossprod(): error return from cholmod_aat()"));
243
    if(!chcp) error(_("Csparse_crossprod(): error return from cholmod_aat()"));
232
    cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
244
    cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
Line 245... Line 257...
245
{
257
{
246
    CHM_SP chx = AS_CHM_SP(x);
258
    CHM_SP chx = AS_CHM_SP(x);
247
    CHM_SP ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
259
    CHM_SP ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
248
    double dtol = asReal(tol);
260
    double dtol = asReal(tol);
249
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
261
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
-
 
262
    R_CheckStack();
250
 
263
 
251
    if(!cholmod_drop(dtol, ans, &c))
264
    if(!cholmod_drop(dtol, ans, &c))
252
	error(_("cholmod_drop() failed"));
265
	error(_("cholmod_drop() failed"));
253
    return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
266
    return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
254
			      GET_SLOT(x, Matrix_DimNamesSym));
267
			      GET_SLOT(x, Matrix_DimNamesSym));
Line 256... Line 269...
256
 
269
 
257
SEXP Csparse_horzcat(SEXP x, SEXP y)
270
SEXP Csparse_horzcat(SEXP x, SEXP y)
258
{
271
{
259
    CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
272
    CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
260
    int Rkind = 0; /* only for "d" - FIXME */
273
    int Rkind = 0; /* only for "d" - FIXME */
-
 
274
    R_CheckStack();
261
 
275
 
262
    /* FIXME: currently drops dimnames */
276
    /* FIXME: currently drops dimnames */
263
    return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),
277
    return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),
264
			      1, 0, Rkind, "", R_NilValue);
278
			      1, 0, Rkind, "", R_NilValue);
265
}
279
}
266
 
280
 
267
SEXP Csparse_vertcat(SEXP x, SEXP y)
281
SEXP Csparse_vertcat(SEXP x, SEXP y)
268
{
282
{
269
    CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
283
    CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
270
    int Rkind = 0; /* only for "d" - FIXME */
284
    int Rkind = 0; /* only for "d" - FIXME */
-
 
285
    R_CheckStack();
271
 
286
 
272
    /* FIXME: currently drops dimnames */
287
    /* FIXME: currently drops dimnames */
273
    return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),
288
    return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),
274
			      1, 0, Rkind, "", R_NilValue);
289
			      1, 0, Rkind, "", R_NilValue);
275
}
290
}
Line 277... Line 292...
277
SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)
292
SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)
278
{
293
{
279
    CHM_SP chx = AS_CHM_SP(x);
294
    CHM_SP chx = AS_CHM_SP(x);
280
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
295
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
281
    CHM_SP ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
296
    CHM_SP ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
-
 
297
    R_CheckStack();
282
 
298
 
283
    return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
299
    return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
284
			      GET_SLOT(x, Matrix_DimNamesSym));
300
			      GET_SLOT(x, Matrix_DimNamesSym));
285
}
301
}
286
 
302
 
Line 295... Line 311...
295
	double one[] = {1, 0};
311
	double one[] = {1, 0};
296
	CHM_SP ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);
312
	CHM_SP ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);
297
	int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
313
	int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
298
	int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
314
	int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
299
 
315
 
-
 
316
	R_CheckStack();
300
	cholmod_free_sparse(&eye, &c);
317
	cholmod_free_sparse(&eye, &c);
301
	return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
318
	return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
302
				  GET_SLOT(x, Matrix_DimNamesSym));
319
				  GET_SLOT(x, Matrix_DimNamesSym));
303
    }
320
    }
304
}
321
}
Line 307... Line 324...
307
{
324
{
308
    CHM_SP chx = AS_CHM_SP(x);
325
    CHM_SP chx = AS_CHM_SP(x);
309
    int rsize = (isNull(i)) ? -1 : LENGTH(i),
326
    int rsize = (isNull(i)) ? -1 : LENGTH(i),
310
	csize = (isNull(j)) ? -1 : LENGTH(j);
327
	csize = (isNull(j)) ? -1 : LENGTH(j);
311
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
328
    int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
-
 
329
    R_CheckStack();
312
 
330
 
313
    if (rsize >= 0 && !isInteger(i))
331
    if (rsize >= 0 && !isInteger(i))
314
	error(_("Index i must be NULL or integer"));
332
	error(_("Index i must be NULL or integer"));
315
    if (csize >= 0 && !isInteger(j))
333
    if (csize >= 0 && !isInteger(j))
316
	error(_("Index j must be NULL or integer"));
334
	error(_("Index j must be NULL or integer"));