mx.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_MX_HPP
27 #define CASADI_MX_HPP
28 #include "shared_object.hpp"
29 #include "matrix_fwd.hpp"
30 #include "sx_fwd.hpp"
31 #include "dm.hpp"
32 #include "generic_matrix.hpp"
33 #include "generic_expression.hpp"
34 #include "generic_type.hpp"
35 #include "printable.hpp"
36 #include <vector>
37 #ifdef CASADI_WITH_THREAD
38 #ifdef CASADI_WITH_THREAD_MINGW
39 #include <mingw.mutex.h>
40 #else // CASADI_WITH_THREAD_MINGW
41 #include <mutex>
42 #endif // CASADI_WITH_THREAD_MINGW
43 #endif //CASADI_WITH_THREAD
44 
45 namespace casadi {
46 
50  class MXNode;
51  class Function;
52  class SerializingStream;
53  class DeserializingStream;
54 
55 #ifndef SWIG
56  struct ConvexifyData {
57  std::vector<casadi_int> scc_offset, scc_mapping;
58  Sparsity scc_sp;
59  Sparsity Hrsp;
60  Sparsity Hsp;
62  casadi_int sz_iw;
63  casadi_int sz_w;
64  casadi_int *iw;
65  double* w;
66  };
67 #endif
68 
88  class CASADI_EXPORT MX :
89  public SWIG_IF_ELSE(GenericExpressionCommon, GenericExpression<MX>),
90  public SWIG_IF_ELSE(PrintableCommon, Printable<MX>),
91  public GenericMatrix<MX>,
92  public SharedObject {
93  public:
97  static std::string type_name() {return "MX";}
98 
102  MX();
103 
107  MX(casadi_int nrow, casadi_int ncol);
108 
109 #ifndef SWIG
113  explicit MX(const std::pair<casadi_int, casadi_int>& rc);
114 #endif // SWIG
115 
121  explicit MX(const Sparsity& sp);
122 
126  MX(const Sparsity& sp, const MX& val);
127 
131  MX(const Sparsity& sp, const std::string& fname);
132 
133 
139  MX(const Matrix<double>& val, const std::string& name);
140 
144  MX(double x);
145 
146 #ifndef SWIG
150  MX(const std::vector<double> &x);
151 #endif
152 
156  MX(const Matrix<double> &x);
157 
159 
162  ~MX();
164 
165 #ifndef SWIG
167 
170  static MX create(MXNode* node);
171 
175  static std::vector<MX> createMultipleOutput(MXNode* node);
177 
181  const Sparsity& sparsity() const;
182 
185  typedef MX ScalarType;
187 
189  typedef GenericMatrix<MX> B;
190 
192  using B::horzsplit;
193  using B::diagsplit;
194  using B::vertsplit;
195  using B::mtimes;
196  using B::repmat;
197 #endif // SWIG
198 
200  bool __nonzero__() const;
201 
205  Sparsity get_sparsity() const { return sparsity();}
206 
220  std::vector<MX> get_nonzeros() const;
221 
227  void erase(const std::vector<casadi_int>& rr, const std::vector<casadi_int>& cc,
228  bool ind1=false);
229 
235  void erase(const std::vector<casadi_int>& rr, bool ind1=false);
236 
242  void enlarge(casadi_int nrow, casadi_int ncol,
243  const std::vector<casadi_int>& rr, const std::vector<casadi_int>& cc,
244  bool ind1=false);
245 
246  MX operator-() const;
247  MX operator+() const { return *this; }
248 
252  MX inv() const;
253 
254 #ifndef SWIG
257 
260  MXNode* operator->();
261 
265  const MXNode* operator->() const;
268 #endif // SWIG
269 
273  MX dep(casadi_int ch=0) const;
274 
278  casadi_int n_out() const;
279 
283  MX get_output(casadi_int oind) const;
284 
288  casadi_int n_dep() const;
289 
291  std::string name() const;
292 
294  explicit operator double() const;
295 
297  explicit operator Matrix<double>() const;
298 
300  bool is_symbolic() const;
301 
303  bool is_constant() const;
304 
306  bool is_integer() const;
307 
311  bool is_doubled() const;
312 
314  bool is_call() const;
315 
318 
320  bool is_output() const;
321 
325  bool has_output() const;
326 
328  casadi_int which_output() const;
329 
331  bool is_op(casadi_int op) const;
332 
334  bool is_multiplication() const;
335 
337  bool is_commutative() const;
338 
340  bool is_norm() const;
341 
347  bool is_valid_input() const;
348 
352  casadi_int n_primitives() const;
353 
357  std::vector<MX> primitives() const;
358 
360 
363  std::vector<MX> split_primitives(const MX& x) const;
364  std::vector<SX> split_primitives(const SX& x) const;
365  std::vector<DM> split_primitives(const DM& x) const;
367 
369 
372  MX join_primitives(const std::vector<MX>& v) const;
373  SX join_primitives(const std::vector<SX>& v) const;
374  DM join_primitives(const std::vector<DM>& v) const;
376 
378 
386  bool has_duplicates() const;
387 
391  void reset_input() const;
393 
397  bool is_eye() const;
398 
402  bool is_zero() const;
403 
407  bool is_one() const;
408 
412  bool is_minus_one() const;
413 
417  bool is_half() const;
418 
422  bool is_value(double val) const;
423 
427  bool is_inf() const;
428 
432  bool is_minus_inf() const;
433 
437  bool is_transpose() const;
438 
440  bool is_regular() const;
441 
443  bool is_binary() const;
444 
448  bool is_nonnegative() const;
449 
451  bool is_unary() const;
452 
454  casadi_int op() const;
455 
457  Dict info() const;
458 
462  void serialize(SerializingStream& s) const;
463 
468 
471  casadi_int get_temp() const;
472 
474  void set_temp(casadi_int t) const;
476 
478 
481  static MX binary(casadi_int op, const MX &x, const MX &y,
482  bool unique_x=false, bool unique_y=false);
483  static MX unary(casadi_int op, const MX &x, bool unique=false);
485 
487 
490  static MX inf(const Sparsity& sp);
491  static MX inf(casadi_int nrow=1, casadi_int ncol=1);
492  static MX inf(const std::pair<casadi_int, casadi_int>& rc);
494 
496 
499  static MX nan(const Sparsity& sp);
500  static MX nan(casadi_int nrow=1, casadi_int ncol=1);
501  static MX nan(const std::pair<casadi_int, casadi_int>& rc);
503 
507  static MX eye(casadi_int n);
508 
509 #ifndef SWIG
511  MXNode* get() const;
512 #endif // SWIG
513 
515 
520  MX nzref(const Sparsity& sp, const std::vector<casadi_int>& nz) const;
522 
525  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr) const;
526  void get(MX& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& rr) const;
527  void get(MX& SWIG_OUTPUT(m), bool ind1, const Sparsity& sp) const;
528  void get(MX& SWIG_OUTPUT(m), bool ind1, const MX& rr) const;
529  void get(MX& SWIG_OUTPUT(m), bool ind1, const casadi_int rr) const {
530  get(m, ind1, Matrix<casadi_int>(rr));
531  }
533 
536  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr, const Slice& cc) const;
537  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr, const Matrix<casadi_int>& cc) const;
538  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr, casadi_int cc) const {
539  get(m, ind1, rr, Matrix<casadi_int>(cc));
540  }
541  void get(MX& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& rr, const Slice& cc) const;
542  void get(MX& SWIG_OUTPUT(m), bool ind1, casadi_int rr, const Slice& cc) const {
543  get(m, ind1, Matrix<casadi_int>(rr), cc);
544  }
545  void get(MX& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& rr,
546  const Matrix<casadi_int>& cc) const;
547  void get(MX& SWIG_OUTPUT(m), bool ind1, casadi_int rr,
548  casadi_int cc) const {
549  get(m, ind1, Matrix<casadi_int>(rr), Matrix<casadi_int>(cc));
550  }
551  void get(MX& SWIG_OUTPUT(m), bool ind1, const MX& rr, const Slice& cc) const;
552  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr, const MX& cc) const;
553  void get(MX& SWIG_OUTPUT(m), bool ind1, const MX& rr, const MX& cc) const;
555 
558  void set(const MX& m, bool ind1, const Slice& rr);
559  void set(const MX& m, bool ind1, const Matrix<casadi_int>& rr);
560  void set(const MX& m, bool ind1, const Sparsity& sp);
562 
565  void set(const MX& m, bool ind1, const Slice& rr, const Slice& cc);
566  void set(const MX& m, bool ind1, const Slice& rr, const Matrix<casadi_int>& cc);
567  void set(const MX& m, bool ind1, const Matrix<casadi_int>& rr, const Slice& cc);
568  void set(const MX& m, bool ind1, const Matrix<casadi_int>& rr, const Matrix<casadi_int>& cc);
570 
573  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const Slice& kk) const;
574  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& kk) const;
575  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const MX& kk) const;
576  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, casadi_int kk) const {
577  get_nz(m, ind1, Matrix<casadi_int>(kk));
578  }
579  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const MX& inner, const Slice& outer) const;
580  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const Slice& inner, const MX& outer) const;
581  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const MX& inner, const MX& outer) const;
583 
586  void set_nz(const MX& m, bool ind1, const Slice& kk);
587  void set_nz(const MX& m, bool ind1, const Matrix<casadi_int>& kk);
588  void set_nz(const MX& m, bool ind1, const MX& kk);
589  void set_nz(const MX& m, bool ind1, casadi_int kk) { set_nz(m, ind1, Matrix<casadi_int>(kk)); }
591 
593 
607  static MX einstein(const MX& A, const MX& B, const MX& C,
608  const std::vector<casadi_int>& dim_a, const std::vector<casadi_int>& dim_b,
609  const std::vector<casadi_int>& dim_c,
610  const std::vector<casadi_int>& a, const std::vector<casadi_int>& b,
611  const std::vector<casadi_int>& c);
612 
613  static MX einstein(const MX& A, const MX& B,
614  const std::vector<casadi_int>& dim_a, const std::vector<casadi_int>& dim_b,
615  const std::vector<casadi_int>& dim_c,
616  const std::vector<casadi_int>& a, const std::vector<casadi_int>& b,
617  const std::vector<casadi_int>& c);
619 
620 #ifndef SWIG
624  static bool is_equal(const MX& x, const MX& y, casadi_int depth=0);
625  static MX mmin(const MX &x);
626  static MX mmax(const MX &x);
628 
631  static MX horzcat(const std::vector<MX>& x);
632  static MX diagcat(const std::vector<MX>& x);
633  static MX vertcat(const std::vector<MX>& x);
634  static std::vector<MX> horzsplit(const MX& x, const std::vector<casadi_int>& offset);
635  static std::vector<MX> diagsplit(const MX& x, const std::vector<casadi_int>& offset1,
636  const std::vector<casadi_int>& offset2);
637  static std::vector<MX> vertsplit(const MX& x, const std::vector<casadi_int>& offset);
638  static MX blockcat(const std::vector< std::vector<MX > > &v);
639  static MX mtimes(const MX& x, const MX& y,
640  const std::string& blas = "reference");
641  static MX mac(const MX& x, const MX& y, const MX& z,
642  const std::string& blas = "reference");
643  static MX reshape(const MX& x, casadi_int nrow, casadi_int ncol);
644  static MX reshape(const MX& x, const Sparsity& sp);
645  static MX sparsity_cast(const MX& x, const Sparsity& sp);
646  static MX kron(const MX& x, const MX& b);
647  static MX kron_contract(const MX& m, const MX& x, bool inner);
648  static MX repmat(const MX& x, casadi_int n, casadi_int m=1);
650 
653  static MX linspace(const MX& a, const MX& b, casadi_int nsteps);
654  static MX jacobian(const MX& f, const MX& x, const Dict& opts = Dict());
655  static MX hessian(const MX& f, const MX& x, const Dict& opts = Dict());
656  static MX hessian(const MX& f, const MX& x, MX& g, const Dict& opts = Dict());
657  static std::vector<std::vector<MX> >
658  forward(const std::vector<MX> &ex,
659  const std::vector<MX> &arg,
660  const std::vector<std::vector<MX> > &v,
661  const Dict& opts = Dict());
662  static std::vector<std::vector<MX> >
663  reverse(const std::vector<MX> &ex,
664  const std::vector<MX> &arg,
665  const std::vector<std::vector<MX> > &v,
666  const Dict& opts = Dict());
667  static std::vector<bool> which_depends(const MX &expr, const MX &var,
668  casadi_int order=1, bool tr=false);
669  static Sparsity jacobian_sparsity(const MX& f, const MX& x);
670  static MX substitute(const MX& ex, const MX& v, const MX& vdef);
671  static std::vector<MX> substitute(const std::vector<MX> &ex,
672  const std::vector<MX> &v,
673  const std::vector<MX> &vdef);
674  static void substitute_inplace(const std::vector<MX>& v,
675  std::vector<MX>& vdef,
676  std::vector<MX>& ex, bool reverse);
677  static MX solve(const MX& a, const MX& b);
678  static MX solve(const MX& a, const MX& b, const std::string& lsolver,
679  const Dict& dict = Dict());
680  static MX inv_minor(const MX& A);
681  static MX inv_node(const MX& A);
682  static MX inv(const MX& A, const std::string& lsolver="qr", const Dict& dict = Dict());
683  static MX pinv(const MX& A, const std::string& lsolver="qr",
684  const Dict& dict = Dict());
685  static MX expm_const(const MX& A, const MX& t);
686  static MX expm(const MX& A);
687  static casadi_int n_nodes(const MX& x);
688  static std::string print_operator(const MX& x, const std::vector<std::string>& args);
689  static void extract(std::vector<MX>& ex, std::vector<MX>& v,
690  std::vector<MX>& vdef, const Dict& opts = Dict());
691  static void shared(std::vector<MX>& ex, std::vector<MX>& v,
692  std::vector<MX>& vdef, const std::string& v_prefix, const std::string& v_suffix);
693  static MX if_else(const MX& cond, const MX& if_true,
694  const MX& if_false, bool short_circuit=false);
695  static MX conditional(const MX& ind, const std::vector<MX> &x, const MX& x_default,
696  bool short_circuit=false);
697  static bool depends_on(const MX& x, const MX& arg);
698  static bool contains_all(const std::vector<MX>& v, const std::vector<MX> &n);
699  static bool contains_any(const std::vector<MX>& v, const std::vector<MX> &n);
700  static MX simplify(const MX& x);
701  static MX transform(const MX& x, const Dict& opts = Dict());
702  static MX transform(const MX& x,
703  const std::vector<std::vector<GenericType> >& passes, const Dict& opts = Dict());
704  static std::vector<MX> transform(const std::vector<MX>& x, const Dict& opts = Dict());
705  static std::vector<MX> transform(const std::vector<MX>& x,
706  const std::vector<std::vector<GenericType> >& passes, const Dict& opts = Dict());
707  static MX dot(const MX& x, const MX& y);
708  static MX mrdivide(const MX& a, const MX& b);
709  static MX mldivide(const MX& a, const MX& b);
710  static MX norm_2(const MX& x);
711  static MX norm_fro(const MX& x);
712  static MX norm_1(const MX& x);
713  static MX norm_inf(const MX& x);
714  static MX unite(const MX& A, const MX& B);
715  static MX trace(const MX& x);
716  static MX diag(const MX& x);
717  static MX sum2(const MX& x);
718  static MX sum1(const MX& x);
719  static MX polyval(const MX& p, const MX& x);
720  static MX det(const MX& x);
721  static MX det(const MX& x, const std::string& lsolver, const Dict& opts=Dict());
722  static std::vector<MX> symvar(const MX& x);
723  static MX nullspace(const MX& A);
724  static MX repsum(const MX& x, casadi_int n, casadi_int m=1);
725  static MX densify(const MX& x, const MX& val=0);
726  static MX _bilin(const MX& A, const MX& x, const MX& y);
727  static MX _rank1(const MX& A, const MX& alpha, const MX& x, const MX& y);
728  static MX project(const MX& x, const Sparsity& sp, bool intersect=false);
729  static MX cumsum(const MX &x, casadi_int axis=-1);
730  static MX _logsumexp(const MX& x);
731  static std::vector<MX> cse(const std::vector<MX>& e);
732  static void extract_parametric(const MX &expr, const MX& par,
733  MX& expr_ret, std::vector<MX>& symbols, std::vector<MX>& parametric, const Dict& opts);
734  static void separate_linear(const MX &expr,
735  const MX &sym_lin, const MX &sym_const,
736  MX& expr_const, MX& expr_lin, MX& expr_nonlin);
738 
741  static MX find(const MX& x);
742  static MX low(const MX& v, const MX& p, const Dict& options = Dict());
743  static MX graph_substitute(const MX& x, const std::vector<MX> &v,
744  const std::vector<MX> &vdef);
745  static MX graph_substitute(const MX& x, const std::vector<MX> &v,
746  const std::vector<MX> &vdef, bool& updated);
747  static std::vector<MX> graph_substitute(const std::vector<MX> &ex,
748  const std::vector<MX> &v,
749  const std::vector<MX> &vdef);
750  static std::vector<MX> graph_substitute(const std::vector<MX> &ex,
751  const std::vector<MX> &v,
752  const std::vector<MX> &vdef,
753  bool& updated);
754  static MX matrix_expand(const MX& e, const std::vector<MX> &boundary,
755  const Dict& options);
756  static std::vector<MX> matrix_expand(const std::vector<MX>& e,
757  const std::vector<MX>& boundary,
758  const Dict& options);
759  static MX lift(const MX& x, const MX& x_guess);
760  static DM evalf(const MX& m);
761  static MX bspline(const MX& x,
762  const DM& coeffs,
763  const std::vector< std::vector<double> >& knots,
764  const std::vector<casadi_int>& degree,
765  casadi_int m,
766  const Dict& opts = Dict());
767  static MX bspline(const MX& x, const MX& coeffs,
768  const std::vector< std::vector<double> >& knots,
769  const std::vector<casadi_int>& degree,
770  casadi_int m,
771  const Dict& opts = Dict());
772  static MX bspline(const MX& x, const MX& coeffs,
773  const std::vector<MX>& knots,
774  const std::vector<casadi_int>& degree,
775  casadi_int m,
776  const Dict& opts = Dict());
777  static MX convexify(const MX& H, const Dict& opts = Dict());
778  static MX stop_diff(const MX& expr, casadi_int order);
779  static MX stop_diff(const MX& expr, const MX& var, casadi_int order);
780  static std::vector<MX> difference(const std::vector<MX>& a, const std::vector<MX>& b);
783 
784 #endif // SWIG
785 
786  // Simplification with reference counting awareness
787  static bool simplify_ref_count(std::vector<MX>& arg,
788  std::vector<MX>& res,
789  const Dict& opts = Dict());
790 
791  // Simplification with constant folding
792  static bool simplify_const_folding(std::vector<MX>& arg,
793  std::vector<MX>& res,
794  const Dict& opts = Dict());
795 
796  // Simplification by combining like terms in linear combinations
797  static bool simplify_combine_terms(std::vector<MX>& arg,
798  std::vector<MX>& res,
799  const Dict& opts = Dict());
800 
801  static DM bspline_dual(const std::vector<double>& x,
802  const std::vector< std::vector<double> >& knots,
803  const std::vector<casadi_int>& degree,
804  const Dict& opts = Dict());
805 
813  static MX interpn_linear(const std::vector<MX>& x, const MX& v, const std::vector<MX>& xq,
814  const Dict& opts=Dict());
815 
816  MX printme(const MX& b) const;
817 
818 #if !defined(SWIG) || defined(DOXYGEN)
828  inline friend MX find(const MX& x) {
829  return MX::find(x);
830  }
831 
837  inline friend MX low(const MX& v, const MX& p, const Dict& options=Dict()) {
838  return MX::low(v, p, options);
839  }
840 
846  inline friend MX graph_substitute(const MX& ex, const std::vector<MX> &v,
847  const std::vector<MX> &vdef) {
848  return MX::graph_substitute(ex, v, vdef);
849  }
850 
851  inline friend MX graph_substitute(const MX& ex, const std::vector<MX> &v,
852  const std::vector<MX> &vdef, bool& updated) {
853  return MX::graph_substitute(ex, v, vdef, updated);
854  }
855 
862  inline friend std::vector<MX>
863  graph_substitute(const std::vector<MX> &ex,
864  const std::vector<MX> &v,
865  const std::vector<MX> &vdef) {
866  return MX::graph_substitute(ex, v, vdef);
867  }
868 
869  inline friend std::vector<MX>
870  graph_substitute(const std::vector<MX> &ex,
871  const std::vector<MX> &v,
872  const std::vector<MX> &vdef,
873  bool& updated) {
874  return MX::graph_substitute(ex, v, vdef, updated);
875  }
876 
883  inline friend MX
884  matrix_expand(const MX& e, const std::vector<MX> &boundary = std::vector<MX>(),
885  const Dict& options = Dict()) {
886  return MX::matrix_expand(e, boundary, options);
887  }
888 
895  inline friend std::vector<MX>
896  matrix_expand(const std::vector<MX>& e,
897  const std::vector<MX> &boundary = std::vector<MX>(),
898  const Dict& options = Dict()) {
899  return MX::matrix_expand(e, boundary, options);
900  }
901 
902 
903  inline friend MX bspline(const MX& x,
904  const DM& coeffs,
905  const std::vector< std::vector<double> >& knots,
906  const std::vector<casadi_int>& degree,
907  casadi_int m,
908  const Dict& opts = Dict()) {
909  return MX::bspline(x, coeffs, knots, degree, m, opts);
910  }
911 
912  inline friend MX bspline(const MX& x, const MX& coeffs,
913  const std::vector< std::vector<double> >& knots,
914  const std::vector<casadi_int>& degree,
915  casadi_int m,
916  const Dict& opts = Dict()) {
917  return MX::bspline(x, coeffs, knots, degree, m, opts);
918  }
919 
920  inline friend MX bspline(const MX& x, const MX& coeffs,
921  const std::vector<MX>& knots,
922  const std::vector<casadi_int>& degree,
923  casadi_int m,
924  const Dict& opts = Dict()) {
925  return MX::bspline(x, coeffs, knots, degree, m, opts);
926  }
927 
928  inline friend DM bspline_dual(const std::vector<double>& x,
929  const std::vector< std::vector<double> >& knots,
930  const std::vector<casadi_int>& degree,
931  const Dict& opts = Dict()) {
932  return MX::bspline_dual(x, knots, degree, opts);
933  }
934 
935  inline friend MX convexify(const MX& H,
936  const Dict& opts = Dict()) {
937  return MX::convexify(H, opts);
938  }
939 
945  inline friend MX lift(const MX& x, const MX& x_guess) {
946  return MX::lift(x, x_guess);
947  }
948 
952  inline friend MX inv_node(const MX& x) {
953  return MX::inv_node(x);
954  }
955 
961  inline friend DM evalf(const MX& expr) {
962  return MX::evalf(expr);
963  }
964 
968  inline friend MX stop_diff(const MX& expr, casadi_int order) {
969  return MX::stop_diff(expr, order);
970  }
971 
977  inline friend MX no_grad(const MX& expr) {
978  return MX::stop_diff(expr, 1);
979  }
980 
986  inline friend MX no_hess(const MX& expr) {
987  return MX::stop_diff(expr, 2);
988  }
989 
990 
994  inline friend MX stop_diff(const MX& expr, const MX& var, casadi_int order) {
995  return MX::stop_diff(expr, var, order);
996  }
997 
999  inline friend std::vector<MX> difference(const std::vector<MX>& a, const std::vector<MX>& b) {
1000  return MX::difference(a, b);
1001  }
1002 
1004 #endif // SWIG
1005 
1011  MX attachAssert(const MX& y, const std::string& fail_message="") const;
1012 
1018  MX monitor(const std::string& comment) const;
1019 
1027  MX dump(const std::string& base_filename, const Dict& opts=Dict()) const;
1028 
1031 
1033  MX T() const;
1034 
1039 
1043  static void set_max_depth(casadi_int eq_depth=1);
1044 
1048  static casadi_int get_max_depth();
1049 
1051  static bool test_cast(const SharedObjectInternal* ptr);
1052 
1056  static std::vector<MX> get_input(const Function& f);
1057 
1061  static std::vector<MX> get_free(const Function& f);
1062 
1064  typedef std::map<std::string, MX> MXDict;
1065 
1069  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& SWIG_OUTPUT(res),
1070  const std::vector<bool>& unique=std::vector<bool>()) const;
1071 
1072 #ifndef SWIG
1074 
1077  void ad_forward(const std::vector<std::vector<MX> >& fseed,
1078  std::vector<std::vector<MX> >& fsens) const;
1079  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
1080  std::vector<std::vector<MX> >& asens) const;
1082 
1084  MX(const Sparsity& sp, double val, bool dummy);
1085 
1086  // Create matrix symbolic primitive
1087  static MX _sym(const std::string& name, const Sparsity& sp);
1088 
1089 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
1090  static std::mutex& get_mutex_temp() { return mutex_temp; }
1091  static std::mutex mutex_temp;
1092 #endif //CASADI_WITH_THREADSAFE_SYMBOLICS
1093  private:
1094 
1096  MX(MXNode* node, bool dummy1, bool dummy2, bool dummy3, bool dummy4);
1097 
1098  // Depth when checking equalities
1099  static casadi_int eq_depth_;
1100 
1101 #endif // SWIG
1102  };
1103 
1104 
1107  typedef std::vector<MX> MXVector;
1108  typedef std::initializer_list<MX> MXIList;
1109  typedef std::vector<MXVector> MXVectorVector;
1110  typedef std::map<std::string, MX> MXDict;
1112 
1121  inline MX kron_contract(const MX& m, const MX& x, bool inner) {
1122  return MX::kron_contract(m, x, inner);
1123  }
1124 
1125 } // namespace casadi
1126 
1127 #endif // CASADI_MX_HPP
Helper class for Serialization.
Function object.
Definition: function.hpp:60
Matrix base class.
Node class for MX objects.
Definition: mx_node.hpp:51
MX - Matrix expression.
Definition: mx.hpp:92
void get_nz(MX &m, bool ind1, casadi_int kk) const
Definition: mx.hpp:576
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 std::vector< MX > get_free(const Function &f)
Get free variables.
static bool simplify_const_folding(std::vector< MX > &arg, std::vector< MX > &res, const Dict &opts=Dict())
static bool simplify_ref_count(std::vector< MX > &arg, std::vector< MX > &res, const Dict &opts=Dict())
bool is_multiplication() const
Check if multiplication.
bool is_minus_one() const
check if minus one (note that false negative answers are possible)
bool is_valid_input() const
Check if matrix can be used to define function inputs.
static bool test_cast(const SharedObjectInternal *ptr)
Check if a particular cast is allowed.
bool is_eye() const
check if identity
void get_nz(MX &m, bool ind1, const Slice &inner, const MX &outer) const
bool is_value(double val) const
check if a certain value (note that false negative answers are possible)
static casadi_int get_max_depth()
Get the depth to which equalities are being checked for simplifications.
static MX binary(casadi_int op, const MX &x, const MX &y, bool unique_x=false, bool unique_y=false)
Create nodes by their ID.
static MX nan(const Sparsity &sp)
create a matrix with all nan
std::vector< MX > split_primitives(const MX &x) const
Split up an expression along symbolic primitives.
MX operator-() const
bool is_output() const
Check if evaluation output.
casadi_int n_out() const
Number of outputs.
void get(MX &m, bool ind1, const Sparsity &sp) const
void erase(const std::vector< casadi_int > &rr, bool ind1=false)
Erase a submatrix (leaving structural zeros in its place)
static MX deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
static MX eye(casadi_int n)
Identity matrix.
casadi_int n_dep() const
Get the number of dependencies of a binary SXElem.
void get(MX &m, bool ind1, const MX &rr, const MX &cc) const
casadi_int n_primitives() const
Get the number of primitives for MXFunction inputs/outputs.
void set(const MX &m, bool ind1, const Matrix< casadi_int > &rr)
bool __nonzero__() const
Returns the truth value of an MX expression.
bool is_half() const
check if 0.5 (note that false negative answers are possible)
void get(MX &m, bool ind1, const MX &rr) const
static MX inf(casadi_int nrow=1, casadi_int ncol=1)
create a matrix with all inf
bool is_call() const
Check if evaluation.
std::string name() const
Get the name.
bool has_output() const
Check if a multiple output node.
bool is_integer() const
Check if integer.
void get(MX &m, bool ind1, const MX &rr, const Slice &cc) const
MX(const Sparsity &sp, const std::string &fname)
Construct matrix with a given sparsity and a file with nonzeros.
bool is_inf() const
check if inf (note that false negative answers are possible)
void set(const MX &m, bool ind1, const Slice &rr, const Slice &cc)
bool is_constant() const
Check if constant.
Sparsity get_sparsity() const
Get an owning reference to the sparsity pattern.
Definition: mx.hpp:205
MX operator+() const
Definition: mx.hpp:247
static MX nan(const std::pair< casadi_int, casadi_int > &rc)
create a matrix with all nan
MX dump(const std::string &base_filename, const Dict &opts=Dict()) const
Dump an expression.
void reset_dump_count()
Reset the dump counter.
bool is_commutative() const
Check if commutative operation.
MX(const Sparsity &sp, const MX &val)
Construct matrix with a given sparsity and nonzeros.
static MX einstein(const MX &A, const MX &B, const MX &C, const std::vector< casadi_int > &dim_a, const std::vector< casadi_int > &dim_b, const std::vector< casadi_int > &dim_c, const std::vector< casadi_int > &a, const std::vector< casadi_int > &b, const std::vector< casadi_int > &c)
Computes an einstein dense tensor contraction.
void set_nz(const MX &m, bool ind1, casadi_int kk)
Definition: mx.hpp:589
void set_nz(const MX &m, bool ind1, const MX &kk)
void get(MX &m, bool ind1, const casadi_int rr) const
Definition: mx.hpp:529
void set(const MX &m, bool ind1, const Sparsity &sp)
void get_nz(MX &m, bool ind1, const MX &inner, const MX &outer) const
void get_nz(MX &m, bool ind1, const MX &kk) const
static void set_max_depth(casadi_int eq_depth=1)
Set or reset the depth to which equalities are being checked for simplifications.
DM join_primitives(const std::vector< DM > &v) const
Join an expression along symbolic primitives.
static MX unary(casadi_int op, const MX &x, bool unique=false)
Create nodes by their ID.
MX T() const
Transpose the matrix.
void get(MX &m, bool ind1, const Slice &rr, const Matrix< casadi_int > &cc) const
void get(MX &m, bool ind1, const Slice &rr, const MX &cc) const
void get(MX &m, bool ind1, casadi_int rr, casadi_int cc) const
Definition: mx.hpp:547
MX(casadi_int nrow, casadi_int ncol)
Create a sparse matrix with all structural zeros.
void get(MX &m, bool ind1, const Slice &rr, casadi_int cc) const
Definition: mx.hpp:538
Function which_function() const
Get function - only valid when is_call() is true.
void get(MX &m, bool ind1, const Slice &rr, const Slice &cc) const
void get_nz(MX &m, bool ind1, const MX &inner, const Slice &outer) const
static MX inf(const std::pair< casadi_int, casadi_int > &rc)
create a matrix with all inf
void get(MX &m, bool ind1, const Matrix< casadi_int > &rr, const Slice &cc) const
MX(double x)
Create scalar constant (also implicit type conversion)
MX(const Sparsity &sp)
Create a sparse matrix from a sparsity pattern.
static std::string type_name()
Get type name.
Definition: mx.hpp:97
bool is_op(casadi_int op) const
Is it a certain operation.
void get(MX &m, bool ind1, const Matrix< casadi_int > &rr) const
bool is_minus_inf() const
check if -inf (note that false negative answers are possible)
bool is_norm() const
Check if norm.
void set_nz(const MX &m, bool ind1, const Slice &kk)
static bool simplify_combine_terms(std::vector< MX > &arg, std::vector< MX > &res, const Dict &opts=Dict())
MX()
Default constructor.
bool is_regular() const
Checks if expression does not contain NaN or Inf.
MX(const Matrix< double > &val, const std::string &name)
Construct matrix with a given sparsity and nonzeros,.
casadi_int which_output() const
Get the index of evaluation output - only valid when is_output() is true.
void serialize(SerializingStream &s) const
Serialize an object.
static MX nan(casadi_int nrow=1, casadi_int ncol=1)
create a matrix with all nan
MX inv() const
Element-wise inverse.
void set(const MX &m, bool ind1, const Slice &rr, const Matrix< casadi_int > &cc)
SX join_primitives(const std::vector< SX > &v) const
Join an expression along symbolic primitives.
MX attachAssert(const MX &y, const std::string &fail_message="") const
returns itself, but with an assertion attached
Dict info() const
std::map< std::string, MX > MXDict
Readability typedef.
Definition: mx.hpp:1064
void set(const MX &m, bool ind1, const Slice &rr)
std::vector< MX > primitives() const
Get primitives.
bool is_one() const
check if one (note that false negative answers are possible)
void get_nz(MX &m, bool ind1, const Matrix< casadi_int > &kk) const
std::vector< DM > split_primitives(const DM &x) const
Split up an expression along symbolic primitives.
void get(MX &m, bool ind1, const Slice &rr) const
void set(const MX &m, bool ind1, const Matrix< casadi_int > &rr, const Matrix< casadi_int > &cc)
std::vector< MX > get_nonzeros() const
Get nonzeros as list of scalar MXes.
MX join_primitives(const std::vector< MX > &v) const
Join an expression along symbolic primitives.
void get(MX &m, bool ind1, const Matrix< casadi_int > &rr, const Matrix< casadi_int > &cc) const
MX(const Matrix< double > &x)
Create sparse matrix constant (also implicit type conversion)
MX dep(casadi_int ch=0) const
Get the nth dependency as MX.
void set(const MX &m, bool ind1, const Matrix< casadi_int > &rr, const Slice &cc)
MX monitor(const std::string &comment) const
Monitor an expression.
Matrix< casadi_int > mapping() const
Get an IM representation of a GetNonzeros or SetNonzeros node.
static MX interpn_linear(const std::vector< MX > &x, const MX &v, const std::vector< MX > &xq, const Dict &opts=Dict())
Low-level access to inlined linear interpolation.
bool is_binary() const
Is binary operation.
void set_nz(const MX &m, bool ind1, const Matrix< casadi_int > &kk)
void get(MX &m, bool ind1, casadi_int rr, const Slice &cc) const
Definition: mx.hpp:542
std::vector< SX > split_primitives(const SX &x) const
Split up an expression along symbolic primitives.
bool is_unary() const
Is unary operation.
static MX einstein(const MX &A, const MX &B, const std::vector< casadi_int > &dim_a, const std::vector< casadi_int > &dim_b, const std::vector< casadi_int > &dim_c, const std::vector< casadi_int > &a, const std::vector< casadi_int > &b, const std::vector< casadi_int > &c)
Computes an einstein dense tensor contraction.
bool is_zero() const
check if zero (note that false negative answers are possible)
bool is_transpose() const
Is the expression a transpose?
void get_nz(MX &m, bool ind1, const Slice &kk) const
bool is_doubled() const
Check if the node is the sum of two equal expressions.
bool is_symbolic() const
Check if symbolic.
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.
MX printme(const MX &b) const
static std::vector< MX > get_input(const Function &f)
Get function inputs.
MX get_output(casadi_int oind) const
Get an output.
casadi_int op() const
Get operation type.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique=std::vector< bool >()) const
Evaluate the MX node with new symbolic dependencies.
bool is_nonnegative() const
Check if a value is always nonnegative (false negatives are allowed)
static DM bspline_dual(const std::vector< double > &x, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, const Dict &opts=Dict())
static MX inf(const Sparsity &sp)
create a matrix with all inf
Helper class for Serialization.
GenericShared implements a reference counting framework similar for efficient and.
Class representing a Slice.
Definition: slice.hpp:48
General sparsity class.
Definition: sparsity.hpp:106
friend MX stop_diff(const MX &expr, const MX &var, casadi_int order)
Stop derivatives of an expression wrt to a select set of symbolic variables.
Definition: mx.hpp:994
friend MX no_hess(const MX &expr)
Stop second derivatives of an expression wrt to all its symbolic variables.
Definition: mx.hpp:986
friend MX bspline(const MX &x, const MX &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
Definition: mx.hpp:912
friend MX low(const MX &v, const MX &p, const Dict &options=Dict())
Find first nonzero.
Definition: mx.hpp:837
friend std::vector< MX > matrix_expand(const std::vector< MX > &e, const std::vector< MX > &boundary=std::vector< MX >(), const Dict &options=Dict())
Expand MX graph to SXFunction call.
Definition: mx.hpp:896
friend MX convexify(const MX &H, const Dict &opts=Dict())
Definition: mx.hpp:935
friend MX inv_node(const MX &x)
Inverse node.
Definition: mx.hpp:952
friend std::vector< MX > difference(const std::vector< MX > &a, const std::vector< MX > &b)
Definition: mx.hpp:999
friend MX find(const MX &x)
Find first nonzero, returned as row index.
Definition: mx.hpp:828
friend MX no_grad(const MX &expr)
Stop first derivatives of an expression wrt to all its symbolic variables.
Definition: mx.hpp:977
friend MX bspline(const MX &x, const MX &coeffs, const std::vector< MX > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
Definition: mx.hpp:920
friend MX graph_substitute(const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
Definition: mx.hpp:851
friend MX bspline(const MX &x, const DM &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
Definition: mx.hpp:903
friend std::vector< MX > graph_substitute(const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
Substitute multiple expressions in graph.
Definition: mx.hpp:863
friend std::vector< MX > graph_substitute(const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
Definition: mx.hpp:870
friend MX matrix_expand(const MX &e, const std::vector< MX > &boundary=std::vector< MX >(), const Dict &options=Dict())
Expand MX graph to SXFunction call.
Definition: mx.hpp:884
friend MX lift(const MX &x, const MX &x_guess)
Lift the expression.
Definition: mx.hpp:945
friend MX stop_diff(const MX &expr, casadi_int order)
Stop derivatives of an expression wrt to all its symbolic variables.
Definition: mx.hpp:968
friend DM bspline_dual(const std::vector< double > &x, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, const Dict &opts=Dict())
Definition: mx.hpp:928
friend DM evalf(const MX &expr)
Evaluates the expression numerically.
Definition: mx.hpp:961
friend MX graph_substitute(const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
Substitute single expression in graph.
Definition: mx.hpp:846
The casadi namespace.
Definition: archiver.hpp:32
std::map< std::string, MX > MXDict
Definition: mx.hpp:1110
std::vector< MX > MXVector
Definition: mx.hpp:1107
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::vector< MXVector > MXVectorVector
Definition: mx.hpp:1109
MX kron_contract(const MX &m, const MX &x, bool inner)
Kronecker contraction.
Definition: mx.hpp:1121
std::initializer_list< MX > MXIList
Definition: mx.hpp:1108