25 #define CASADI_DM_INSTANTIATOR_CPP
26 #include "matrix_impl.hpp"
28 #include "filesystem_impl.hpp"
35 const std::string& lsolver,
const Dict& dict) {
36 Linsol mysolver(
"tmp_solve", lsolver, A.sparsity(), dict);
37 return mysolver.solve(A, b,
false);
43 const std::string& lsolver,
const Dict& dict) {
44 return solve(A, DM::eye(A.size1()), lsolver, dict);
49 pinv(
const DM& A,
const std::string& lsolver,
51 if (A.size1()>=A.size2()) {
52 return solve(mtimes(A.T(), A), A.T(), lsolver, dict);
54 return solve(mtimes(A, A.T()), A, lsolver, dict).T();
60 rand(
const Sparsity& sp) {
62 std::uniform_real_distribution<double> distribution(0., 1.);
64 std::vector<double> nz(sp.nnz());
65 for (
double& e : nz) e = distribution(rng_);
67 return DM(sp, nz,
false);
73 Function ret =
expmsol(
"mysolver",
"slicot", A.sparsity());
74 return ret(std::vector<DM>{A, 1})[0];
79 expm_const(
const DM& A,
const DM& t) {
85 _logsumexp(
const DM& A) {
86 return casadi_logsumexp(A.ptr(), A.numel());
90 std::vector<DM> CASADI_EXPORT DM::
91 cse(
const std::vector<DM>& e) {
96 std::vector<double> CASADI_EXPORT DM::
97 call(
const Function& f,
const std::vector<double>& dep) {
98 casadi_error(
"Not implemented");
101 template<>
void CASADI_EXPORT DM::export_code(
const std::string& lang,
102 std::ostream &stream,
const Dict& options)
const {
104 casadi_assert(lang==
"matlab",
"Only matlab language supported for now.");
107 bool opt_inline =
false;
108 std::string name =
"m";
109 casadi_int indent_level = 0;
110 bool spoof_zero =
false;
113 for (
auto&& op : options) {
114 if (op.first==
"inline") {
115 opt_inline = op.second;
116 }
else if (op.first==
"name") {
117 name = op.second.to_string();
118 }
else if (op.first==
"indent_level") {
119 indent_level = op.second;
120 }
else if (op.first==
"spoof_zero") {
121 spoof_zero = op.second;
123 casadi_error(
"Unknown option '" + op.first +
"'.");
129 for (casadi_int i=0;i<indent_level;++i) {
133 casadi_assert(!opt_inline,
"Inline not supported for now.");
136 std::ios_base::fmtflags fmtfl = stream.flags();
137 stream << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1);
140 std::vector<double> d = nonzeros();
144 for (
double& e : d) {
150 if (is_scalar(
true)) {
151 stream << indent << name <<
" = " << d[0] <<
";" << std::endl;
157 bool all_equal =
true;
165 if (all_equal && !d.empty()) {
167 stream << indent << name <<
"_nz = ones(1, " << d.size() <<
")*" << d[0] <<
";" << std::endl;
170 stream << indent << name <<
"_nz = [";
171 for (casadi_int i=0;i<d.size();++i) {
172 stream << d[i] <<
" ";
173 if ((i+1)%20 == 0) stream <<
"..." << std::endl << indent <<
" ";
175 stream <<
"];" << std::endl;
184 stream << indent << name <<
" = reshape(";
185 stream << name <<
"_nz, ";
186 stream << size1() <<
", " << size2() <<
");" << std::endl;
190 opts[
"as_matrix"] =
false;
191 opts[
"indent_level"] = indent_level;
193 opts[
"indent_level"] = opt_inline;
194 sparsity().export_code(lang, stream, opts);
195 stream << indent << name <<
" = sparse(" << name <<
"_i, " << name <<
"_j, ";
196 stream << name <<
"_nz, ";
197 stream << size1() <<
", " << size2() <<
");" << std::endl;
202 Dict CASADI_EXPORT DM::info()
const {
203 return {{
"sparsity", sparsity().info()}, {
"data", nonzeros()}};
207 void CASADI_EXPORT DM::to_file(
const std::string& filename,
208 const Sparsity& sp,
const double* nonzeros,
209 const std::string& format_hint) {
210 std::string format = Sparsity::file_format(filename, format_hint, {
"mtx",
"txt"});
212 Filesystem::open(out, filename);
215 out <<
"%%MatrixMarket matrix coordinate real general" << std::endl;
216 out << sp.size1() <<
" " << sp.size2() <<
" " << sp.nnz() << std::endl;
217 std::vector<casadi_int> row = sp.get_row();
218 std::vector<casadi_int> col = sp.get_col();
220 for (casadi_int k=0;k<row.size();++k) {
221 out << row[k]+1 <<
" " << col[k]+1 <<
" ";
225 }
else if (format==
"txt") {
229 casadi_int size1 = sp.size1();
230 casadi_int size2 = sp.size2();
231 const casadi_int* colind = sp.colind();
232 const casadi_int* row = sp.row();
235 std::vector<casadi_int> ind(colind, colind+size2+1);
238 casadi_int w = std::numeric_limits<double>::digits10 + 9;
241 for (casadi_int rr=0; rr<size1; ++rr) {
243 for (casadi_int cc=0; cc<size2; ++cc) {
245 if (cc<size2-1) out << std::setw(w);
247 if (ind[cc]<colind[cc+1] && row[ind[cc]]==rr) {
250 out << std::setw(w) <<
"00";
252 if (cc<size2-1) out <<
" ";
257 casadi_error(
"Unknown format '" + format +
"'");
262 DM CASADI_EXPORT DM::from_file(
const std::string& filename,
const std::string& format_hint) {
263 std::string format = Sparsity::file_format(filename, format_hint, {
"mtx",
"txt"});
264 std::ifstream in(filename);
265 casadi_assert(in.good(),
"Could not open '" + filename +
"'.");
269 std::vector<double> values;
270 casadi_int n_row = 0;
271 casadi_int n_col = 0;
272 bool first_line =
true;
273 std::istringstream stream;
275 std::vector<casadi_int> row;
276 std::vector<casadi_int> col;
281 while (std::getline(in, line)) {
283 if (line.empty())
continue;
286 if (line[0]==
'%' || line[0]==
'#' || line[0]==
'/')
continue;
295 for (i=0; !stream.eof(); ++i) {
296 casadi_int start = stream.tellg();
300 casadi_assert(ret==0,
"Parsing error on line " +
str(i+1) +
", column " +
str(start+1));
301 casadi_int stop = line.size();
302 if (!stream.eof()) stop = stream.tellg();
305 bool structural_zero =
false;
308 casadi_int n_zeros = 0;
309 for (casadi_int k=start;k<stop;++k) {
311 if (c==
' ' || c==
'\t')
continue;
318 if (n_zeros==2) structural_zero =
true;
321 if (!structural_zero) {
322 row.push_back(n_row);
324 values.push_back(val);
326 if (first_line) n_col++;
330 casadi_assert(i==n_col,
"Inconsistent dimensions. "
331 "File started with " +
str(n_col) +
", while line " +
str(n_row+1) +
332 " has " +
str(i) +
".");
337 return DM::triplet(row, col, values, n_row, n_col);
338 }
else if (format==
"mtx") {
340 bool first_line =
true;
341 std::istringstream stream;
343 casadi_int n_row=0, n_col=0, nnz=0;
344 std::vector<double> values;
345 std::vector<casadi_int> row, col;
349 while (std::getline(in, line)) {
351 if (line.empty())
continue;
354 if (line[0]==
'%' || line[0]==
'#' || line[0]==
'/')
continue;
364 casadi_assert(!stream.fail(),
"Could not parse first line");
378 values.push_back(val);
381 return DM::triplet(row, col, values, n_row, n_col);
383 casadi_error(
"Unknown format '" + format +
"'");
390 #pragma GCC diagnostic push
391 #pragma GCC diagnostic ignored "-Wattributes"
395 #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)