25 #define CASADI_DM_INSTANTIATOR_CPP
26 #include "matrix_impl.hpp"
28 #include "blas_impl.hpp"
29 #include "filesystem_impl.hpp"
36 const std::string& lsolver,
const Dict& dict) {
37 Linsol mysolver(
"tmp_solve", lsolver, A.sparsity(), dict);
38 return mysolver.solve(A, b,
false);
44 const std::string& lsolver,
const Dict& dict) {
45 return solve(A, DM::eye(A.size1()), lsolver, dict);
50 det(
const DM& A,
const std::string& lsolver,
const Dict& dict) {
51 Linsol mysolver(
"tmp_det", lsolver, A.sparsity(), dict);
52 return mysolver.det(A);
57 pinv(
const DM& A,
const std::string& lsolver,
59 if (A.size1()>=A.size2()) {
60 return solve(mtimes(A.T(), A), A.T(), lsolver, dict);
62 return solve(mtimes(A, A.T()), A, lsolver, dict).T();
68 rand(
const Sparsity& sp) {
70 std::uniform_real_distribution<double> distribution(0., 1.);
72 std::vector<double> nz(sp.nnz());
73 for (
double& e : nz) e = distribution(rng_);
75 return DM(sp, nz,
false);
81 Function ret =
expmsol(
"mysolver",
"slicot", A.sparsity());
82 return ret(std::vector<DM>{A, 1})[0];
87 expm_const(
const DM& A,
const DM& t) {
93 _logsumexp(
const DM& A) {
94 return casadi_logsumexp(A.ptr(), A.numel());
98 std::vector<DM> CASADI_EXPORT DM::
99 cse(
const std::vector<DM>& e) {
104 std::vector<double> CASADI_EXPORT DM::
105 call(
const Function& f,
const std::vector<double>& dep) {
106 casadi_error(
"Not implemented");
109 template<>
void CASADI_EXPORT DM::export_code(
const std::string& lang,
110 std::ostream &stream,
const Dict& options)
const {
112 casadi_assert(lang==
"matlab",
"Only matlab language supported for now.");
115 bool opt_inline =
false;
116 std::string name =
"m";
117 casadi_int indent_level = 0;
118 bool spoof_zero =
false;
121 for (
auto&& op : options) {
122 if (op.first==
"inline") {
123 opt_inline = op.second;
124 }
else if (op.first==
"name") {
125 name = op.second.to_string();
126 }
else if (op.first==
"indent_level") {
127 indent_level = op.second;
128 }
else if (op.first==
"spoof_zero") {
129 spoof_zero = op.second;
131 casadi_error(
"Unknown option '" + op.first +
"'.");
137 for (casadi_int i=0;i<indent_level;++i) {
141 casadi_assert(!opt_inline,
"Inline not supported for now.");
144 std::ios_base::fmtflags fmtfl = stream.flags();
145 stream << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1);
148 std::vector<double> d = nonzeros();
152 for (
double& e : d) {
158 if (is_scalar(
true)) {
159 stream << indent << name <<
" = " << d[0] <<
";" << std::endl;
165 bool all_equal =
true;
173 if (all_equal && !d.empty()) {
175 stream << indent << name <<
"_nz = ones(1, " << d.size() <<
")*" << d[0] <<
";" << std::endl;
178 stream << indent << name <<
"_nz = [";
179 for (casadi_int i=0;i<d.size();++i) {
180 stream << d[i] <<
" ";
181 if ((i+1)%20 == 0) stream <<
"..." << std::endl << indent <<
" ";
183 stream <<
"];" << std::endl;
192 stream << indent << name <<
" = reshape(";
193 stream << name <<
"_nz, ";
194 stream << size1() <<
", " << size2() <<
");" << std::endl;
198 opts[
"as_matrix"] =
false;
199 opts[
"indent_level"] = indent_level;
201 opts[
"indent_level"] = opt_inline;
202 sparsity().export_code(lang, stream, opts);
203 stream << indent << name <<
" = sparse(" << name <<
"_i, " << name <<
"_j, ";
204 stream << name <<
"_nz, ";
205 stream << size1() <<
", " << size2() <<
");" << std::endl;
210 Dict CASADI_EXPORT DM::info()
const {
211 return {{
"sparsity", sparsity().info()}, {
"data", nonzeros()}};
215 void CASADI_EXPORT DM::to_file(
const std::string& filename,
216 const Sparsity& sp,
const double* nonzeros,
217 const std::string& format_hint) {
218 std::string format = Sparsity::file_format(filename, format_hint, {
"mtx",
"txt"});
219 auto out_ptr = Filesystem::ofstream_ptr(filename);
220 std::ostream& out = *out_ptr;
223 out <<
"%%MatrixMarket matrix coordinate real general" << std::endl;
224 out << sp.size1() <<
" " << sp.size2() <<
" " << sp.nnz() << std::endl;
225 std::vector<casadi_int> row = sp.get_row();
226 std::vector<casadi_int> col = sp.get_col();
228 for (casadi_int k=0;k<row.size();++k) {
229 out << row[k]+1 <<
" " << col[k]+1 <<
" ";
233 }
else if (format==
"txt") {
237 casadi_int size1 = sp.size1();
238 casadi_int size2 = sp.size2();
239 const casadi_int* colind = sp.colind();
240 const casadi_int* row = sp.row();
243 std::vector<casadi_int> ind(colind, colind+size2+1);
246 casadi_int w = std::numeric_limits<double>::digits10 + 9;
249 for (casadi_int rr=0; rr<size1; ++rr) {
251 for (casadi_int cc=0; cc<size2; ++cc) {
253 if (cc<size2-1) out << std::setw(w);
255 if (ind[cc]<colind[cc+1] && row[ind[cc]]==rr) {
258 out << std::setw(w) <<
"00";
260 if (cc<size2-1) out <<
" ";
265 casadi_error(
"Unknown format '" + format +
"'");
270 DM CASADI_EXPORT DM::from_file(
const std::string& filename,
const std::string& format_hint) {
271 std::string format = Sparsity::file_format(filename, format_hint, {
"mtx",
"txt"});
272 auto in_ptr = Filesystem::ifstream_ptr(filename);
273 std::istream& in = *in_ptr;
277 std::vector<double> values;
278 casadi_int n_row = 0;
279 casadi_int n_col = 0;
280 bool first_line =
true;
281 std::istringstream stream;
283 std::vector<casadi_int> row;
284 std::vector<casadi_int> col;
289 while (std::getline(in, line)) {
291 if (line.empty())
continue;
294 if (line[0]==
'%' || line[0]==
'#' || line[0]==
'/')
continue;
303 for (i=0; !stream.eof(); ++i) {
304 casadi_int start = stream.tellg();
308 casadi_assert(ret==0,
"Parsing error on line " +
str(i+1) +
", column " +
str(start+1));
309 casadi_int stop = line.size();
310 if (!stream.eof()) stop = stream.tellg();
313 bool structural_zero =
false;
316 casadi_int n_zeros = 0;
317 for (casadi_int k=start;k<stop;++k) {
319 if (c==
' ' || c==
'\t')
continue;
326 if (n_zeros==2) structural_zero =
true;
329 if (!structural_zero) {
330 row.push_back(n_row);
332 values.push_back(val);
334 if (first_line) n_col++;
338 casadi_assert(i==n_col,
"Inconsistent dimensions. "
339 "File started with " +
str(n_col) +
", while line " +
str(n_row+1) +
340 " has " +
str(i) +
".");
345 return DM::triplet(row, col, values, n_row, n_col);
346 }
else if (format==
"mtx") {
348 bool first_line =
true;
349 std::istringstream stream;
351 casadi_int n_row=0, n_col=0, nnz=0;
352 std::vector<double> values;
353 std::vector<casadi_int> row, col;
357 while (std::getline(in, line)) {
359 if (line.empty())
continue;
362 if (line[0]==
'%' || line[0]==
'#' || line[0]==
'/')
continue;
372 casadi_assert(!stream.fail(),
"Could not parse first line");
386 values.push_back(val);
389 return DM::triplet(row, col, values, n_row, n_col);
391 casadi_error(
"Unknown format '" + format +
"'");
401 void CASADI_EXPORT mtimes_dense_dispatch<double>(
402 const std::string& blas,
403 const double* A, casadi_int m, casadi_int k,
404 const double* B, casadi_int n,
double* C) {
405 Blas::mtimes(Blas::shorthand_for(blas), A, m, k, B, n,
C);
411 #pragma GCC diagnostic push
412 #pragma GCC diagnostic ignored "-Wattributes"
416 #pragma GCC diagnostic pop
static Matrix< double > solve(const Matrix< double > &A, const Matrix< double > &b)
Function expmsol(const std::string &name, const std::string &solver, const Sparsity &A, const Dict &opts)
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void normalized_setup(std::istream &stream)
const double nan
Not a number.
int normalized_in(std::istream &stream, double &ret)
void normalized_out(std::ostream &stream, double val)