23 int *pdim =
DIM(obj), n = pdim[1];
25 Rf_error(
_(
"matrix is not square"));
26 ul = (
class[1] !=
'g') ?
UPLO(obj) : (ul ==
'\0') ?
'U' : ul;
27 char cl[] =
".denseCholesky";
28 cl[0] = (
class[0] ==
'z') ?
'z' :
'd';
36 PROTECT_WITH_INDEX(obj, &pid);
37 if (
class[0] !=
'z' &&
class[0] !=
'd') {
41 if (
class[1] ==
't' &&
DIAG(obj) !=
'N')
44 y = PROTECT(Rf_allocVector(
TYPEOF(x), XLENGTH(x)));
46 if (
class[2] !=
'p') {
47 if (
TYPEOF(x) == CPLXSXP) {
48 Rcomplex *px = COMPLEX(x), *py = COMPLEX(y);
49 memset(py, 0,
sizeof(Rcomplex) * (
size_t) XLENGTH(y));
50 F77_CALL(zlacpy)(&ul, &n, &n, px, &n, py, &n
FCONE);
52 F77_CALL(zpotrf)(&ul, &n, py, &n, &info
FCONE);
55 SEXP perm = PROTECT(Rf_allocVector(INTSXP, n));
56 int *pperm = INTEGER(perm), rank;
57 double *work = (
double *) R_alloc((
size_t) n * 2,
sizeof(
double));
58 F77_CALL(zpstrf)(&ul, &n, py, &n, pperm, &rank, &tol, work,
63 py += (R_xlen_t) rank * n + rank;
64 for (j = rank; j < n; ++j) {
65 memset(py, 0,
sizeof(Rcomplex) * (
size_t) d);
73 double *px = REAL(x), *py = REAL(y);
74 memset(py, 0,
sizeof(
double) * (
size_t) XLENGTH(y));
75 F77_CALL(dlacpy)(&ul, &n, &n, px, &n, py, &n
FCONE);
77 F77_CALL(dpotrf)(&ul, &n, py, &n, &info
FCONE);
80 SEXP perm = PROTECT(Rf_allocVector(INTSXP, n));
81 int *pperm = INTEGER(perm), rank;
82 double *work = (
double *) R_alloc((
size_t) n * 2,
sizeof(
double));
83 F77_CALL(dpstrf)(&ul, &n, py, &n, pperm, &rank, &tol, work,
88 py += (R_xlen_t) rank * n + rank;
89 for (j = rank; j < n; ++j) {
90 memset(py, 0,
sizeof(
double) * (
size_t) d);
99 if (
TYPEOF(x) == CPLXSXP) {
100 Rcomplex *px = COMPLEX(x), *py = COMPLEX(y);
101 memcpy(py, px,
sizeof(Rcomplex) * (
size_t) XLENGTH(y));
102 F77_CALL(zpptrf)(&ul, &n, py, &info
FCONE);
105 double *px = REAL(x), *py = REAL(y);
106 memcpy(py, px,
sizeof(
double) * (
size_t) XLENGTH(y));
107 F77_CALL(dpptrf)(&ul, &n, py, &info
FCONE);
119 int *ll,
int *super, Rcomplex beta,
int force,
123 PROTECT_WITH_INDEX(obj, &pid);
124 if (
class[0] !=
'z' &&
class[0] !=
'd') {
128 if (
class[1] ==
's' && (
class[0] !=
'z' ||
TRANS(obj) ==
'C'))
137 if (
class[2] !=
'C') {
138 if (
class[2] ==
'R' &&
class[1] ==
's')
145 cholmod_sparse *A =
M2CHS(obj, 1);
146 cholmod_factor *L = (trf == R_NilValue) ? NULL :
M2CHF(trf, 1);
147 double b[2]; b[0] = beta.r; b[1] = beta.i;
149 A->stype = (
class[1] !=
's') ? 0 : (
UPLO(obj) ==
'U') ? 1 : -1;
152 L = cholmod_copy_factor(L, &
c);
156 c.method[0].ordering = CHOLMOD_NATURAL;
159 c.supernodal = (!super || *super == NA_LOGICAL) ? CHOLMOD_AUTO :
160 ((*super != 0) ? CHOLMOD_SUPERNODAL : CHOLMOD_SIMPLICIAL);
161 L = cholmod_analyze(A, &
c);
165 *super = L->is_super != 0;
167 *ll = L->is_super != 0 || L->is_ll != 0;
170 c.final_ll = ((ll) ? *ll : L->is_ll) != 0;
171 c.final_super = ((super) ? *super : L->is_super) != 0;
173 c.final_monotonic = 1;
175 cholmod_factorize_p(A, b, NULL, 0, L, &
c);
176 cholmod_defaults(&
c);
178 SEXP ans = PROTECT(
CHF2M(L, 1));
179 cholmod_free_factor(&L, &
c);
180 if (
TYPEOF(ans) == CHARSXP) {
182 Rf_error (
"%s", CHAR(ans));
184 Rf_warning(
"%s", CHAR(ans));
187 SEXP adimnames = PROTECT(Rf_allocVector(VECSXP, 2)),
188 odimnames = PROTECT(
DIMNAMES(obj, 0));
189 symDN(adimnames, odimnames, (
class[1] !=
's') ? 0 : -1);
198 SEXP s_tol, SEXP s_uplo)
201 int pivot = Rf_asLogical(s_pivot);
202 double tol = Rf_asReal(s_tol);
204 Rf_error(
_(
"'%s' is not a number"),
"tol");
206 if (s_uplo != R_NilValue)
208 int cache = (
class[1] ==
's') &&
209 ((
class[0] ==
'z' &&
TRANS(s_obj) ==
'C') ||
class[0] ==
'd');
210 char nm[] =
"denseCholesky..";
211 if (
class[1] ==
's' &&
class[2] ==
'p')
212 nm[13] = nm[14] =
'-';
215 nm[14] = (!pivot) ?
'-' :
'+';
217 SEXP ans = (cache) ?
get_factor(s_obj, nm) : R_NilValue;
218 if (ans == R_NilValue) {
219 int warn = Rf_asInteger(s_warn);
231 SEXP s_ll, SEXP s_super, SEXP s_beta,
232 SEXP s_force, SEXP s_uplo)
235 int warn = Rf_asInteger(s_warn), order = Rf_asInteger(s_order),
236 ll = Rf_asLogical(s_ll), super = Rf_asLogical(s_super),
237 force = Rf_asLogical(s_force);
238 Rcomplex beta = Rf_asComplex(s_beta);
239 if (order < 0 || order > 1)
241 if (!R_FINITE(beta.r) || !R_FINITE(beta.i))
242 Rf_error(
_(
"'%s' is not a number or not finite"),
"beta");
244 if (s_uplo != R_NilValue)
246 int cache = (
class[1] ==
's') &&
247 ((
class[0] ==
'z' &&
TRANS(s_obj) ==
'C') ||
class[0] ==
'd');
248 char nm[] =
"..........Cholesky.";
249 nm[18] = (order == 0) ?
'-' :
'+';
250 SEXP ans = R_NilValue;
252 if (super == NA_LOGICAL || super == 0) {
253 memcpy(nm,
"simplicial", 10);
255 if (ans != R_NilValue) super = 0;
257 if (ans == R_NilValue && (super == NA_LOGICAL || super != 0)) {
258 memcpy(nm,
"supernodal", 10);
260 if (ans != R_NilValue) super = 1;
263 if (beta.r != 0.0 || beta.i != 0.0 || ans == R_NilValue ||
264 (super == 0 && ll != 0)) {
265 if (beta.r != 0.0 || beta.i != 0.0) {
268 order, &ll, &super, beta,
272 if (ans == R_NilValue) {
275 order, &zz, &super, beta,
277 if (cache && ans != R_NilValue) {
278 memcpy(nm, (super == 0) ?
"simplicial" :
"supernodal", 10);
284 if (ans != R_NilValue && super == 0 && ll != 0) {
286 cholmod_factor *L =
M2CHF(ans, 1);
287 L = cholmod_copy_factor(L, &
c);
288 cholmod_change_factor(L->xtype, 1, 0, 1, 1, L, &
c);
289 SEXP tmp = PROTECT(
CHF2M(L, 1));
290 cholmod_free_factor(&L, &
c);
291 if (
TYPEOF(tmp) == CHARSXP) {
293 Rf_error (
"%s", CHAR(tmp));
295 Rf_warning(
"%s", CHAR(tmp));
SEXP sparse_transpose(SEXP, const char *, char, int)
SEXP sparse_as_general(SEXP, const char *)
SEXP dense_force_canonical(SEXP, const char *, int)
SEXP dense_cholesky(SEXP obj, const char *class, int warn, int pivot, double tol, char ul)
SEXP R_dense_cholesky(SEXP s_obj, SEXP s_warn, SEXP s_pivot, SEXP s_tol, SEXP s_uplo)
SEXP sparse_as_kind(SEXP, const char *, char)
SEXP sparse_cholesky(SEXP obj, const char *class, int warn, int order, int *ll, int *super, Rcomplex beta, int force, char ul, SEXP trf)
SEXP R_sparse_cholesky(SEXP s_obj, SEXP s_warn, SEXP s_order, SEXP s_ll, SEXP s_super, SEXP s_beta, SEXP s_force, SEXP s_uplo)
SEXP sparse_force_symmetric(SEXP, const char *, char, char)
SEXP sparse_as_Csparse(SEXP, const char *)
SEXP dense_as_kind(SEXP, const char *, char, int)
#define ERROR_LAPACK_3(_ROUTINE_, _INFO_, _WARN_)
#define ERROR_LAPACK_4(_ROUTINE_, _INFO_, _WARN_, _RANK_)
const char * valid_dense[]
#define SET_DIMNAMES(x, mode, value)
const char * Matrix_class(SEXP, const char **, int, const char *)
#define DIMNAMES(x, mode)
#define GET_SLOT(x, name)
SEXP newObject(const char *)
const char * valid_sparse[]
#define SET_SLOT(x, name, value)
void symDN(SEXP dest, SEXP src, int J)
SEXP get_factor(SEXP obj, const char *nm)
void set_factor(SEXP obj, const char *nm, SEXP val)
cholmod_factor * M2CHF(SEXP obj, int values)
cholmod_sparse * M2CHS(SEXP obj, int values)
SEXP CHF2M(cholmod_factor *L, int values)