27 #include "csparse_interface.hpp"
28 #include "casadi/core/global_options.hpp"
33 int CASADI_LINSOL_CSPARSE_EXPORT
36 plugin->name =
"csparse";
38 plugin->version = CASADI_VERSION;
58 if (this->
S) cs_sfree(this->
S);
59 if (this->
N) cs_nfree(this->
N);
73 m->A.nzmax = this->
nnz();
74 m->A.m = this->
nrow();
75 m->A.n = this->
ncol();
76 m->colind.resize(this->
ncol()+1);
77 m->row.resize(this->
nnz());
86 m->temp_.resize(m->A.n);
94 m->
A.x =
const_cast<double*
>(A);
98 if (m->S) cs_sfree(m->S);
99 m->S = cs_sqr(order, &m->A, 0);
107 m->
A.x =
const_cast<double*
>(A);
110 for (casadi_int k=0; k<this->
nnz(); ++k) {
111 casadi_assert(!isnan(A[k]),
112 "Nonzero " +
str(k) +
" is not-a-number");
113 casadi_assert(!isinf(A[k]),
114 "Nonzero " +
str(k) +
" is infinite");
118 uout() <<
"CsparseInterface::prepare: numeric factorization" << std::endl;
119 uout() <<
"linear system to be factorized = " << std::endl;
125 if (m->N) cs_nfree(m->N);
126 m->N = cs_lu(&m->A, m->S, tol) ;
128 DM temp(
sp_, std::vector<double>(A, A+
nnz()));
129 temp = sparsify(temp);
131 std::stringstream ss;
132 ss <<
"CsparseInterface::prepare: factorization failed due to matrix"
133 " being singular. Matrix contains numerical zeros which are "
134 "structurally non-zero. Promoting these zeros to be structural "
135 "zeros, the matrix was found to be structurally rank deficient."
136 " sprank: " << sprank(temp.
sparsity()) <<
" <-> " << temp.
size2() << std::endl;
138 ss <<
"Sparsity of the linear system: " << std::endl;
143 std::stringstream ss;
144 ss <<
"CsparseInterface::prepare: factorization failed, check if Jacobian is singular"
147 ss <<
"Sparsity of the linear system: " << std::endl;
153 casadi_assert_dev(m->N!=
nullptr);
158 casadi_int nrhs,
bool tr)
const {
160 casadi_assert_dev(m->N!=
nullptr);
162 double *t = &m->temp_.front();
164 for (casadi_int k=0; k<nrhs; ++k) {
166 cs_pvec(m->S->q, x, t, m->A.n) ;
167 casadi_assert_dev(m->N->U!=
nullptr);
168 cs_utsolve(m->N->U, t) ;
169 cs_ltsolve(m->N->L, t) ;
170 cs_pvec(m->N->pinv, t, x, m->A.n) ;
172 cs_ipvec(m->N->pinv, x, t, m->A.n) ;
173 cs_lsolve(m->N->L, t) ;
174 cs_usolve(m->N->U, t) ;
175 cs_ipvec(m->S->q, t, x, m->A.n) ;
200 for (
int i=0; i<m->N->L->n; ++i) {
201 for (
int ii=m->N->L->p[i]; ii < m->N->L->p[i+1]; ++ii) {
202 if (i == m->N->L->i[ii]) detL *= m->
N->L->x[ii];
208 for (
int i=0; i<m->N->U->n; ++i) {
209 for (
int ii=m->N->U->p[i]; ii < m->N->U->p[i+1]; ++ii) {
210 if (i == m->N->U->i[ii]) detU *= m->N->U->x[ii];
~CsparseInterface() override
static const std::string meta_doc
A documentation string.
int init_mem(void *mem) const override
Initalize memory block.
int sfact(void *mem, const double *A) const override
int solve(void *mem, const double *A, double *x, casadi_int nrhs, bool tr) const override
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
void init(const Dict &opts) override
Initialize.
CsparseInterface(const std::string &name, const Sparsity &sp)
static LinsolInternal * creator(const std::string &name, const Sparsity &sp)
Create a new LinsolInternal.
int nfact(void *mem, const double *A) const override
Numeric factorization.
double det(void *mem, const double *A) const override
Determinant.
casadi_int size2() const
Get the second dimension (i.e. number of columns)
const casadi_int * colind() const
void init(const Dict &opts) override
Initialize.
const casadi_int * row() const
casadi_int nrow() const
Get sparsity pattern.
int init_mem(void *mem) const override
Initalize memory block.
void print_sparse(std::ostream &stream, bool truncate=true) const
Print sparse matrix style.
const Sparsity & sparsity() const
Const access the sparsity - reference to data member.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
bool verbose_
Verbose printout.
static const Options options_
Options.
void clear_mem()
Clear all memory (called from destructor)
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.
bool is_singular() const
Check whether the sparsity-pattern indicates structural singularity.
void copy_vector(const std::vector< S > &s, std::vector< D > &d)
int CASADI_LINSOL_CSPARSE_EXPORT casadi_register_linsol_csparse(LinsolInternal::Plugin *plugin)
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
void CASADI_LINSOL_CSPARSE_EXPORT casadi_load_linsol_csparse()