28 #include "shared_object.hpp"
29 #include "matrix_fwd.hpp"
32 #include "generic_matrix.hpp"
33 #include "generic_expression.hpp"
34 #include "generic_type.hpp"
35 #include "printable.hpp"
37 #ifdef CASADI_WITH_THREAD
38 #ifdef CASADI_WITH_THREAD_MINGW
39 #include <mingw.mutex.h>
52 class SerializingStream;
53 class DeserializingStream;
56 struct ConvexifyData {
57 std::vector<casadi_int> scc_offset, scc_mapping;
88 class CASADI_EXPORT
MX :
89 public SWIG_IF_ELSE(GenericExpressionCommon, GenericExpression<MX>),
90 public SWIG_IF_ELSE(PrintableCommon, Printable<MX>),
107 MX(casadi_int nrow, casadi_int ncol);
113 explicit MX(
const std::pair<casadi_int, casadi_int>& rc);
150 MX(
const std::vector<double> &x);
175 static std::vector<MX> createMultipleOutput(
MXNode* node);
185 typedef MX ScalarType;
227 void erase(
const std::vector<casadi_int>& rr,
const std::vector<casadi_int>& cc,
235 void erase(
const std::vector<casadi_int>& rr,
bool ind1=
false);
242 void enlarge(casadi_int nrow, casadi_int ncol,
243 const std::vector<casadi_int>& rr,
const std::vector<casadi_int>& cc,
259 const MXNode* operator->()
const;
288 explicit operator double()
const;
372 bool has_duplicates()
const;
377 void reset_input()
const;
415 casadi_int
op()
const;
432 casadi_int get_temp()
const;
435 void set_temp(casadi_int t)
const;
451 static MX inf(casadi_int nrow=1, casadi_int ncol=1);
452 static MX inf(
const std::pair<casadi_int, casadi_int>& rc);
460 static MX nan(casadi_int nrow=1, casadi_int ncol=1);
461 static MX nan(
const std::pair<casadi_int, casadi_int>& rc);
476 void get(
MX& SWIG_OUTPUT(m),
bool ind1,
const Slice& rr)
const;
479 void get(
MX& SWIG_OUTPUT(m),
bool ind1,
const MX& rr)
const;
480 void get(
MX& SWIG_OUTPUT(m),
bool ind1,
const casadi_int rr)
const {
489 void get(
MX& SWIG_OUTPUT(m),
bool ind1,
const Slice& rr, casadi_int cc)
const {
493 void get(
MX& SWIG_OUTPUT(m),
bool ind1, casadi_int rr,
const Slice& cc)
const {
498 void get(
MX& SWIG_OUTPUT(m),
bool ind1, casadi_int rr,
499 casadi_int cc)
const {
502 void get(
MX& SWIG_OUTPUT(m),
bool ind1,
const MX& rr,
const Slice& cc)
const;
503 void get(
MX& SWIG_OUTPUT(m),
bool ind1,
const Slice& rr,
const MX& cc)
const;
504 void get(
MX& SWIG_OUTPUT(m),
bool ind1,
const MX& rr,
const MX& cc)
const;
526 void get_nz(
MX& SWIG_OUTPUT(m),
bool ind1,
const MX& kk)
const;
527 void get_nz(
MX& SWIG_OUTPUT(m),
bool ind1, casadi_int kk)
const {
530 void get_nz(
MX& SWIG_OUTPUT(m),
bool ind1,
const MX& inner,
const Slice& outer)
const;
531 void get_nz(
MX& SWIG_OUTPUT(m),
bool ind1,
const Slice& inner,
const MX& outer)
const;
532 void get_nz(
MX& SWIG_OUTPUT(m),
bool ind1,
const MX& inner,
const MX& outer)
const;
559 const std::vector<casadi_int>& dim_a,
const std::vector<casadi_int>& dim_b,
560 const std::vector<casadi_int>& dim_c,
561 const std::vector<casadi_int>& a,
const std::vector<casadi_int>& b,
562 const std::vector<casadi_int>& c);
565 const std::vector<casadi_int>& dim_a,
const std::vector<casadi_int>& dim_b,
566 const std::vector<casadi_int>& dim_c,
567 const std::vector<casadi_int>& a,
const std::vector<casadi_int>& b,
568 const std::vector<casadi_int>& c);
575 static bool is_equal(
const MX& x,
const MX& y, casadi_int depth=0);
576 static MX mmin(
const MX &x);
577 static MX mmax(
const MX &x);
582 static MX horzcat(
const std::vector<MX>& x);
583 static MX diagcat(
const std::vector<MX>& x);
584 static MX vertcat(
const std::vector<MX>& x);
585 static std::vector<MX> horzsplit(
const MX& x,
const std::vector<casadi_int>& offset);
586 static std::vector<MX> diagsplit(
const MX& x,
const std::vector<casadi_int>& offset1,
587 const std::vector<casadi_int>& offset2);
588 static std::vector<MX> vertsplit(
const MX& x,
const std::vector<casadi_int>& offset);
589 static MX blockcat(
const std::vector< std::vector<MX > > &v);
590 static MX mtimes(
const MX& x,
const MX& y);
591 static MX mac(
const MX& x,
const MX& y,
const MX& z);
592 static MX reshape(
const MX& x, casadi_int nrow, casadi_int ncol);
594 static MX sparsity_cast(
const MX& x,
const Sparsity& sp);
595 static MX kron(
const MX& x,
const MX& b);
596 static MX repmat(
const MX& x, casadi_int n, casadi_int m=1);
601 static MX jacobian(
const MX& f,
const MX& x,
const Dict& opts =
Dict());
602 static MX hessian(
const MX& f,
const MX& x,
const Dict& opts =
Dict());
603 static MX hessian(
const MX& f,
const MX& x,
MX& g,
const Dict& opts =
Dict());
604 static std::vector<std::vector<MX> >
605 forward(
const std::vector<MX> &ex,
606 const std::vector<MX> &arg,
607 const std::vector<std::vector<MX> > &v,
609 static std::vector<std::vector<MX> >
610 reverse(
const std::vector<MX> &ex,
611 const std::vector<MX> &arg,
612 const std::vector<std::vector<MX> > &v,
614 static std::vector<bool> which_depends(
const MX &expr,
const MX &var,
615 casadi_int order=1,
bool tr=
false);
616 static Sparsity jacobian_sparsity(
const MX& f,
const MX& x);
617 static MX substitute(
const MX& ex,
const MX& v,
const MX& vdef);
618 static std::vector<MX> substitute(
const std::vector<MX> &ex,
619 const std::vector<MX> &v,
620 const std::vector<MX> &vdef);
621 static void substitute_inplace(
const std::vector<MX>& v,
622 std::vector<MX>& vdef,
623 std::vector<MX>& ex,
bool reverse);
624 static MX solve(
const MX& a,
const MX& b);
625 static MX solve(
const MX& a,
const MX& b,
const std::string& lsolver,
627 static MX inv_minor(
const MX& A);
628 static MX inv_node(
const MX& A);
629 static MX inv(
const MX& A,
const std::string& lsolver=
"qr",
const Dict& dict =
Dict());
630 static MX pinv(
const MX& A,
const std::string& lsolver=
"qr",
632 static MX expm_const(
const MX& A,
const MX& t);
633 static MX expm(
const MX& A);
634 static casadi_int n_nodes(
const MX& x);
635 static std::string print_operator(
const MX& x,
const std::vector<std::string>& args);
636 static void extract(std::vector<MX>& ex, std::vector<MX>& v,
637 std::vector<MX>& vdef,
const Dict& opts =
Dict());
638 static void shared(std::vector<MX>& ex, std::vector<MX>& v,
639 std::vector<MX>& vdef,
const std::string& v_prefix,
const std::string& v_suffix);
640 static MX if_else(
const MX& cond,
const MX& if_true,
641 const MX& if_false,
bool short_circuit=
false);
642 static MX conditional(
const MX& ind,
const std::vector<MX> &x,
const MX& x_default,
643 bool short_circuit=
false);
644 static bool depends_on(
const MX& x,
const MX& arg);
645 static bool contains_all(
const std::vector<MX>& v,
const std::vector<MX> &n);
646 static bool contains_any(
const std::vector<MX>& v,
const std::vector<MX> &n);
647 static MX simplify(
const MX& x);
648 static MX dot(
const MX& x,
const MX& y);
649 static MX mrdivide(
const MX& a,
const MX& b);
650 static MX mldivide(
const MX& a,
const MX& b);
651 static MX norm_2(
const MX& x);
652 static MX norm_fro(
const MX& x);
653 static MX norm_1(
const MX& x);
654 static MX norm_inf(
const MX& x);
655 static MX unite(
const MX& A,
const MX& B);
656 static MX trace(
const MX& x);
657 static MX diag(
const MX& x);
658 static MX sum2(
const MX& x);
659 static MX sum1(
const MX& x);
660 static MX polyval(
const MX& p,
const MX& x);
661 static MX det(
const MX& x);
662 static std::vector<MX> symvar(
const MX& x);
663 static MX nullspace(
const MX& A);
664 static MX repsum(
const MX& x, casadi_int n, casadi_int m=1);
665 static MX densify(
const MX& x,
const MX& val=0);
666 static MX _bilin(
const MX& A,
const MX& x,
const MX& y);
667 static MX _rank1(
const MX& A,
const MX& alpha,
const MX& x,
const MX& y);
668 static MX project(
const MX& x,
const Sparsity& sp,
bool intersect=
false);
669 static MX cumsum(
const MX &x, casadi_int axis=-1);
670 static MX _logsumexp(
const MX& x);
671 static std::vector<MX> cse(
const std::vector<MX>& e);
672 static void extract_parametric(
const MX &expr,
const MX& par,
673 MX& expr_ret, std::vector<MX>& symbols, std::vector<MX>& parametric,
const Dict& opts);
674 static void separate_linear(
const MX &expr,
675 const MX &sym_lin,
const MX &sym_const,
676 MX& expr_const,
MX& expr_lin,
MX& expr_nonlin);
681 static MX find(
const MX& x);
682 static MX low(
const MX& v,
const MX& p,
const Dict& options =
Dict());
683 static MX graph_substitute(
const MX& x,
const std::vector<MX> &v,
684 const std::vector<MX> &vdef);
685 static MX graph_substitute(
const MX& x,
const std::vector<MX> &v,
686 const std::vector<MX> &vdef,
bool& updated);
687 static std::vector<MX> graph_substitute(
const std::vector<MX> &ex,
688 const std::vector<MX> &v,
689 const std::vector<MX> &vdef);
690 static std::vector<MX> graph_substitute(
const std::vector<MX> &ex,
691 const std::vector<MX> &v,
692 const std::vector<MX> &vdef,
694 static MX matrix_expand(
const MX& e,
const std::vector<MX> &boundary,
695 const Dict& options);
696 static std::vector<MX> matrix_expand(
const std::vector<MX>& e,
697 const std::vector<MX>& boundary,
698 const Dict& options);
699 static MX lift(
const MX& x,
const MX& x_guess);
700 static DM evalf(
const MX& m);
701 static MX bspline(
const MX& x,
703 const std::vector< std::vector<double> >& knots,
704 const std::vector<casadi_int>& degree,
707 static MX bspline(
const MX& x,
const MX& coeffs,
708 const std::vector< std::vector<double> >& knots,
709 const std::vector<casadi_int>& degree,
712 static MX convexify(
const MX& H,
const Dict& opts =
Dict());
713 static MX stop_diff(
const MX& expr, casadi_int order);
714 static MX stop_diff(
const MX& expr,
const MX& var, casadi_int order);
715 static std::vector<MX> difference(
const std::vector<MX>& a,
const std::vector<MX>& b);
722 const std::vector< std::vector<double> >& knots,
723 const std::vector<casadi_int>& degree,
738 #if !defined(SWIG) || defined(DOXYGEN)
767 const std::vector<MX> &vdef) {
772 const std::vector<MX> &vdef,
bool& updated) {
782 inline friend std::vector<MX>
784 const std::vector<MX> &v,
785 const std::vector<MX> &vdef) {
789 inline friend std::vector<MX>
791 const std::vector<MX> &v,
792 const std::vector<MX> &vdef,
815 inline friend std::vector<MX>
817 const std::vector<MX> &boundary = std::vector<MX>(),
825 const std::vector< std::vector<double> >& knots,
826 const std::vector<casadi_int>& degree,
829 return MX::bspline(x, coeffs, knots, degree, m, opts);
833 const std::vector< std::vector<double> >& knots,
834 const std::vector<casadi_int>& degree,
837 return MX::bspline(x, coeffs, knots, degree, m, opts);
841 const std::vector< std::vector<double> >& knots,
842 const std::vector<casadi_int>& degree,
911 inline friend std::vector<MX>
difference(
const std::vector<MX>& a,
const std::vector<MX>& b) {
964 typedef std::map<std::string, MX>
MXDict;
969 void eval_mx(
const std::vector<MX>& arg, std::vector<MX>& SWIG_OUTPUT(res))
const;
976 void ad_forward(
const std::vector<std::vector<MX> >& fseed,
977 std::vector<std::vector<MX> >& fsens)
const;
978 void ad_reverse(
const std::vector<std::vector<MX> >& aseed,
979 std::vector<std::vector<MX> >& asens)
const;
983 MX(
const Sparsity& sp,
double val,
bool dummy);
986 static MX _sym(
const std::string& name,
const Sparsity& sp);
988 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
989 static std::mutex& get_mutex_temp() {
return mutex_temp; }
990 static std::mutex mutex_temp;
995 MX(
MXNode* node,
bool dummy1,
bool dummy2,
bool dummy3,
bool dummy4);
998 static casadi_int eq_depth_;
Helper class for Serialization.
Node class for MX objects.
void get_nz(MX &m, bool ind1, casadi_int kk) const
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.
bool is_multiplication() const
Check if multiplication.
bool is_minus_one() const
check if zero (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
static casadi_int get_max_depth()
Get the depth to which equalities are being checked for simplifications.
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.
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.
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.
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.
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.
static MX nan(const std::pair< casadi_int, casadi_int > &rc)
create a matrix with all nan
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)
void set_nz(const MX &m, bool ind1, const MX &kk)
void get(MX &m, bool ind1, const casadi_int rr) const
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.
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
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
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.
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_norm() const
Check if norm.
void set_nz(const MX &m, bool ind1, const Slice &kk)
static MX binary(casadi_int op, const MX &x, const MX &y)
Create nodes by their ID.
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
void set(const MX &m, bool ind1, const Slice &rr, const Matrix< casadi_int > &cc)
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const
Evaluate the MX node with new symbolic dependencies.
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
std::map< std::string, MX > MXDict
Readability typedef.
void set(const MX &m, bool ind1, const Slice &rr)
std::vector< MX > primitives() const
Get primitives.
bool is_one() const
check if zero (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
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
static MX unary(casadi_int op, const MX &x)
Create nodes by their ID.
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.
static DM bspline_dual(const std::vector< double > &x, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > °ree, 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.
std::map< std::string, MX > MXDict
std::vector< MX > MXVector
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::vector< MXVector > MXVectorVector
std::initializer_list< MX > MXIList