matrix_decl.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_MATRIX_DECL_HPP
27 #define CASADI_MATRIX_DECL_HPP
28 
29 #include "matrix_fwd.hpp"
30 #include "exception.hpp"
31 #include "casadi_limits.hpp"
32 #include "runtime/casadi_runtime.hpp"
33 #include "generic_matrix.hpp"
34 #include "generic_expression.hpp"
35 #include "printable.hpp"
36 
37 #include <random>
38 #include <typeinfo>
39 #include <vector>
40 #include <initializer_list>
41 
42 namespace casadi {
43 
49  struct CASADI_EXPORT MatrixCommon {};
50 
53 
56  template <typename Scalar> inline std::string matrixName()
57  { return std::string("Matrix<") + typeid(Scalar).name() + std::string(">");}
58  template<> inline std::string matrixName<double>() { return "DM"; }
59  template<> inline std::string matrixName<casadi_int>() { return "IM"; }
62 
87  template<typename Scalar>
88  class CASADI_EXPORT Matrix :
89  public MatrixCommon,
90  public SWIG_IF_ELSE(GenericExpressionCommon, GenericExpression<Matrix<Scalar> >),
91  public GenericMatrix<Matrix<Scalar> >,
92  public SWIG_IF_ELSE(PrintableCommon, Printable<Matrix<Scalar> >) {
93  public:
94 
99  Matrix();
100 
102  Matrix(const Matrix<Scalar>& m);
103 
104 #ifndef SWIG
106  Matrix<Scalar>& operator=(const Matrix<Scalar>& m);
107 #endif // SWIG
108 
112  Matrix(casadi_int nrow, casadi_int ncol);
113 
114 #ifndef SWIG
118  explicit Matrix(const std::pair<casadi_int, casadi_int>& rc);
119 
123  std::vector<Scalar>* operator->();
124 
128  const std::vector<Scalar>* operator->() const;
129 #endif // SWIG
130 
136  explicit Matrix(const Sparsity& sp);
137 
141  Matrix(const Sparsity& sp, const Matrix<Scalar>& d);
142 
144  Matrix(double val);
145 
146 #if !(defined(SWIG) && defined(SWIGMATLAB))
148  explicit Matrix(const std::vector< std::vector<double> >& m);
149 
153  template<typename A>
154  Matrix(const std::vector<A>& x) : sparsity_(Sparsity::dense(x.size(), 1)),
155  nonzeros_(std::vector<Scalar>(x.size())) {
156  auto x_it = x.begin();
157  for (auto&& d : nonzeros_) d = static_cast<Scalar>(*x_it++);
158  }
159 #endif
160 
166  template<typename A>
167  Matrix(const Matrix<A>& x) : sparsity_(x.sparsity()), nonzeros_(std::vector<Scalar>(x.nnz())) {
168  auto x_it = x->begin();
169  for (auto&& d : nonzeros_) d = static_cast<Scalar>(*x_it++);
170  }
171 
172 #ifndef SWIG
174  Matrix(const std::vector<Scalar>& x);
175 
177  Matrix(std::initializer_list<Scalar> x) : Matrix<Scalar>(std::vector<Scalar>(x)) {}
178 
180  const Scalar scalar() const;
181 
183  typedef Scalar ScalarType;
184 
186  typedef GenericMatrix<Matrix<Scalar> > B;
187 
189  using B::nnz;
190  using B::nnz_lower;
191  using B::nnz_upper;
192  using B::numel;
193  using B::size1;
194  using B::size2;
195  using B::size;
196  using B::is_empty;
197  using B::is_scalar;
198  using B::is_dense;
199  using B::is_vector;
200  using B::is_row;
201  using B::is_column;
202  using B::is_tril;
203  using B::is_triu;
204  using B::colind;
205  using B::row;
206  using B::dim;
207  using B::sym;
208  using B::zeros;
209  using B::ones;
210  using B::nz;
211  using B::operator();
212  using B::horzsplit;
213  using B::vertsplit;
214  using B::diagsplit;
215  using B::mtimes;
216 #endif // SWIG
217 
219  bool has_nz(casadi_int rr, casadi_int cc) const;
220 
222  bool __nonzero__() const;
223 
226  void get(Matrix<Scalar>& SWIG_OUTPUT(m), bool ind1, const Slice& rr) const;
227  void get(Matrix<Scalar>& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& rr) const;
228  void get(Matrix<Scalar>& SWIG_OUTPUT(m), bool ind1, const Sparsity& sp) const;
230 
233  void get(Matrix<Scalar>& SWIG_OUTPUT(m), bool ind1,
234  const Slice& rr, const Slice& cc) const;
235  void get(Matrix<Scalar>& SWIG_OUTPUT(m), bool ind1,
236  const Slice& rr, const Matrix<casadi_int>& cc) const;
237  void get(Matrix<Scalar>& SWIG_OUTPUT(m), bool ind1,
238  const Matrix<casadi_int>& rr, const Slice& cc) const;
239  void get(Matrix<Scalar>& SWIG_OUTPUT(m), bool ind1,
240  const Matrix<casadi_int>& rr, const Matrix<casadi_int>& cc) const;
242 
245  void set(const Matrix<Scalar>& m, bool ind1, const Slice& rr);
246  void set(const Matrix<Scalar>& m, bool ind1, const Matrix<casadi_int>& rr);
247  void set(const Matrix<Scalar>& m, bool ind1, const Sparsity& sp);
249 
252  void set(const Matrix<Scalar>& m, bool ind1, const Slice& rr, const Slice& cc);
253  void set(const Matrix<Scalar>& m, bool ind1, const Slice& rr, const Matrix<casadi_int>& cc);
254  void set(const Matrix<Scalar>& m, bool ind1, const Matrix<casadi_int>& rr, const Slice& cc);
255  void set(const Matrix<Scalar>& m, bool ind1, const Matrix<casadi_int>& rr,
256  const Matrix<casadi_int>& cc);
258 
261  void get_nz(Matrix<Scalar>& SWIG_OUTPUT(m), bool ind1, const Slice& k) const;
262  void get_nz(Matrix<Scalar>& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& k) const;
264 
267  void set_nz(const Matrix<Scalar>& m, bool ind1, const Slice& k);
268  void set_nz(const Matrix<Scalar>& m, bool ind1, const Matrix<casadi_int>& k);
270 
273 
276 
279  static Matrix<Scalar> binary(casadi_int op, const Matrix<Scalar> &x, const Matrix<Scalar> &y);
280  static Matrix<Scalar> unary(casadi_int op, const Matrix<Scalar> &x);
281  static Matrix<Scalar> scalar_matrix(casadi_int op,
282  const Matrix<Scalar> &x, const Matrix<Scalar> &y);
283  static Matrix<Scalar> matrix_scalar(casadi_int op,
284  const Matrix<Scalar> &x, const Matrix<Scalar> &y);
285  static Matrix<Scalar> matrix_matrix(casadi_int op,
286  const Matrix<Scalar> &x, const Matrix<Scalar> &y);
289 
290 #ifndef SWIG
294  static bool is_equal(const Matrix<Scalar> &x, const Matrix<Scalar> &y, casadi_int depth=0);
295  static Matrix<Scalar> mmin(const Matrix<Scalar> &x);
296  static Matrix<Scalar> mmax(const Matrix<Scalar> &x);
298 
301  static Matrix<Scalar> simplify(const Matrix<Scalar> &x);
302  static Matrix<Scalar> jacobian(const Matrix<Scalar> &f, const Matrix<Scalar> &x,
303  const Dict& opts = Dict());
304  static Sparsity jacobian_sparsity(const Matrix<Scalar> &f, const Matrix<Scalar> &x);
305  static Matrix<Scalar> hessian(const Matrix<Scalar> &f, const Matrix<Scalar> &x,
306  const Dict& opts = Dict());
307  static Matrix<Scalar> hessian(const Matrix<Scalar> &f, const Matrix<Scalar> &x,
308  Matrix<Scalar>& g, const Dict& opts = Dict());
309  static Matrix<Scalar>
310  substitute(const Matrix<Scalar>& ex,
311  const Matrix<Scalar>& v,
312  const Matrix<Scalar>& vdef);
313  static std::vector<Matrix<Scalar> >
314  substitute(const std::vector<Matrix<Scalar> >& ex,
315  const std::vector<Matrix<Scalar> >& v,
316  const std::vector<Matrix<Scalar> >& vdef);
317  static void substitute_inplace(const std::vector<Matrix<Scalar> >& v,
318  std::vector<Matrix<Scalar> >& vdef,
319  std::vector<Matrix<Scalar> >& ex,
320  bool revers);
321  static Matrix<Scalar> pinv(const Matrix<Scalar> &x);
322  static Matrix<Scalar> pinv(const Matrix<Scalar> &A,
323  const std::string& lsolver, const Dict& opts);
324  static Matrix<Scalar> expm_const(const Matrix<Scalar> &A, const Matrix<Scalar> &t);
325  static Matrix<Scalar> expm(const Matrix<Scalar> &A);
326  static Matrix<Scalar> solve(const Matrix<Scalar> &A, const Matrix<Scalar>& b);
327  static Matrix<Scalar> solve(const Matrix<Scalar> &A, const Matrix<Scalar>& b,
328  const std::string& lsolver, const Dict& opts);
329  static Matrix<Scalar> inv(const Matrix<Scalar> &A);
330  static Matrix<Scalar> inv(const Matrix<Scalar> &A,
331  const std::string& lsolver, const Dict& opts);
332 
333  static casadi_int n_nodes(const Matrix<Scalar> &x);
334  static std::string print_operator(const Matrix<Scalar> &x,
335  const std::vector<std::string>& args);
336  static void extract(std::vector<Matrix<Scalar>>& ex, std::vector<Matrix<Scalar>>& v,
337  std::vector<Matrix<Scalar>>& vdef, const Dict& opts = Dict());
338  static void shared(std::vector<Matrix<Scalar> >& ex,
339  std::vector<Matrix<Scalar> >& v,
340  std::vector<Matrix<Scalar> >& vdef,
341  const std::string& v_prefix,
342  const std::string& v_suffix);
343  static Matrix<Scalar> _bilin(const Matrix<Scalar>& A,
344  const Matrix<Scalar>& x,
345  const Matrix<Scalar>& y);
346  static Matrix<Scalar> _rank1(const Matrix<Scalar>& A,
347  const Matrix<Scalar>& alpha,
348  const Matrix<Scalar>& x,
349  const Matrix<Scalar>& y);
350  static Matrix<Scalar> if_else(const Matrix<Scalar> &x,
351  const Matrix<Scalar> &if_true,
352  const Matrix<Scalar> &if_false,
353  bool short_circuit=false);
354  static Matrix<Scalar> conditional(const Matrix<Scalar> &ind,
355  const std::vector<Matrix<Scalar> > &x,
356  const Matrix<Scalar> &x_default,
357  bool short_circuit=false);
358  static bool depends_on(const Matrix<Scalar> &x, const Matrix<Scalar> &arg);
359  static Matrix<Scalar> mrdivide(const Matrix<Scalar> &x, const Matrix<Scalar> &y);
360  static Matrix<Scalar> mldivide(const Matrix<Scalar> &x, const Matrix<Scalar> &y);
361  static std::vector<Matrix<Scalar> > symvar(const Matrix<Scalar> &x);
362  static Matrix<Scalar> det(const Matrix<Scalar> &x);
363  static Matrix<Scalar> inv_minor(const Matrix<Scalar> &x);
364  static Matrix<Scalar> trace(const Matrix<Scalar> &x);
365  static Matrix<Scalar> norm_1(const Matrix<Scalar> &x);
366  static Matrix<Scalar> norm_2(const Matrix<Scalar> &x);
367  static Matrix<Scalar> norm_fro(const Matrix<Scalar> &x);
368  static Matrix<Scalar> norm_inf(const Matrix<Scalar> &x);
369  static Matrix<Scalar> sum2(const Matrix<Scalar> &x);
370  static Matrix<Scalar> sum1(const Matrix<Scalar> &x);
371  static Matrix<Scalar> dot(const Matrix<Scalar> &x, const Matrix<Scalar> &y);
372  static Matrix<Scalar> nullspace(const Matrix<Scalar> &x);
373  static Matrix<Scalar> diag(const Matrix<Scalar> &x);
374  static Matrix<Scalar> unite(const Matrix<Scalar> &A, const Matrix<Scalar>& B);
375  static Matrix<Scalar> project(const Matrix<Scalar> &x,
376  const Sparsity& sp, bool intersect=false);
377  static Matrix<Scalar> polyval(const Matrix<Scalar> &p, const Matrix<Scalar>& x);
378  static Matrix<Scalar> densify(const Matrix<Scalar> &x, const Matrix<Scalar>& val);
379  static Matrix<Scalar> densify(const Matrix<Scalar> &x);
380  static Matrix<Scalar> einstein(const Matrix<Scalar>& A, const Matrix<Scalar>& B,
381  const Matrix<Scalar>& C,
382  const std::vector<casadi_int>& dim_a, const std::vector<casadi_int>& dim_b,
383  const std::vector<casadi_int>& dim_c,
384  const std::vector<casadi_int>& a, const std::vector<casadi_int>& b,
385  const std::vector<casadi_int>& c);
386 
387  static Matrix<Scalar> einstein(const Matrix<Scalar>& A, const Matrix<Scalar>& B,
388  const std::vector<casadi_int>& dim_a, const std::vector<casadi_int>& dim_b,
389  const std::vector<casadi_int>& dim_c,
390  const std::vector<casadi_int>& a, const std::vector<casadi_int>& b,
391  const std::vector<casadi_int>& c);
392  static Matrix<Scalar> cumsum(const Matrix<Scalar> &x, casadi_int axis=-1);
393  static Matrix<Scalar> _logsumexp(const Matrix<Scalar>& x);
394  static std::vector< Matrix<Scalar> > cse(const std::vector< Matrix<Scalar> >& e);
396 
399  static Matrix<Scalar> blockcat(const std::vector< std::vector<Matrix<Scalar> > > &v);
400  static Matrix<Scalar> horzcat(const std::vector<Matrix<Scalar> > &v);
401  static std::vector<Matrix<Scalar> >
402  horzsplit(const Matrix<Scalar>& x,
403  const std::vector<casadi_int>& offset);
404  static Matrix<Scalar> vertcat(const std::vector<Matrix<Scalar> > &v);
405  static std::vector< Matrix<Scalar> >
406  vertsplit(const Matrix<Scalar>& x,
407  const std::vector<casadi_int>& offset);
408  static std::vector< Matrix<Scalar> >
409  diagsplit(const Matrix<Scalar>& x,
410  const std::vector<casadi_int>& offset1,
411  const std::vector<casadi_int>& offset2);
412  static Matrix<Scalar> reshape(const Matrix<Scalar> &x, casadi_int nrow, casadi_int ncol);
413  static Matrix<Scalar> reshape(const Matrix<Scalar> &x, const Sparsity& sp);
414  static Matrix<Scalar> sparsity_cast(const Matrix<Scalar> &x, const Sparsity& sp);
415  static Matrix<Scalar> kron(const Matrix<Scalar> &x, const Matrix<Scalar>& y);
416  static Matrix<Scalar> mtimes(const Matrix<Scalar> &x, const Matrix<Scalar> &y);
417  static Matrix<Scalar> mac(const Matrix<Scalar> &x,
418  const Matrix<Scalar> &y,
419  const Matrix<Scalar> &z);
421 
424  static Matrix<Scalar> sparsify(const Matrix<Scalar> &x, double tol=0);
425  static void expand(const Matrix<Scalar>& x,
426  Matrix<Scalar>& weights,
427  Matrix<Scalar>& terms);
428  static Matrix<Scalar> pw_const(const Matrix<Scalar> &t,
429  const Matrix<Scalar> &tval, const Matrix<Scalar> &val);
430  static Matrix<Scalar> pw_lin(const Matrix<Scalar> &t,
431  const Matrix<Scalar> &tval, const Matrix<Scalar> &val);
432  static Matrix<Scalar> heaviside(const Matrix<Scalar> &x);
433  static Matrix<Scalar> rectangle(const Matrix<Scalar> &x);
434  static Matrix<Scalar> triangle(const Matrix<Scalar> &x);
435  static Matrix<Scalar> ramp(const Matrix<Scalar> &x);
436  static Matrix<Scalar> gauss_quadrature(const Matrix<Scalar> &f,
437  const Matrix<Scalar> &x, const Matrix<Scalar> &a,
438  const Matrix<Scalar> &b, casadi_int order=5);
439  static Matrix<Scalar> gauss_quadrature(const Matrix<Scalar> &f,
440  const Matrix<Scalar> &x, const Matrix<Scalar> &a,
441  const Matrix<Scalar> &b, casadi_int order,
442  const Matrix<Scalar>& w);
443  static std::vector<std::vector<Matrix<Scalar> > >
444  forward(const std::vector<Matrix<Scalar> > &ex,
445  const std::vector<Matrix<Scalar> > &arg,
446  const std::vector<std::vector<Matrix<Scalar> > > &v,
447  const Dict& opts = Dict());
448  static std::vector<std::vector<Matrix<Scalar> > >
449  reverse(const std::vector<Matrix<Scalar> > &ex,
450  const std::vector<Matrix<Scalar> > &arg,
451  const std::vector<std::vector<Matrix<Scalar> > > &v,
452  const Dict& opts = Dict());
453  static std::vector<bool> which_depends(const Matrix<Scalar> &expr, const Matrix<Scalar> &var,
454  casadi_int order=1, bool tr=false);
455  static Matrix<Scalar> taylor(const Matrix<Scalar>& ex, const Matrix<Scalar>& x,
456  const Matrix<Scalar>& a, casadi_int order);
457  static Matrix<Scalar> mtaylor(const Matrix<Scalar>& ex, const Matrix<Scalar>& x,
458  const Matrix<Scalar>& a, casadi_int order);
459  static Matrix<Scalar> mtaylor(const Matrix<Scalar>& ex,
460  const Matrix<Scalar>& x, const Matrix<Scalar>& a,
461  casadi_int order,
462  const std::vector<casadi_int>& order_contributions);
463  static Matrix<Scalar> poly_coeff(const Matrix<Scalar>& ex, const Matrix<Scalar>&x);
464  static Matrix<Scalar> poly_roots(const Matrix<Scalar>& p);
465  static Matrix<Scalar> eig_symbolic(const Matrix<Scalar>& m);
466  static Matrix<double> evalf(const Matrix<Scalar>& m);
467  static void qr_sparse(const Matrix<Scalar>& A, Matrix<Scalar>& V, Matrix<Scalar>& R,
468  Matrix<Scalar>& beta, std::vector<casadi_int>& prinv,
469  std::vector<casadi_int>& pc, bool amd=true);
470  static Matrix<Scalar> qr_solve(const Matrix<Scalar>& b, const Matrix<Scalar>& v,
471  const Matrix<Scalar>& r, const Matrix<Scalar>& beta,
472  const std::vector<casadi_int>& prinv,
473  const std::vector<casadi_int>& pc, bool tr=false);
474  static void qr(const Matrix<Scalar>& A, Matrix<Scalar>& Q, Matrix<Scalar>& R);
475  static void ldl(const Matrix<Scalar>& A, Matrix<Scalar>& D, Matrix<Scalar>& LT,
476  std::vector<casadi_int>& p, bool amd=true);
477  static Matrix<Scalar> ldl_solve(const Matrix<Scalar>& b, const Matrix<Scalar>& D,
478  const Matrix<Scalar>& LT, const std::vector<casadi_int>& p);
479  static Matrix<Scalar> all(const Matrix<Scalar>& x);
480  static Matrix<Scalar> any(const Matrix<Scalar>& x);
481  static Matrix<Scalar> adj(const Matrix<Scalar>& x);
482  static Matrix<Scalar> minor(const Matrix<Scalar>& x, casadi_int i, casadi_int j);
483  static Matrix<Scalar> cofactor(const Matrix<Scalar>& A, casadi_int i, casadi_int j);
484  static Matrix<Scalar> chol(const Matrix<Scalar>& A);
485  static Matrix<Scalar> norm_inf_mul(const Matrix<Scalar>& x, const Matrix<Scalar> &y);
486  static Matrix<Scalar> diagcat(const std::vector< Matrix<Scalar> > &A);
489 #endif // SWIG
490 
492 
494  Matrix<Scalar> T() const;
495 
496 #if !defined(SWIG) || defined(DOXYGEN)
504  friend inline Matrix<Scalar> adj(const Matrix<Scalar>& A) {
505  return Matrix<Scalar>::adj(A);
506  }
507 
511  friend inline Matrix<Scalar> minor(const Matrix<Scalar> &x, casadi_int i, casadi_int j) {
512  return Matrix<Scalar>::minor(x, i, j);
513  }
514 
518  friend inline Matrix<Scalar> cofactor(const Matrix<Scalar> &x, casadi_int i, casadi_int j) {
519  return Matrix<Scalar>::cofactor(x, i, j);
520  }
521 
530  friend inline void qr(const Matrix<Scalar>& A, Matrix<Scalar>& Q, Matrix<Scalar>& R) {
531  return Matrix<Scalar>::qr(A, Q, R);
532  }
533 
539  friend inline void qr_sparse(const Matrix<Scalar>& A, Matrix<Scalar>& V, Matrix<Scalar>& R,
540  Matrix<Scalar>& beta, std::vector<casadi_int>& prinv,
541  std::vector<casadi_int>& pc, bool amd=true) {
542  return Matrix<Scalar>::qr_sparse(A, V, R, beta, prinv, pc, amd);
543  }
544 
548  friend inline Matrix<Scalar>
550  const Matrix<Scalar>& r, const Matrix<Scalar>& beta,
551  const std::vector<casadi_int>& prinv,
552  const std::vector<casadi_int>& pc, bool tr=false) {
553  return Matrix<Scalar>::qr_solve(b, v, r, beta, prinv, pc, tr);
554  }
555 
562  friend inline Matrix<Scalar> chol(const Matrix<Scalar>& A) {
563  return Matrix<Scalar>::chol(A);
564  }
565 
573  friend inline void ldl(const Matrix<Scalar>& A, Matrix<Scalar>& D, Matrix<Scalar>& LT,
574  std::vector<casadi_int>& p, bool amd=true) {
575  return Matrix<Scalar>::ldl(A, D, LT, p, amd);
576  }
577 
581  friend inline Matrix<Scalar>
582  ldl_solve(const Matrix<Scalar>& b, const Matrix<Scalar>& D, const Matrix<Scalar>& LT,
583  const std::vector<casadi_int>& p) {
584  return Matrix<Scalar>::ldl_solve(b, D, LT, p);
585  }
586 
590  friend inline Matrix<Scalar> any(const Matrix<Scalar> &x) {
591  return Matrix<Scalar>::any(x);
592  }
593 
597  friend inline Matrix<Scalar> all(const Matrix<Scalar> &x) {
598  return Matrix<Scalar>::all(x);
599  }
600 
604  friend inline Matrix<Scalar>
606  return Matrix<Scalar>::norm_inf_mul(x, y);
607  }
608 
612  friend inline Matrix<Scalar>
613  sparsify(const Matrix<Scalar>& A, double tol=0) {
614  return Matrix<Scalar>::sparsify(A, tol);
615  }
616 
620  friend inline void expand(const Matrix<Scalar>& ex, Matrix<Scalar> &weights,
621  Matrix<Scalar>& terms) {
622  Matrix<Scalar>::expand(ex, weights, terms);
623  }
624 
635  friend inline Matrix<Scalar> pw_const(const Matrix<Scalar> &t,
636  const Matrix<Scalar> &tval,
637  const Matrix<Scalar> &val) {
638  return Matrix<Scalar>::pw_const(t, tval, val);
639  }
640 
651  friend inline Matrix<Scalar>
652  pw_lin(const Matrix<Scalar> &t, const Matrix<Scalar> &tval,
653  const Matrix<Scalar> &val) {
654  return Matrix<Scalar>::pw_lin(t, tval, val);
655  }
656 
668  friend inline Matrix<Scalar> heaviside(const Matrix<Scalar> &x) {
669  return Matrix<Scalar>::heaviside(x);
670  }
671 
685  friend inline Matrix<Scalar> rectangle(const Matrix<Scalar> &x) {
686  return Matrix<Scalar>::rectangle(x);
687  }
688 
700  friend inline Matrix<Scalar> triangle(const Matrix<Scalar> &x) {
701  return Matrix<Scalar>::triangle(x);
702  }
703 
717  friend inline Matrix<Scalar> ramp(const Matrix<Scalar> &x) {
718  return Matrix<Scalar>::ramp(x);
719  }
720 
722 
725  friend inline Matrix<Scalar>
727  const Matrix<Scalar> &a, const Matrix<Scalar> &b,
728  casadi_int order=5) {
729  return Matrix<Scalar>::gauss_quadrature(f, x, a, b, order);
730  }
731  friend inline Matrix<Scalar>
733  const Matrix<Scalar> &a, const Matrix<Scalar> &b,
734  casadi_int order, const Matrix<Scalar>& w) {
735  return Matrix<Scalar>::gauss_quadrature(f, x, a, b, order, w);
736  }
738 
740 
754  friend inline Matrix<Scalar> taylor(const Matrix<Scalar>& ex, const Matrix<Scalar>& x,
755  const Matrix<Scalar>& a, casadi_int order=1) {
756  return Matrix<Scalar>::taylor(ex, x, a, order);
757  }
758  friend inline Matrix<Scalar> taylor(const Matrix<Scalar>& ex, const Matrix<Scalar>& x) {
759  return Matrix<Scalar>::taylor(ex, x, 0, 1);
760  }
762 
770  friend inline Matrix<Scalar> mtaylor(const Matrix<Scalar>& ex, const Matrix<Scalar>& x,
771  const Matrix<Scalar>& a, casadi_int order=1) {
772  return Matrix<Scalar>::mtaylor(ex, x, a, order);
773  }
774 
801  friend inline Matrix<Scalar> mtaylor(const Matrix<Scalar>& ex, const Matrix<Scalar>& x,
802  const Matrix<Scalar>& a, casadi_int order,
803  const std::vector<casadi_int>& order_contributions) {
804  return Matrix<Scalar>::mtaylor(ex, x, a, order, order_contributions);
805  }
806 
813  friend inline Matrix<Scalar> poly_coeff(const Matrix<Scalar>& f,
814  const Matrix<Scalar>& x) {
815  return Matrix<Scalar>::poly_coeff(f, x);
816  }
817 
824  friend inline Matrix<Scalar> poly_roots(const Matrix<Scalar>& p) {
825  return Matrix<Scalar>::poly_roots(p);
826  }
827 
833  friend inline Matrix<Scalar> eig_symbolic(const Matrix<Scalar>& m) {
835  }
836 
837 
843  inline friend Matrix<double> evalf(const Matrix<Scalar>& expr) {
844  return Matrix<Scalar>::evalf(expr);
845  }
847 #endif
848 
852  static void set_max_depth(casadi_int eq_depth=1);
853 
857  static casadi_int get_max_depth();
858 
862  static std::vector<Matrix<Scalar> > get_input(const Function& f);
863 
867  static std::vector<Matrix<Scalar> > get_free(const Function& f);
868 
870  static std::string type_name();
871 
873  void print_split(std::vector<std::string>& SWIG_OUTPUT(nz),
874  std::vector<std::string>& SWIG_OUTPUT(inter)) const;
875 
877  void disp(std::ostream& stream, bool more=false) const;
878 
880  std::string get_str(bool more=false) const;
881 
883  void print_scalar(std::ostream &stream) const;
884 
886  void print_vector(std::ostream &stream, bool truncate=true) const;
887 
889  void print_dense(std::ostream &stream, bool truncate=true) const;
890 
892  void print_sparse(std::ostream &stream, bool truncate=true) const;
893 
894 #ifndef SWIG
896  static void print_default(std::ostream &stream, const Sparsity& sp, const Scalar* nonzeros,
897  bool truncate=true);
898 
900  static void print_scalar(std::ostream &stream, const Scalar& e);
901 
903  static void print_vector(std::ostream &stream, const Sparsity& sp, const Scalar* nonzeros,
904  bool truncate=true);
905 
907  static void print_sparse(std::ostream &stream, const Sparsity& sp, const Scalar* nonzeros,
908  bool truncate=true);
909 
911  static void print_split(casadi_int nnz, const Scalar* nonzeros, std::vector<std::string>& nz,
912  std::vector<std::string>& inter);
913 
915  static void print_dense(std::ostream &stream, const Sparsity& sp, const Scalar* nonzeros,
916  bool truncate=true);
917 #endif
918 
919  void clear();
920  void resize(casadi_int nrow, casadi_int ncol);
921  void reserve(casadi_int nnz);
922  void reserve(casadi_int nnz, casadi_int ncol);
923 
929  void erase(const std::vector<casadi_int>& rr, const std::vector<casadi_int>& cc,
930  bool ind1=false);
931 
937  void erase(const std::vector<casadi_int>& rr, bool ind1=false);
938 
944  void remove(const std::vector<casadi_int>& rr, const std::vector<casadi_int>& cc);
945 
952  void enlarge(casadi_int nrow, casadi_int ncol,
953  const std::vector<casadi_int>& rr, const std::vector<casadi_int>& cc,
954  bool ind1=false);
955 
956 #ifndef SWIG
959  std::vector<Scalar>& nonzeros();
960  const std::vector<Scalar>& nonzeros() const;
962 
965  Scalar* ptr();
966  const Scalar* ptr() const;
967  friend inline Scalar* get_ptr(Matrix<Scalar>& v) { return v.ptr(); }
968  friend inline const Scalar* get_ptr(const Matrix<Scalar>& v) { return v.ptr(); }
970 
972  const Sparsity& sparsity() const;
973 
974 #endif // SWIG
975 
980 
987  static Matrix<Scalar> triplet(const std::vector<casadi_int>& row,
988  const std::vector<casadi_int>& col,
989  const Matrix<Scalar>& d);
990  static Matrix<Scalar> triplet(const std::vector<casadi_int>& row,
991  const std::vector<casadi_int>& col,
992  const Matrix<Scalar>& d, casadi_int nrow, casadi_int ncol);
993  static Matrix<Scalar> triplet(const std::vector<casadi_int>& row,
994  const std::vector<casadi_int>& col,
995  const Matrix<Scalar>& d,
996  const std::pair<casadi_int, casadi_int>& rc);
998 
1000 
1003  static Matrix<Scalar> inf(const Sparsity& sp);
1004  static Matrix<Scalar> inf(casadi_int nrow=1, casadi_int ncol=1);
1005  static Matrix<Scalar> inf(const std::pair<casadi_int, casadi_int>& rc);
1007 
1009 
1012  static Matrix<Scalar> nan(const Sparsity& sp);
1013  static Matrix<Scalar> nan(casadi_int nrow=1, casadi_int ncol=1);
1014  static Matrix<Scalar> nan(const std::pair<casadi_int, casadi_int>& rc);
1016 
1020  static Matrix<Scalar> eye(casadi_int n);
1021 
1027  casadi_int element_hash() const;
1028 
1030  bool is_regular() const;
1031 
1035  bool is_smooth() const;
1036 
1042  bool is_leaf() const;
1043 
1049  bool is_commutative() const;
1050 
1065  bool is_symbolic() const;
1066 
1078  bool is_valid_input() const;
1079 
1081 
1089  bool has_duplicates() const;
1090 
1094  void reset_input() const;
1096 
1100  bool is_constant() const;
1101 
1107  bool is_integer() const;
1108 
1112  bool is_zero() const;
1113 
1117  bool is_one() const;
1118 
1122  bool is_minus_one() const;
1123 
1129  bool is_eye() const;
1130 
1132  casadi_int op() const;
1133 
1135  bool is_op(casadi_int op) const;
1136 
1140  bool has_zeros() const;
1141 
1145  std::vector<Scalar> get_nonzeros() const;
1146 
1150  std::vector<Scalar> get_elements() const;
1151 
1152 #ifndef SWIG
1156  template<typename A>
1157  std::vector<A> get_nonzeros() const;
1158 #endif // SWIG
1159 
1163  explicit operator double() const;
1164 
1168  explicit operator casadi_int() const;
1169 
1170 #ifndef SWIG
1174  template<typename A>
1175  explicit operator std::vector<A>() const;
1176 #endif // SWIG
1177 
1181  std::string name() const;
1182 
1189  Matrix<Scalar> dep(casadi_int ch=0) const;
1190 
1196  casadi_int n_dep() const;
1197 
1198  // @{
1200  static void set_precision(casadi_int precision);
1201  static void set_width(casadi_int width);
1202  static void set_scientific(bool scientific);
1203  // @}
1204 
1206  static void rng(casadi_int seed);
1207 
1209 
1212  static Matrix<Scalar> rand( // NOLINT(runtime/threadsafe_fn)
1213  casadi_int nrow=1,
1214  casadi_int ncol=1);
1215  static Matrix<Scalar> rand(const Sparsity& sp); // NOLINT(runtime/threadsafe_fn)
1216  static Matrix<Scalar> rand( // NOLINT(runtime/threadsafe_fn)
1217  const std::pair<casadi_int, casadi_int>& rc);
1219 
1234  void export_code(const std::string& lang,
1235  std::ostream &stream=casadi::uout(), const Dict& options=Dict()) const;
1236 
1238  Dict info() const;
1239  #ifndef SWIG
1243  void serialize(std::ostream &stream) const;
1244  #endif
1245 
1249  std::string serialize() const;
1250 
1254  static Matrix<Scalar> deserialize(std::istream& stream);
1255 
1259  static Matrix<Scalar> deserialize(const std::string& s);
1260 
1265 
1267 
1268  // @{
1284  void to_file(const std::string& filename, const std::string& format="") const;
1285 #ifndef SWIG
1286  static void to_file(const std::string& filename, const Sparsity& sp,
1287  const Scalar* nonzeros, const std::string& format="");
1288 #endif
1289 
1290  static Matrix<double> from_file(const std::string& filename, const std::string& format_hint="");
1292 
1293 #ifndef SWIG
1295  Matrix(const Sparsity& sp, const Scalar& val, bool dummy);
1296 
1298  Matrix(const Sparsity& sp, const std::vector<Scalar>& d, bool dummy);
1299 
1301  static Matrix<Scalar> _sym(const std::string& name, const Sparsity& sp);
1303 
1304  private:
1306  Sparsity sparsity_;
1307 
1309  std::vector<Scalar> nonzeros_;
1310 
1312  static casadi_int stream_precision_;
1313  static casadi_int stream_width_;
1314  static bool stream_scientific_;
1315 
1317  static std::default_random_engine rng_;
1318 
1319 #endif // SWIG
1320  };
1321 
1323  template<typename Scalar>
1324  template<typename A>
1325  std::vector<A> Matrix<Scalar>::get_nonzeros() const {
1326  std::vector<A> ret(nnz());
1327  auto r = ret.begin();
1328  for (auto&& e : nonzeros()) *r++ = static_cast<A>(e);
1329  return ret;
1330  }
1331 
1333  template<typename Scalar>
1334  template<typename A>
1335  Matrix<Scalar>::operator std::vector<A>() const {
1336  // Get sparsity pattern
1337  casadi_int size1 = this->size1(), size2 = this->size2();
1338  const casadi_int *colind = this->colind(), *row = this->row();
1339  // Copy the nonzeros
1340  auto it = nonzeros().begin();
1341  std::vector<A> ret(numel(), 0);
1342  for (casadi_int cc=0; cc<size2; ++cc) {
1343  for (casadi_int el=colind[cc]; el<colind[cc+1]; ++el) {
1344  ret[row[el] + cc*size1] = static_cast<A>(*it++);
1345  }
1346  }
1347  return ret;
1348  }
1349 
1350 } // namespace casadi
1351 
1352 #endif // CASADI_MATRIX_DECL_HPP
Helper class for Serialization.
Function object.
Definition: function.hpp:60
Matrix base class.
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:92
static Matrix< Scalar > rand(const Sparsity &sp)
Create a matrix with uniformly distributed random numbers.
Matrix(const Matrix< A > &x)
Create a matrix from another matrix with a different entry type.
static Matrix< Scalar > nan(const std::pair< casadi_int, casadi_int > &rc)
create a matrix with all nan
bool has_zeros() const
Check if the matrix has any zero entries which are not structural zeros.
static Matrix< Scalar > deserialize(const std::string &s)
Build Sparsity from serialization.
bool is_one() const
check if the matrix is 1 (note that false negative answers are possible)
void remove(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc)
Remove columns and rows.
bool is_regular() const
Checks if expression does not contain NaN or Inf.
Matrix(double val)
This constructor enables implicit type conversion from a numeric type.
Matrix< Scalar > T() const
Transpose the matrix.
static void set_max_depth(casadi_int eq_depth=1)
Set or reset the depth to which equalities are being checked for simplifications.
void resize(casadi_int nrow, casadi_int ncol)
Sparsity get_sparsity() const
Get an owning reference to the sparsity pattern.
static Matrix< Scalar > deserialize(std::istream &stream)
Build Sparsity from serialization.
casadi_int element_hash() const
Returns a number that is unique for a given symbolic scalar.
void erase(const std::vector< casadi_int > &rr, bool ind1=false)
Erase a submatrix (leaving structural zeros in its place)
void serialize(SerializingStream &s) const
Serialize an object.
bool is_commutative() const
Check whether a binary SX is commutative.
Dict info() const
bool is_symbolic() const
Check if symbolic (Dense)
Matrix(const Sparsity &sp)
Create a sparse matrix from a sparsity pattern.
static std::vector< Matrix< Scalar > > get_free(const Function &f)
Get free.
Matrix(casadi_int nrow, casadi_int ncol)
Create a sparse matrix with all structural zeros.
static casadi_int get_max_depth()
Get the depth to which equalities are being checked for simplifications.
Matrix< Scalar > operator+() const
bool is_smooth() const
Check if smooth.
static Matrix< Scalar > nan(const Sparsity &sp)
create a matrix with all nan
static Matrix< Scalar > triplet(const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d, casadi_int nrow, casadi_int ncol)
Construct a sparse matrix from triplet form.
bool is_minus_one() const
check if the matrix is -1 (note that false negative answers are possible)
void to_file(const std::string &filename, const std::string &format="") const
static Matrix< Scalar > triplet(const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d)
Construct a sparse matrix from triplet form.
void disp(std::ostream &stream, bool more=false) const
Print a representation of the object.
static Matrix< Scalar > rand(casadi_int nrow=1, casadi_int ncol=1)
Create a matrix with uniformly distributed random numbers.
bool is_eye() const
check if the matrix is an identity matrix (note that false negative answers
bool is_op(casadi_int op) const
Is it a certain operation.
void erase(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
Erase a submatrix (leaving structural zeros in its place)
static Matrix< Scalar > inf(casadi_int nrow=1, casadi_int ncol=1)
create a matrix with all inf
std::vector< Scalar > get_elements() const
Get all elements.
static Matrix< Scalar > triplet(const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d, const std::pair< casadi_int, casadi_int > &rc)
Construct a sparse matrix from triplet form.
Matrix(const std::vector< std::vector< double > > &m)
Dense matrix constructor with data given as vector of vectors.
static std::vector< Matrix< Scalar > > get_input(const Function &f)
Get function input.
Matrix< Scalar > dep(casadi_int ch=0) const
Get expressions of the children of the expression.
bool is_zero() const
check if the matrix is 0 (note that false negative answers are possible)
static Matrix< Scalar > inf(const Sparsity &sp)
create a matrix with all inf
std::string serialize() const
Serialize.
void enlarge(casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
Enlarge matrix.
static Matrix< Scalar > deserialize(DeserializingStream &s)
bool is_constant() const
Check if the matrix is constant (note that false negative answers are possible)
Matrix(const std::vector< A > &x)
Create an expression from a vector.
void reserve(casadi_int nnz)
void export_code(const std::string &lang, std::ostream &stream=casadi::uout(), const Dict &options=Dict()) const
Export matrix in specific language.
casadi_int op() const
Get operation type.
void reserve(casadi_int nnz, casadi_int ncol)
static Matrix< Scalar > eye(casadi_int n)
create an n-by-n identity matrix
bool is_valid_input() const
Check if matrix can be used to define function inputs.
Matrix(const Sparsity &sp, const Matrix< Scalar > &d)
Construct matrix with a given sparsity and nonzeros.
static Matrix< Scalar > rand(const std::pair< casadi_int, casadi_int > &rc)
Create a matrix with uniformly distributed random numbers.
bool is_integer() const
Check if the matrix is integer-valued.
Matrix< Scalar > operator-() const
casadi_int n_dep() const
Get the number of dependencies of a binary SXElem.
static Matrix< Scalar > nan(casadi_int nrow=1, casadi_int ncol=1)
create a matrix with all nan
static Matrix< Scalar > inf(const std::pair< casadi_int, casadi_int > &rc)
create a matrix with all inf
std::string get_str(bool more=false) const
Get string representation.
Matrix< Scalar > printme(const Matrix< Scalar > &y) const
bool is_leaf() const
Check if SX is a leaf of the SX graph.
std::string name() const
Get name (only if symbolic scalar)
std::vector< Scalar > get_nonzeros() const
Get all nonzeros.
static Matrix< double > from_file(const std::string &filename, const std::string &format_hint="")
Helper class for Serialization.
General sparsity class.
Definition: sparsity.hpp:99
friend Matrix< Scalar > mtaylor(const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order=1)
multivariate Taylor series expansion
friend Matrix< Scalar > cofactor(const Matrix< Scalar > &x, casadi_int i, casadi_int j)
Get the (i,j) cofactor matrix.
friend Matrix< Scalar > any(const Matrix< Scalar > &x)
Returns true only if any element in the matrix is true.
friend void ldl(const Matrix< Scalar > &A, Matrix< Scalar > &D, Matrix< Scalar > &LT, std::vector< casadi_int > &p, bool amd=true)
Sparse LDL^T factorization.
friend Matrix< Scalar > norm_inf_mul(const Matrix< Scalar > &x, const Matrix< Scalar > &y)
Inf-norm of a Matrix-Matrix product.
friend Matrix< Scalar > eig_symbolic(const Matrix< Scalar > &m)
Attempts to find the eigenvalues of a symbolic matrix.
friend Matrix< double > evalf(const Matrix< Scalar > &expr)
Evaluates the expression numerically.
friend Matrix< Scalar > chol(const Matrix< Scalar > &A)
Obtain a Cholesky factorisation of a matrix.
friend Matrix< Scalar > pw_lin(const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)
t a scalar variable (e.g. time)
friend Matrix< Scalar > ldl_solve(const Matrix< Scalar > &b, const Matrix< Scalar > &D, const Matrix< Scalar > &LT, const std::vector< casadi_int > &p)
Solve using a sparse LDL^T factorization.
friend void expand(const Matrix< Scalar > &ex, Matrix< Scalar > &weights, Matrix< Scalar > &terms)
Expand the expression as a weighted sum (with constant weights)
friend Matrix< Scalar > poly_coeff(const Matrix< Scalar > &f, const Matrix< Scalar > &x)
extracts polynomial coefficients from an expression
friend Matrix< Scalar > pw_const(const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)
Create a piecewise constant function.
friend Matrix< Scalar > taylor(const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order=1)
univariate Taylor series expansion
friend void qr_sparse(const Matrix< Scalar > &A, Matrix< Scalar > &V, Matrix< Scalar > &R, Matrix< Scalar > &beta, std::vector< casadi_int > &prinv, std::vector< casadi_int > &pc, bool amd=true)
Sparse direct QR factorization.
friend Matrix< Scalar > taylor(const Matrix< Scalar > &ex, const Matrix< Scalar > &x)
univariate Taylor series expansion
friend Matrix< Scalar > all(const Matrix< Scalar > &x)
Returns true only if every element in the matrix is true.
friend Matrix< Scalar > poly_roots(const Matrix< Scalar > &p)
Attempts to find the roots of a polynomial.
friend void qr(const Matrix< Scalar > &A, Matrix< Scalar > &Q, Matrix< Scalar > &R)
QR factorization using the modified Gram-Schmidt algorithm.
friend Matrix< Scalar > heaviside(const Matrix< Scalar > &x)
Heaviside function.
friend Matrix< Scalar > gauss_quadrature(const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Matrix< Scalar > &a, const Matrix< Scalar > &b, casadi_int order, const Matrix< Scalar > &w)
Integrate f from a to b using Gaussian quadrature with n points.
friend Matrix< Scalar > rectangle(const Matrix< Scalar > &x)
rectangle function
friend Matrix< Scalar > triangle(const Matrix< Scalar > &x)
triangle function
friend Matrix< Scalar > sparsify(const Matrix< Scalar > &A, double tol=0)
Make a matrix sparse by removing numerical zeros.
friend Matrix< Scalar > ramp(const Matrix< Scalar > &x)
ramp function
friend Matrix< Scalar > gauss_quadrature(const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Matrix< Scalar > &a, const Matrix< Scalar > &b, casadi_int order=5)
Integrate f from a to b using Gaussian quadrature with n points.
friend Matrix< Scalar > qr_solve(const Matrix< Scalar > &b, const Matrix< Scalar > &v, const Matrix< Scalar > &r, const Matrix< Scalar > &beta, const std::vector< casadi_int > &prinv, const std::vector< casadi_int > &pc, bool tr=false)
Solve using a sparse QR factorization.
friend Matrix< Scalar > adj(const Matrix< Scalar > &A)
Matrix adjoint.
friend Matrix< Scalar > minor(const Matrix< Scalar > &x, casadi_int i, casadi_int j)
Get the (i,j) minor matrix.
friend Matrix< Scalar > mtaylor(const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order, const std::vector< casadi_int > &order_contributions)
multivariate Taylor series expansion
The casadi namespace.
CASADI_EXPORT std::ostream & uout()
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.