12SEXP
dense_lu(SEXP obj,
const char *
class,
int warn)
14 char cl[] =
".denseLU";
15 cl[0] = (
class[0] ==
'z') ?
'z' :
'd';
17 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1], r = (m < n) ? m : n;
22 PROTECT_WITH_INDEX(obj, &pid);
23 if (
class[0] !=
'z' &&
class[0] !=
'd') {
29 SEXP perm = PROTECT(Rf_allocVector(INTSXP, r)),
31 y = PROTECT(Rf_allocVector(
TYPEOF(x), XLENGTH(x)));
32 int *pperm = INTEGER(perm), info;
33 if (
TYPEOF(x) == CPLXSXP) {
34 Rcomplex *px = COMPLEX(x), *py = COMPLEX(y);
35 memcpy(py, px,
sizeof(Rcomplex) * (
size_t) XLENGTH(y));
36 F77_CALL(zgetrf)(&m, &n, py, &m, pperm, &info);
39 double *px = REAL(x), *py = REAL(y);
40 memcpy(py, px,
sizeof(
double) * (
size_t) XLENGTH(y));
41 F77_CALL(dgetrf)(&m, &n, py, &m, pperm, &info);
53#define DO_FREE(_T_, _S_, _N_, _P_) \
56 _T_ = Matrix_cs_spfree(_T_); \
58 _S_ = Matrix_cs_sfree (_S_); \
60 _N_ = Matrix_cs_nfree (_N_); \
62 _P_ = Matrix_cs_free (_P_); \
66#define DO_SORT(_A_, _T_) \
68 Matrix_cs_dropzeros(_A_); \
69 _T_ = Matrix_cs_transpose(_A_, 1); \
72 _A_ = Matrix_cs_spfree(_A_); \
73 _A_ = Matrix_cs_transpose(_T_, 1); \
76 _T_ = Matrix_cs_spfree(_T_); \
79SEXP
sparse_lu(SEXP obj,
const char *
class,
int warn,
int order,
83 PROTECT_WITH_INDEX(obj, &pid);
84 if (
class[0] !=
'z' &&
class[0] !=
'd') {
88 if (
class[1] !=
'g') {
92 if (
class[2] !=
'C') {
101 Rf_error(
_(
"sparse LU factorization of m-by-n matrix requires m == n"));
115 char cl[] =
".sparseLU";
122 SEXP L = PROTECT(
CXS2M(N->
L, 1,
't')),
123 U = PROTECT(
CXS2M(N->
U, 1,
't'));
129 SEXP p = PROTECT(Rf_allocVector(INTSXP, A->
m));
130 memcpy(INTEGER(p), P,
sizeof(
int) * (
size_t) A->
m);
134 SEXP q = PROTECT(Rf_allocVector(INTSXP, A->
n));
135 memcpy(INTEGER(q), S->
q,
sizeof(
int) * (
size_t) A->
n);
147 Rf_error (
_(
"sparse LU factorization failed: out of memory or near-singular"));
149 Rf_warning(
_(
"sparse LU factorization failed: out of memory or near-singular"));
158 (
class[1] ==
'g' ||
class[1] ==
's') &&
159 (
class[0] ==
'z' ||
class[0] ==
'd');
160 const char *nm =
"denseLU";
161 SEXP ans = (cache) ?
get_factor(s_obj, nm) : R_NilValue;
162 if (ans == R_NilValue) {
163 int warn = Rf_asLogical(s_warn);
174SEXP
R_sparse_lu(SEXP s_obj, SEXP s_warn, SEXP s_order, SEXP s_tol)
177 double tol = Rf_asReal(s_tol);
179 Rf_error(
_(
"'%s' is not a number"),
"tol");
180 int order = Rf_asInteger(s_order);
181 if (order == NA_INTEGER)
182 order = (tol == 1.0) ? 2 : 1;
183 else if (order < 0 || order > 3)
186 (
class[1] ==
'g' ||
class[1] ==
's') &&
187 (
class[0] ==
'z' ||
class[0] ==
'd');
188 const char *nm = (order == 0) ?
"sparseLU-" :
"sparseLU+";
189 SEXP ans = (cache) ?
get_factor(s_obj, nm) : R_NilValue;
190 if (ans == R_NilValue) {
191 int warn = Rf_asInteger(s_warn);
192 ans =
sparse_lu(s_obj,
class, warn, order, tol);
#define ERROR_LAPACK_2(_ROUTINE_, _INFO_, _WARN_, _LETTER_)
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)
SEXP get_factor(SEXP obj, const char *nm)
void set_factor(SEXP obj, const char *nm, SEXP val)
Matrix_csn * Matrix_cs_lu(const Matrix_cs *A, const Matrix_css *S, double tol)
SEXP CXS2M(Matrix_cs *A, int values, char shape)
int * Matrix_cs_pinv(const int *p, int n)
Matrix_css * Matrix_cs_sqr(int order, const Matrix_cs *A, int qr)
Matrix_cs * M2CXS(SEXP obj, int values)
#define CXSPARSE_XTYPE_SET(_VALUE_)
SEXP R_sparse_lu(SEXP s_obj, SEXP s_warn, SEXP s_order, SEXP s_tol)
SEXP sparse_as_general(SEXP, const char *)
#define DO_FREE(_T_, _S_, _N_, _P_)
SEXP dense_as_general(SEXP, const char *, int)
SEXP sparse_as_kind(SEXP, const char *, char)
#define DO_SORT(_A_, _T_)
SEXP R_dense_lu(SEXP s_obj, SEXP s_warn)
SEXP dense_lu(SEXP obj, const char *class, int warn)
SEXP sparse_as_Csparse(SEXP, const char *)
SEXP sparse_lu(SEXP obj, const char *class, int warn, int order, double tol)
SEXP dense_as_kind(SEXP, const char *, char, int)