26 #include "csparse_cholesky_interface.hpp"
33 int CASADI_LINSOL_CSPARSECHOLESKY_EXPORT
34 casadi_register_linsol_csparsecholesky(LinsolInternal::Plugin* plugin) {
36 plugin->name =
"csparsecholesky";
38 plugin->version = CASADI_VERSION;
45 void CASADI_LINSOL_CSPARSECHOLESKY_EXPORT casadi_load_linsol_csparsecholesky() {
51 LinsolInternal(name, sp) {
54 CSparseCholeskyInterface::~CSparseCholeskyInterface() {
58 CsparseCholMemory::~CsparseCholMemory() {
59 if (this->S) cs_sfree(this->S);
60 if (this->L) cs_nfree(this->L);
63 void CSparseCholeskyInterface::init(
const Dict& opts) {
65 LinsolInternal::init(opts);
68 int CSparseCholeskyInterface::init_mem(
void* mem)
const {
69 if (LinsolInternal::init_mem(mem))
return 1;
70 auto m =
static_cast<CsparseCholMemory*
>(mem);
74 m->A.nzmax = this->nnz();
75 m->A.m = this->nrow();
76 m->A.n = this->ncol();
77 m->colind.resize(m->A.n+1);
78 m->row.resize(this->nnz());
81 m->row.resize(m->A.nzmax);
89 m->temp.resize(m->A.n);
94 int CSparseCholeskyInterface::sfact(
void* mem,
const double* A)
const {
95 auto m =
static_cast<CsparseCholMemory*
>(mem);
98 m->A.x =
const_cast<double*
>(A);
101 casadi_int order = 0;
102 m->S = cs_schol(order, &m->A);
106 int CSparseCholeskyInterface::nfact(
void* mem,
const double* A)
const {
107 auto m =
static_cast<CsparseCholMemory*
>(mem);
109 m->A.x =
const_cast<double*
>(A);
112 casadi_int nnz = this->nnz();
113 for (casadi_int k=0; k<nnz; ++k) {
114 casadi_assert(!isnan(A[k]),
115 "Nonzero " +
str(k) +
" is not-a-number");
116 casadi_assert(!isinf(A[k]),
117 "Nonzero " +
str(k) +
" is infinite");
120 if (m->L) cs_nfree(m->L);
121 m->L = cs_chol(&m->A, m->S) ;
122 casadi_assert_dev(m->L!=
nullptr);
126 int CSparseCholeskyInterface::
127 solve(
void* mem,
const double* A,
double* x, casadi_int nrhs,
bool tr)
const {
128 auto m =
static_cast<CsparseCholMemory*
>(mem);
130 casadi_assert_dev(m->L!=
nullptr);
132 double *t = &m->temp.front();
133 for (casadi_int k=0; k<nrhs; ++k) {
135 cs_pvec(m->S->q, x, t, m->A.n) ;
136 cs_ltsolve(m->L->L, t) ;
137 cs_lsolve(m->L->L, t) ;
138 cs_pvec(m->L->pinv, t, x, m->A.n) ;
140 cs_ipvec(m->L->pinv, x, t, m->A.n) ;
141 cs_lsolve(m->L->L, t) ;
142 cs_ltsolve(m->L->L, t) ;
143 cs_ipvec(m->S->q, t, x, m->A.n) ;
static const std::string meta_doc
A documentation string.
CSparseCholeskyInterface(const std::string &name, const Sparsity &sp)
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
static LinsolInternal * creator(const std::string &name, const Sparsity &sp)
Create a new LinsolInternal.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
static const Options options_
Options.
void copy_vector(const std::vector< S > &s, std::vector< D > &d)
std::string str(const T &v)
String representation, any type.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.