26 #include "linsol_qr.hpp"
27 #include "casadi/core/global_options.hpp"
32 int CASADI_LINSOL_QR_EXPORT
37 plugin->version = CASADI_VERSION;
60 "Minimum R entry before singularity is declared [1e-12]"}},
63 "Amount of factorisations to remember (thread-local) [0]"}}
74 for (
auto&& op : opts) {
75 if (op.first==
"eps") {
77 }
else if (op.first==
"cache") {
88 std::vector<bool> seen(p.size(),
false);
89 casadi_int ncycle = 0;
90 for (casadi_int i=0; i<static_cast<casadi_int>(p.size()); ++i) {
93 for (casadi_int j=i; !seen[j]; j=p[j]) seen[j] =
true;
96 return (
static_cast<casadi_int
>(p.size())-ncycle) % 2 ? -1 : 1;
113 m->beta.resize(
ncol());
130 double* cache =
nullptr;
131 bool cache_hit = casadi_cache_check(A,
get_ptr(m->cache),
get_ptr(m->cache_loc),
134 if (cache && cache_hit) {
149 casadi_int irmin, nullity;
153 print(
"Singularity detected: Rank %lld<%lld\n",
ncol()-nullity,
ncol());
154 print(
"First singular R entry: %g<%g, corresponding to row %lld\n", rmin,
eps_, irmin);
156 print(
"Linear combination of columns:\n[");
157 for (casadi_int k=0; k<
ncol(); ++k)
print(k==0 ?
"%g" :
", %g", m->w[k]);
172 int LinsolQr::solve(
void* mem,
const double* A,
double* x, casadi_int nrhs,
bool tr)
const {
174 casadi_qr_solve(x, nrhs, tr,
195 g.
local(
"qr_v",
"casadi_real",
"*");
197 g.
local(
"qr_r",
"casadi_real",
"*");
198 g <<
"qr_r = w+" <<
sp_v_.
nnz() <<
";\n";
199 g.
local(
"qr_beta",
"casadi_real",
"*");
201 g.
local(
"qr_w",
"casadi_real",
"*");
207 g <<
"casadi_real *c;\n";
209 g <<
"int cache_loc[" <<
n_cache_ <<
"] = {";
210 for (casadi_int i=0;i<
n_cache_;++i) {
214 g <<
"if (" << g.
cache_check(A,
"cache",
"cache_loc",
216 casadi_int offset =
sp_.
nnz();
217 g.
comment(
"Retrieve from cache");
220 g << g.
copy(
"c+" +
str(offset),
ncol(),
"qr_beta") <<
"\n"; offset+=
ncol();
225 g << g.
qr(sp, A,
"qr_w", sp_v,
"qr_v", sp_r,
"qr_r",
"qr_beta", prinv, pc) <<
"\n";
228 casadi_int offset = 0;
233 g << g.
copy(
"qr_beta",
ncol(),
"c+"+
str(offset)) <<
"\n"; offset+=
ncol();
241 casadi_int nrhs,
bool tr)
const {
250 const std::string& d)
const {
259 int version = s.
version(
"LinsolQr", 1, 2);
Helper class for C code generation.
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
void comment(const std::string &s)
Write a comment line (ignored if not verbose)
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
std::string cache_check(const std::string &key, const std::string &cache, const std::string &loc, casadi_int stride, casadi_int sz, casadi_int key_sz, const std::string &val)
cache check
std::string qr_solve(const std::string &x, casadi_int nrhs, bool tr, const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta, const std::string &prinv, const std::string &pc, const std::string &w)
QR solve.
std::string det(const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta)
Determinant from sparse QR factors.
std::string sparsity(const Sparsity &sp, bool canonical=true)
std::string qr(const std::string &sp, const std::string &A, const std::string &w, const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta, const std::string &prinv, const std::string &pc)
QR factorization.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void init(const Dict &opts) override
Initialize.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
casadi_int nrow() const
Get sparsity pattern.
int init_mem(void *mem) const override
Initalize memory block.
static const std::string meta_doc
A documentation string.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
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
int nfact(void *mem, const double *A) const override
Numeric factorization.
std::vector< casadi_int > pc_
void generate_det(CodeGenerator &g, const std::string &A, const std::string &d) const override
Generate C code for the determinant.
std::vector< casadi_int > prinv_
Symbolic factorization.
static LinsolInternal * creator(const std::string &name, const Sparsity &sp)
Create a new LinsolInternal.
double pivot_sign_
Sign of the row/column pivoting permutations (data-independent)
void generate(CodeGenerator &g, const std::string &A, const std::string &x, casadi_int nrhs, bool tr) const override
Generate C code.
int init_mem(void *mem) const override
Initalize memory block.
void generate_factorize(CodeGenerator &g, const std::string &A) const
casadi_int n_cache_
Cache size.
double det(void *mem, const double *A) const override
Determinant.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
static const Options options_
Options.
void finalize() override
Finalize the object creation.
LinsolQr(const std::string &name, const Sparsity &sp)
void init(const Dict &opts) override
Initialize.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
void print(const char *fmt,...) const
C-style formatted printing during evaluation.
bool verbose_
Verbose printout.
virtual void finalize()
Finalize the object creation.
static const Options options_
Options.
void clear_mem()
Clear all memory (called from destructor)
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
casadi_int nnz() const
Get the number of (structural) non-zeros.
void qr_sparse(Sparsity &V, Sparsity &R, std::vector< casadi_int > &prinv, std::vector< casadi_int > &pc, bool amd=true) const
Symbolic QR factorization.
static double permutation_sign(const std::vector< casadi_int > &p)
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void CASADI_LINSOL_QR_EXPORT casadi_load_linsol_qr()
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
int CASADI_LINSOL_QR_EXPORT casadi_register_linsol_qr(LinsolInternal::Plugin *plugin)
Options metadata for a class.