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;
62  casadi_int sz_iw;
63  casadi_int sz_w;
64  casadi_int *iw;
65  double* w;
66  };
67 #endif
68 
141  class CASADI_EXPORT MX :
142  public SWIG_IF_ELSE(GenericExpressionCommon, GenericExpression<MX>),
143  public SWIG_IF_ELSE(PrintableCommon, Printable<MX>),
144  public GenericMatrix<MX>,
145  public SharedObject {
146  public:
150  static std::string type_name() {return "MX";}
151 
180  MX();
181 
226  MX(casadi_int nrow, casadi_int ncol);
227 
228 #ifndef SWIG
232  explicit MX(const std::pair<casadi_int, casadi_int>& rc);
233 #endif // SWIG
234 
274  explicit MX(const Sparsity& sp);
275 
340  MX(const Sparsity& sp, const MX& val);
341 
380  MX(const Sparsity& sp, const std::string& fname);
381 
382 
423  MX(const Matrix<double>& val, const std::string& name);
424 
455  MX(double x);
456 
457 #ifndef SWIG
461  MX(const std::vector<double> &x);
462 #endif
463 
514  MX(const Matrix<double> &x);
515 
517 
520  ~MX();
522 
523 #ifndef SWIG
525 
528  static MX create(MXNode* node);
529 
533  static std::vector<MX> createMultipleOutput(MXNode* node);
535 
569  const Sparsity& sparsity() const;
570 
573  typedef MX ScalarType;
575 
578 
580  using B::horzsplit;
581  using B::diagsplit;
582  using B::vertsplit;
583  using B::mtimes;
584  using B::repmat;
585 #endif // SWIG
586 
618  bool __nonzero__() const;
619 
623  Sparsity get_sparsity() const { return sparsity();}
624 
635  std::vector<MX> get_nonzeros() const;
636 
678  void erase(const std::vector<casadi_int>& rr, const std::vector<casadi_int>& cc,
679  bool ind1=false);
680 
714  void erase(const std::vector<casadi_int>& rr, bool ind1=false);
715 
761  void enlarge(casadi_int nrow, casadi_int ncol,
762  const std::vector<casadi_int>& rr, const std::vector<casadi_int>& cc,
763  bool ind1=false);
764 
795  MX operator-() const;
796 
798  MX operator+() const { return *this; }
799 
819  MX inv() const;
820 
821 #ifndef SWIG
824 
827  MXNode* operator->();
828 
832  const MXNode* operator->() const;
835 #endif // SWIG
836 
884  MX dep(casadi_int ch=0) const;
885 
933  casadi_int n_out() const;
934 
987  MX get_output(casadi_int oind) const;
988 
1036  casadi_int n_dep() const;
1037 
1084  std::string name() const;
1085 
1127  explicit operator double() const;
1128 
1178  explicit operator Matrix<double>() const;
1179 
1221  bool is_symbolic() const;
1222 
1262  bool is_constant() const;
1263 
1292  bool is_integer() const;
1293 
1328  bool is_doubled() const;
1329 
1382  bool is_call() const;
1383 
1439  Function which_function() const;
1440 
1493  bool is_output() const;
1494 
1539  bool has_output() const;
1540 
1582  casadi_int which_output() const;
1583 
1627  bool is_op(casadi_int op) const;
1628 
1660  bool is_multiplication() const;
1661 
1696  bool is_commutative() const;
1697 
1728  bool is_norm() const;
1729 
1776  bool is_valid_input() const;
1777 
1813  casadi_int n_primitives() const;
1814 
1864  std::vector<MX> primitives() const;
1865 
1867 
1920  std::vector<MX> split_primitives(const MX& x) const;
1921  std::vector<SX> split_primitives(const SX& x) const;
1922  std::vector<DM> split_primitives(const DM& x) const;
1924 
1926 
1976  MX join_primitives(const std::vector<MX>& v) const;
1977  SX join_primitives(const std::vector<SX>& v) const;
1978  DM join_primitives(const std::vector<DM>& v) const;
1980 
1982 
2027  bool has_duplicates() const;
2028 
2032  void reset_input() const;
2034 
2065  bool is_eye() const;
2066 
2107  bool is_zero() const;
2108 
2139  bool is_one() const;
2140 
2168  bool is_minus_one() const;
2169 
2173  bool is_half() const;
2174 
2212  bool is_value(double val) const;
2213 
2242  bool is_inf() const;
2243 
2270  bool is_minus_inf() const;
2271 
2306  bool is_transpose() const;
2307 
2350  bool is_regular() const;
2351 
2391  bool is_binary() const;
2392 
2434  bool is_nonnegative() const;
2435 
2473  bool is_unary() const;
2474 
2517  casadi_int op() const;
2518 
2559  Dict info() const;
2560 
2602  void serialize(SerializingStream& s) const;
2603 
2609  static MX deserialize(DeserializingStream& s);
2610 
2613  casadi_int get_temp() const;
2614 
2616  void set_temp(casadi_int t) const;
2618 
2620 
2679  static MX binary(casadi_int op, const MX &x, const MX &y,
2680  bool unique_x=false, bool unique_y=false);
2681  static MX unary(casadi_int op, const MX &x, bool unique=false);
2683 
2685 
2715  static MX inf(const Sparsity& sp);
2716  static MX inf(casadi_int nrow=1, casadi_int ncol=1);
2717  static MX inf(const std::pair<casadi_int, casadi_int>& rc);
2719 
2721 
2750  static MX nan(const Sparsity& sp);
2751  static MX nan(casadi_int nrow=1, casadi_int ncol=1);
2752  static MX nan(const std::pair<casadi_int, casadi_int>& rc);
2754 
2780  static MX eye(casadi_int n);
2781 
2782 #ifndef SWIG
2784  MXNode* get() const;
2785 #endif // SWIG
2786 
2788 
2793  MX nzref(const Sparsity& sp, const std::vector<casadi_int>& nz) const;
2795 
2797 
2887  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr) const;
2888  void get(MX& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& rr) const;
2889  void get(MX& SWIG_OUTPUT(m), bool ind1, const Sparsity& sp) const;
2890  void get(MX& SWIG_OUTPUT(m), bool ind1, const MX& rr) const;
2891  void get(MX& SWIG_OUTPUT(m), bool ind1, const casadi_int rr) const {
2892  get(m, ind1, Matrix<casadi_int>(rr));
2893  }
2895 
2897 
2951  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr, const Slice& cc) const;
2952  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr, const Matrix<casadi_int>& cc) const;
2953  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr, casadi_int cc) const {
2954  get(m, ind1, rr, Matrix<casadi_int>(cc));
2955  }
2956  void get(MX& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& rr, const Slice& cc) const;
2957  void get(MX& SWIG_OUTPUT(m), bool ind1, casadi_int rr, const Slice& cc) const {
2958  get(m, ind1, Matrix<casadi_int>(rr), cc);
2959  }
2960  void get(MX& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& rr,
2961  const Matrix<casadi_int>& cc) const;
2962  void get(MX& SWIG_OUTPUT(m), bool ind1, casadi_int rr,
2963  casadi_int cc) const {
2964  get(m, ind1, Matrix<casadi_int>(rr), Matrix<casadi_int>(cc));
2965  }
2966  void get(MX& SWIG_OUTPUT(m), bool ind1, const MX& rr, const Slice& cc) const;
2967  void get(MX& SWIG_OUTPUT(m), bool ind1, const Slice& rr, const MX& cc) const;
2968  void get(MX& SWIG_OUTPUT(m), bool ind1, const MX& rr, const MX& cc) const;
2970 
2972 
3049  void set(const MX& m, bool ind1, const Slice& rr);
3050  void set(const MX& m, bool ind1, const Matrix<casadi_int>& rr);
3051  void set(const MX& m, bool ind1, const Sparsity& sp);
3053 
3055 
3112  void set(const MX& m, bool ind1, const Slice& rr, const Slice& cc);
3113  void set(const MX& m, bool ind1, const Slice& rr, const Matrix<casadi_int>& cc);
3114  void set(const MX& m, bool ind1, const Matrix<casadi_int>& rr, const Slice& cc);
3115  void set(const MX& m, bool ind1, const Matrix<casadi_int>& rr, const Matrix<casadi_int>& cc);
3117 
3119 
3172  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const Slice& kk) const;
3173  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const Matrix<casadi_int>& kk) const;
3174  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const MX& kk) const;
3175  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, casadi_int kk) const {
3176  get_nz(m, ind1, Matrix<casadi_int>(kk));
3177  }
3178  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const MX& inner, const Slice& outer) const;
3179  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const Slice& inner, const MX& outer) const;
3180  void get_nz(MX& SWIG_OUTPUT(m), bool ind1, const MX& inner, const MX& outer) const;
3182 
3184 
3235  void set_nz(const MX& m, bool ind1, const Slice& kk);
3236  void set_nz(const MX& m, bool ind1, const Matrix<casadi_int>& kk);
3237  void set_nz(const MX& m, bool ind1, const MX& kk);
3238  void set_nz(const MX& m, bool ind1, casadi_int kk) { set_nz(m, ind1, Matrix<casadi_int>(kk)); }
3240 
3242 
3310  static MX einstein(const MX& A, const MX& B, const MX& C,
3311  const std::vector<casadi_int>& dim_a, const std::vector<casadi_int>& dim_b,
3312  const std::vector<casadi_int>& dim_c,
3313  const std::vector<casadi_int>& a, const std::vector<casadi_int>& b,
3314  const std::vector<casadi_int>& c);
3315 
3316  static MX einstein(const MX& A, const MX& B,
3317  const std::vector<casadi_int>& dim_a, const std::vector<casadi_int>& dim_b,
3318  const std::vector<casadi_int>& dim_c,
3319  const std::vector<casadi_int>& a, const std::vector<casadi_int>& b,
3320  const std::vector<casadi_int>& c);
3322 
3323 #ifndef SWIG
3327  static bool is_equal(const MX& x, const MX& y, casadi_int depth=0);
3328  static MX mmin(const MX &x);
3329  static MX mmax(const MX &x);
3331 
3334  static MX horzcat(const std::vector<MX>& x);
3335  static MX diagcat(const std::vector<MX>& x);
3336  static MX vertcat(const std::vector<MX>& x);
3337  static std::vector<MX> horzsplit(const MX& x, const std::vector<casadi_int>& offset);
3338  static std::vector<MX> diagsplit(const MX& x, const std::vector<casadi_int>& offset1,
3339  const std::vector<casadi_int>& offset2);
3340  static std::vector<MX> vertsplit(const MX& x, const std::vector<casadi_int>& offset);
3341  static MX blockcat(const std::vector< std::vector<MX > > &v);
3342  static MX mtimes(const MX& x, const MX& y,
3343  const std::string& blas = "reference");
3344  static MX mac(const MX& x, const MX& y, const MX& z,
3345  const std::string& blas = "reference");
3346  static MX reshape(const MX& x, casadi_int nrow, casadi_int ncol);
3347  static MX reshape(const MX& x, const Sparsity& sp);
3348  static MX sparsity_cast(const MX& x, const Sparsity& sp);
3349  static MX kron(const MX& x, const MX& b);
3350  static MX kron_contract(const MX& m, const MX& x, bool inner);
3351  static MX repmat(const MX& x, casadi_int n, casadi_int m=1);
3353 
3356  static MX linspace(const MX& a, const MX& b, casadi_int nsteps);
3357  static MX jacobian(const MX& f, const MX& x, const Dict& opts = Dict());
3358  static MX hessian(const MX& f, const MX& x, const Dict& opts = Dict());
3359  static MX hessian(const MX& f, const MX& x, MX& g, const Dict& opts = Dict());
3360  static std::vector<std::vector<MX> >
3361  forward(const std::vector<MX> &ex,
3362  const std::vector<MX> &arg,
3363  const std::vector<std::vector<MX> > &v,
3364  const Dict& opts = Dict());
3365  static std::vector<std::vector<MX> >
3366  reverse(const std::vector<MX> &ex,
3367  const std::vector<MX> &arg,
3368  const std::vector<std::vector<MX> > &v,
3369  const Dict& opts = Dict());
3370  static std::vector<bool> which_depends(const MX &expr, const MX &var,
3371  casadi_int order=1, bool tr=false);
3372  static Sparsity jacobian_sparsity(const MX& f, const MX& x);
3373  static MX substitute(const MX& ex, const MX& v, const MX& vdef);
3374  static std::vector<MX> substitute(const std::vector<MX> &ex,
3375  const std::vector<MX> &v,
3376  const std::vector<MX> &vdef);
3377  static void substitute_inplace(const std::vector<MX>& v,
3378  std::vector<MX>& vdef,
3379  std::vector<MX>& ex, bool reverse);
3380  static MX solve(const MX& a, const MX& b);
3381  static MX solve(const MX& a, const MX& b, const std::string& lsolver,
3382  const Dict& dict = Dict());
3383  static MX inv_minor(const MX& A);
3384  static MX inv_node(const MX& A);
3385  static MX inv(const MX& A, const std::string& lsolver="qr", const Dict& dict = Dict());
3386  static MX pinv(const MX& A, const std::string& lsolver="qr",
3387  const Dict& dict = Dict());
3388  static MX expm_const(const MX& A, const MX& t);
3389  static MX expm(const MX& A);
3390  static casadi_int n_nodes(const MX& x);
3391  static std::string print_operator(const MX& x, const std::vector<std::string>& args);
3392  static void extract(std::vector<MX>& ex, std::vector<MX>& v,
3393  std::vector<MX>& vdef, const Dict& opts = Dict());
3394  static void shared(std::vector<MX>& ex, std::vector<MX>& v,
3395  std::vector<MX>& vdef, const std::string& v_prefix, const std::string& v_suffix);
3396  static MX if_else(const MX& cond, const MX& if_true,
3397  const MX& if_false, bool short_circuit=false);
3398  static MX conditional(const MX& ind, const std::vector<MX> &x, const MX& x_default,
3399  bool short_circuit=false);
3400  static bool depends_on(const MX& x, const MX& arg);
3401  static bool contains_all(const std::vector<MX>& v, const std::vector<MX> &n);
3402  static bool contains_any(const std::vector<MX>& v, const std::vector<MX> &n);
3403  static MX simplify(const MX& x);
3404  static MX transform(const MX& x, const Dict& opts = Dict());
3405  static MX transform(const MX& x,
3406  const std::vector<std::vector<GenericType> >& passes, const Dict& opts = Dict());
3407  static std::vector<MX> transform(const std::vector<MX>& x, const Dict& opts = Dict());
3408  static std::vector<MX> transform(const std::vector<MX>& x,
3409  const std::vector<std::vector<GenericType> >& passes, const Dict& opts = Dict());
3410  static MX dot(const MX& x, const MX& y);
3411  static MX mrdivide(const MX& a, const MX& b);
3412  static MX mldivide(const MX& a, const MX& b);
3413  static MX norm_2(const MX& x);
3414  static MX norm_fro(const MX& x);
3415  static MX norm_1(const MX& x);
3416  static MX norm_inf(const MX& x);
3417  static MX unite(const MX& A, const MX& B);
3418  static MX trace(const MX& x);
3419  static MX diag(const MX& x);
3420  static MX sum2(const MX& x);
3421  static MX sum1(const MX& x);
3422  static MX polyval(const MX& p, const MX& x);
3423  static MX det(const MX& x);
3424  static MX det(const MX& x, const std::string& lsolver, const Dict& opts=Dict());
3425  static std::vector<MX> symvar(const MX& x);
3426  static MX nullspace(const MX& A);
3427  static MX repsum(const MX& x, casadi_int n, casadi_int m=1);
3428  static MX densify(const MX& x, const MX& val=0);
3429  static MX _bilin(const MX& A, const MX& x, const MX& y);
3430  static MX _rank1(const MX& A, const MX& alpha, const MX& x, const MX& y);
3431  static MX project(const MX& x, const Sparsity& sp, bool intersect=false);
3432  static MX cumsum(const MX &x, casadi_int axis=-1);
3433  static MX _logsumexp(const MX& x);
3434  static std::vector<MX> cse(const std::vector<MX>& e);
3435  static void extract_parametric(const MX &expr, const MX& par,
3436  MX& expr_ret, std::vector<MX>& symbols, std::vector<MX>& parametric, const Dict& opts);
3437  static void separate_linear(const MX &expr,
3438  const MX &sym_lin, const MX &sym_const,
3439  MX& expr_const, MX& expr_lin, MX& expr_nonlin);
3441 
3444  static MX find(const MX& x);
3445  static MX low(const MX& v, const MX& p, const Dict& options = Dict());
3446  static MX graph_substitute(const MX& x, const std::vector<MX> &v,
3447  const std::vector<MX> &vdef);
3448  static MX graph_substitute(const MX& x, const std::vector<MX> &v,
3449  const std::vector<MX> &vdef, bool& updated);
3450  static std::vector<MX> graph_substitute(const std::vector<MX> &ex,
3451  const std::vector<MX> &v,
3452  const std::vector<MX> &vdef);
3453  static std::vector<MX> graph_substitute(const std::vector<MX> &ex,
3454  const std::vector<MX> &v,
3455  const std::vector<MX> &vdef,
3456  bool& updated);
3457  static MX matrix_expand(const MX& e, const std::vector<MX> &boundary,
3458  const Dict& options);
3459  static std::vector<MX> matrix_expand(const std::vector<MX>& e,
3460  const std::vector<MX>& boundary,
3461  const Dict& options);
3462  static MX lift(const MX& x, const MX& x_guess);
3463  static DM evalf(const MX& m);
3464  static MX bspline(const MX& x,
3465  const DM& coeffs,
3466  const std::vector< std::vector<double> >& knots,
3467  const std::vector<casadi_int>& degree,
3468  casadi_int m,
3469  const Dict& opts = Dict());
3470  static MX bspline(const MX& x, const MX& coeffs,
3471  const std::vector< std::vector<double> >& knots,
3472  const std::vector<casadi_int>& degree,
3473  casadi_int m,
3474  const Dict& opts = Dict());
3475  static MX bspline(const MX& x, const MX& coeffs,
3476  const std::vector<MX>& knots,
3477  const std::vector<casadi_int>& degree,
3478  casadi_int m,
3479  const Dict& opts = Dict());
3480  static MX convexify(const MX& H, const Dict& opts = Dict());
3481  static MX stop_diff(const MX& expr, casadi_int order);
3482  static MX stop_diff(const MX& expr, const MX& var, casadi_int order);
3483  static std::vector<MX> difference(const std::vector<MX>& a, const std::vector<MX>& b);
3486 
3487 #endif // SWIG
3488 
3489  // Simplification with reference counting awareness
3490  static bool simplify_ref_count(std::vector<MX>& arg,
3491  std::vector<MX>& res,
3492  const Dict& opts = Dict());
3493 
3494  // Simplification with constant folding
3495  static bool simplify_const_folding(std::vector<MX>& arg,
3496  std::vector<MX>& res,
3497  const Dict& opts = Dict());
3498 
3499  // Simplification by combining like terms in linear combinations
3500  static bool simplify_combine_terms(std::vector<MX>& arg,
3501  std::vector<MX>& res,
3502  const Dict& opts = Dict());
3503 
3554  static DM bspline_dual(const std::vector<double>& x,
3555  const std::vector< std::vector<double> >& knots,
3556  const std::vector<casadi_int>& degree,
3557  const Dict& opts = Dict());
3558 
3624  static MX interpn_linear(const std::vector<MX>& x, const MX& v, const std::vector<MX>& xq,
3625  const Dict& opts=Dict());
3626 
3667  MX printme(const MX& b) const;
3668 
3669 #if !defined(SWIG) || defined(DOXYGEN)
3732  inline friend MX find(const MX& x) {
3733  return MX::find(x);
3734  }
3735 
3802  inline friend MX low(const MX& v, const MX& p, const Dict& options=Dict()) {
3803  return MX::low(v, p, options);
3804  }
3805 
3887  inline friend MX graph_substitute(const MX& ex, const std::vector<MX> &v,
3888  const std::vector<MX> &vdef) {
3889  return MX::graph_substitute(ex, v, vdef);
3890  }
3891 
3892  inline friend MX graph_substitute(const MX& ex, const std::vector<MX> &v,
3893  const std::vector<MX> &vdef, bool& updated) {
3894  return MX::graph_substitute(ex, v, vdef, updated);
3895  }
3896 
3934  inline friend std::vector<MX>
3935  graph_substitute(const std::vector<MX> &ex,
3936  const std::vector<MX> &v,
3937  const std::vector<MX> &vdef) {
3938  return MX::graph_substitute(ex, v, vdef);
3939  }
3940 
3941  inline friend std::vector<MX>
3942  graph_substitute(const std::vector<MX> &ex,
3943  const std::vector<MX> &v,
3944  const std::vector<MX> &vdef,
3945  bool& updated) {
3946  return MX::graph_substitute(ex, v, vdef, updated);
3947  }
3948 
4019  inline friend MX
4020  matrix_expand(const MX& e, const std::vector<MX> &boundary = std::vector<MX>(),
4021  const Dict& options = Dict()) {
4022  return MX::matrix_expand(e, boundary, options);
4023  }
4024 
4030  inline friend std::vector<MX>
4031  matrix_expand(const std::vector<MX>& e,
4032  const std::vector<MX> &boundary = std::vector<MX>(),
4033  const Dict& options = Dict()) {
4034  return MX::matrix_expand(e, boundary, options);
4035  }
4036 
4037 
4115  inline friend MX bspline(const MX& x,
4116  const DM& coeffs,
4117  const std::vector< std::vector<double> >& knots,
4118  const std::vector<casadi_int>& degree,
4119  casadi_int m,
4120  const Dict& opts = Dict()) {
4121  return MX::bspline(x, coeffs, knots, degree, m, opts);
4122  }
4123 
4169  inline friend MX bspline(const MX& x, const MX& coeffs,
4170  const std::vector< std::vector<double> >& knots,
4171  const std::vector<casadi_int>& degree,
4172  casadi_int m,
4173  const Dict& opts = Dict()) {
4174  return MX::bspline(x, coeffs, knots, degree, m, opts);
4175  }
4176 
4216  inline friend MX bspline(const MX& x, const MX& coeffs,
4217  const std::vector<MX>& knots,
4218  const std::vector<casadi_int>& degree,
4219  casadi_int m,
4220  const Dict& opts = Dict()) {
4221  return MX::bspline(x, coeffs, knots, degree, m, opts);
4222  }
4223 
4225  inline friend DM bspline_dual(const std::vector<double>& x,
4226  const std::vector< std::vector<double> >& knots,
4227  const std::vector<casadi_int>& degree,
4228  const Dict& opts = Dict()) {
4229  return MX::bspline_dual(x, knots, degree, opts);
4230  }
4231 
4326  inline friend MX convexify(const MX& H,
4327  const Dict& opts = Dict()) {
4328  return MX::convexify(H, opts);
4329  }
4330 
4338  inline friend MX lift(const MX& x, const MX& x_guess) {
4339  return MX::lift(x, x_guess);
4340  }
4341 
4393  inline friend MX inv_node(const MX& x) {
4394  return MX::inv_node(x);
4395  }
4396 
4451  inline friend DM evalf(const MX& expr) {
4452  return MX::evalf(expr);
4453  }
4454 
4496  inline friend MX stop_diff(const MX& expr, casadi_int order) {
4497  return MX::stop_diff(expr, order);
4498  }
4499 
4544  inline friend MX no_grad(const MX& expr) {
4545  return MX::stop_diff(expr, 1);
4546  }
4547 
4586  inline friend MX no_hess(const MX& expr) {
4587  return MX::stop_diff(expr, 2);
4588  }
4589 
4590 
4630  inline friend MX stop_diff(const MX& expr, const MX& var, casadi_int order) {
4631  return MX::stop_diff(expr, var, order);
4632  }
4633 
4674  inline friend std::vector<MX> difference(const std::vector<MX>& a, const std::vector<MX>& b) {
4675  return MX::difference(a, b);
4676  }
4677 
4679 #endif // SWIG
4680 
4740  MX attachAssert(const MX& y, const std::string& fail_message="") const;
4741 
4779  MX monitor(const std::string& comment) const;
4780 
4795  MX dump(const std::string& base_filename, const Dict& opts=Dict()) const;
4796 
4798  void reset_dump_count();
4799 
4836  MX T() const;
4837 
4886  Matrix<casadi_int> mapping() const;
4887 
4944  static void set_max_depth(casadi_int eq_depth=1);
4945 
4951  static casadi_int get_max_depth();
4952 
4954  static bool test_cast(const SharedObjectInternal* ptr);
4955 
4994  static std::vector<MX> get_input(const Function& f);
4995 
5050  static std::vector<MX> get_free(const Function& f);
5051 
5053  typedef std::map<std::string, MX> MXDict;
5054 
5110  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& SWIG_OUTPUT(res),
5111  const std::vector<bool>& unique=std::vector<bool>()) const;
5112 
5113 #ifndef SWIG
5115 
5118  void ad_forward(const std::vector<std::vector<MX> >& fseed,
5119  std::vector<std::vector<MX> >& fsens) const;
5120  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
5121  std::vector<std::vector<MX> >& asens) const;
5123 
5125  MX(const Sparsity& sp, double val, bool dummy);
5126 
5127  // Create matrix symbolic primitive
5128  static MX _sym(const std::string& name, const Sparsity& sp);
5129 
5130 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
5131  static std::mutex& get_mutex_temp() { return mutex_temp; }
5132  static std::mutex mutex_temp;
5133 #endif //CASADI_WITH_THREADSAFE_SYMBOLICS
5134  private:
5135 
5137  MX(MXNode* node, bool dummy1, bool dummy2, bool dummy3, bool dummy4);
5138 
5139  // Depth when checking equalities
5140  static casadi_int eq_depth_;
5141 
5142 #endif // SWIG
5143  };
5144 
5145 
5148  typedef std::vector<MX> MXVector;
5149  typedef std::initializer_list<MX> MXIList;
5150  typedef std::vector<MXVector> MXVectorVector;
5151  typedef std::map<std::string, MX> MXDict;
5153 
5213  inline MX kron_contract(const MX& m, const MX& x, bool inner) {
5214  return MX::kron_contract(m, x, inner);
5215  }
5216 
5217 #ifndef SWIG
5225  CASADI_EXPORT std::string export_graph(const MX& expression, const Dict& opts=Dict());
5226  CASADI_EXPORT std::string export_graph(const std::vector<MX>& expressions,
5227  const Dict& opts=Dict());
5228  CASADI_EXPORT void export_graph(const MX& expression, const std::string& fname,
5229  const Dict& opts=Dict());
5230  CASADI_EXPORT void export_graph(const std::vector<MX>& expressions, const std::string& fname,
5231  const Dict& opts=Dict());
5233 #endif // SWIG
5234 
5235 } // namespace casadi
5236 
5237 #endif // CASADI_MX_HPP
Helper class for Serialization.
Function object.
Definition: function.hpp:126
Matrix base class.
Node class for MX objects.
Definition: mx_node.hpp:51
MX - Matrix expression.
Definition: mx.hpp:145
void get_nz(MX &m, bool ind1, casadi_int kk) const
Get a set of nonzeros (what x.nz[k] does)
Definition: mx.hpp:3175
static MX lift(const MX &x, const MX &x_guess)
Definition: mx.cpp:766
GenericMatrix< MX > B
Base class.
Definition: mx.hpp:577
static std::vector< MX > difference(const std::vector< MX > &a, const std::vector< MX > &b)
Definition: mx.cpp:3092
static MX find(const MX &x)
Definition: mx.cpp:2209
static MX stop_diff(const MX &expr, casadi_int order)
Definition: mx.cpp:3020
Sparsity get_sparsity() const
Get an owning reference to the sparsity pattern.
Definition: mx.hpp:623
MX operator+() const
Unary plus: returns the expression itself.
Definition: mx.hpp:798
MX ScalarType
Definition: mx.hpp:573
void set_nz(const MX &m, bool ind1, casadi_int kk)
Assign to a set of nonzeros (what x.nz[k] = m does)
Definition: mx.hpp:3238
static MX matrix_expand(const MX &e, const std::vector< MX > &boundary, const Dict &options)
Definition: mx.cpp:2039
void get(MX &m, bool ind1, const casadi_int rr) const
Get a submatrix by linear index (what x[k] does)
Definition: mx.hpp:2891
static DM evalf(const MX &m)
Definition: mx.cpp:771
void get(MX &m, bool ind1, casadi_int rr, casadi_int cc) const
Get a submatrix by row and column index (what x[i, j] does)
Definition: mx.hpp:2962
static 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.cpp:2217
void get(MX &m, bool ind1, const Slice &rr, casadi_int cc) const
Get a submatrix by row and column index (what x[i, j] does)
Definition: mx.hpp:2953
static std::string type_name()
Get type name.
Definition: mx.hpp:150
static MX inv_node(const MX &A)
Definition: mx.cpp:2022
static MX graph_substitute(const MX &x, const std::vector< MX > &v, const std::vector< MX > &vdef)
Definition: mx.cpp:1544
static MX kron_contract(const MX &m, const MX &x, bool inner)
Definition: mx.cpp:2074
std::map< std::string, MX > MXDict
Readability typedef.
Definition: mx.hpp:5053
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())
Evaluate the B-spline basis functions at given points.
Definition: mx.cpp:2242
static MX convexify(const MX &H, const Dict &opts=Dict())
Definition: mx.cpp:2249
void get(MX &m, bool ind1, casadi_int rr, const Slice &cc) const
Get a submatrix by row and column index (what x[i, j] does)
Definition: mx.hpp:2957
static MX low(const MX &v, const MX &p, const Dict &options=Dict())
Definition: mx.cpp:2213
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:200
friend MX stop_diff(const MX &expr, const MX &var, casadi_int order)
Stop derivatives of an expression wrt a selected set of symbolic variables.
Definition: mx.hpp:4630
friend MX no_hess(const MX &expr)
Stop second derivatives of an expression wrt all its symbolic variables.
Definition: mx.hpp:4586
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())
Evaluate a B-spline whose coefficients are an expression.
Definition: mx.hpp:4169
friend MX low(const MX &v, const MX &p, const Dict &options=Dict())
Find the interval of a sorted grid that contains each query value.
Definition: mx.hpp:3802
friend std::vector< MX > matrix_expand(const std::vector< MX > &e, const std::vector< MX > &boundary=std::vector< MX >(), const Dict &options=Dict())
Expand several MX expressions into one call to an SX Function.
Definition: mx.hpp:4031
friend MX convexify(const MX &H, const Dict &opts=Dict())
Make a symmetric matrix positive definite at evaluation time.
Definition: mx.hpp:4326
friend MX inv_node(const MX &x)
Symbolic matrix inverse node.
Definition: mx.hpp:4393
friend std::vector< MX > difference(const std::vector< MX > &a, const std::vector< MX > &b)
Return all elements of a that do not occur in b, preserving order.
Definition: mx.hpp:4674
friend MX find(const MX &x)
Find the index of the first nonzero entry of a vector.
Definition: mx.hpp:3732
friend MX no_grad(const MX &expr)
Treat an expression as constant for differentiation (stop first derivatives)
Definition: mx.hpp:4544
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())
Evaluate a B-spline whose knots and coefficients are expressions.
Definition: mx.hpp:4216
friend MX graph_substitute(const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
Definition: mx.hpp:3892
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())
Evaluate a tensor-product B-spline with fixed coefficients.
Definition: mx.hpp:4115
friend std::vector< MX > graph_substitute(const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
Substitute nodes in several expressions at once, by node identity.
Definition: mx.hpp:3935
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:3942
friend MX matrix_expand(const MX &e, const std::vector< MX > &boundary=std::vector< MX >(), const Dict &options=Dict())
Expand an MX expression into a call to an SX Function.
Definition: mx.hpp:4020
friend MX lift(const MX &x, const MX &x_guess)
Lift the expression.
Definition: mx.hpp:4338
friend MX stop_diff(const MX &expr, casadi_int order)
Stop derivatives of an expression with respect to all its symbolic variables.
Definition: mx.hpp:4496
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())
Evaluate the B-spline basis functions at given points, see MX::bspline_dual.
Definition: mx.hpp:4225
friend DM evalf(const MX &expr)
Evaluate an expression without free symbols numerically.
Definition: mx.hpp:4451
friend MX graph_substitute(const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
Substitute nodes in the expression graph, by node identity.
Definition: mx.hpp:3887
The casadi namespace.
Definition: archiver.cpp:28
std::map< std::string, MX > MXDict
Definition: mx.hpp:5151
bool is_equal(double x, double y, casadi_int depth=0)
Definition: calculus.hpp:287
template class CASADI_EXPORT Matrix< double >
T norm_1(const std::vector< T > &x)
T norm_inf(const std::vector< T > &x)
double if_else(double x, double y, double z)
Definition: calculus.hpp:296
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros
std::vector< MX > MXVector
Definition: mx.hpp:5148
void linspace(std::vector< T > &v, const F &first, const L &last)
Matlab's linspace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::vector< T > cumsum(const std::vector< T > &values)
cumulative sum
const double inf
infinity
Definition: calculus.hpp:50
double printme(double x, double y)
Definition: calculus.hpp:279
std::vector< MXVector > MXVectorVector
Definition: mx.hpp:5150
CASADI_EXPORT std::string export_graph(const MX &expression, const Dict &opts=Dict())
Export expressions as .html, .dot, or .casadi_viz, defaulting to expression view.
Definition: visualizer.cpp:545
T dot(const std::vector< T > &a, const std::vector< T > &b)
const double nan
Not a number.
Definition: calculus.hpp:53
MX kron_contract(const MX &m, const MX &x, bool inner)
Kronecker contraction, the adjoint of kron.
Definition: mx.hpp:5213
T norm_2(const std::vector< T > &x)
std::initializer_list< MX > MXIList
Definition: mx.hpp:5149
bool is_zero(const T &x)
std::vector< T > reverse(const std::vector< T > &v)
Reverse a list.
bool is_regular(const std::vector< T > &v)
Checks if array does not contain NaN or Inf.
double simplify(double x)
Definition: calculus.hpp:277
std::vector< casadi_int > scc_offset
Definition: mx.hpp:57
casadi_int * iw
Definition: mx.hpp:64
casadi_convexify_config< double > config
Definition: mx.hpp:61
casadi_int sz_iw
Definition: mx.hpp:62
Sparsity Hrsp
Definition: mx.hpp:59
double * w
Definition: mx.hpp:65
casadi_int sz_w
Definition: mx.hpp:63
Sparsity Hsp
Definition: mx.hpp:60
std::vector< casadi_int > scc_mapping
Definition: mx.hpp:57
Sparsity scc_sp
Definition: mx.hpp:58