26 #ifndef CASADI_MATRIX_DECL_HPP
27 #define CASADI_MATRIX_DECL_HPP
29 #include "matrix_fwd.hpp"
30 #include "exception.hpp"
31 #include "casadi_limits.hpp"
32 #include "runtime/casadi_runtime.hpp"
33 #include "generic_matrix.hpp"
34 #include "generic_expression.hpp"
35 #include "printable.hpp"
40 #include <initializer_list>
41 #ifdef CASADI_WITH_THREAD
42 #ifdef CASADI_WITH_THREAD_MINGW
43 #include <mingw.mutex.h>
63 template <
typename Scalar>
inline std::string matrixName()
64 {
return std::string(
"Matrix<") +
typeid(Scalar).name() + std::string(
">");}
65 template<>
inline std::string matrixName<double>() {
return "DM"; }
66 template<>
inline std::string matrixName<casadi_int>() {
return "IM"; }
142 template<
typename Scalar>
145 public SWIG_IF_ELSE(GenericExpressionCommon, GenericExpression<Matrix<Scalar> >),
147 public SWIG_IF_ELSE(PrintableCommon, Printable<Matrix<Scalar> >) {
233 Matrix(casadi_int nrow, casadi_int ncol);
239 explicit Matrix(
const std::pair<casadi_int, casadi_int>& rc);
244 std::vector<Scalar>* operator->();
249 const std::vector<Scalar>* operator->()
const;
353 #if !(defined(SWIG) && defined(SWIGMATLAB))
387 explicit Matrix(
const std::vector< std::vector<double> >& m);
410 nonzeros_(std::vector<Scalar>(x.size())) {
411 auto x_it = x.begin();
412 for (
auto&& d : nonzeros_) d =
static_cast<Scalar
>(*x_it++);
459 Matrix(
const Matrix<A>& x) : sparsity_(x.sparsity()), nonzeros_(std::vector<Scalar>(x.nnz())) {
460 auto x_it = x->begin();
461 for (
auto&& d : nonzeros_) d =
static_cast<Scalar
>(*x_it++);
466 Matrix(
const std::vector<Scalar>& x);
469 Matrix(std::initializer_list<Scalar> x) :
Matrix<Scalar>(std::vector<Scalar>(x)) {}
472 const Scalar scalar()
const;
475 typedef Scalar ScalarType;
478 typedef GenericMatrix<Matrix<Scalar> > B;
542 bool has_nz(casadi_int rr, casadi_int cc)
const;
576 bool __nonzero__()
const;
663 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Slice& rr)
const;
664 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Matrix<casadi_int>& rr)
const;
665 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Sparsity& sp)
const;
755 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
756 const Slice& rr,
const Slice& cc)
const;
757 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
758 const Slice& rr,
const Matrix<casadi_int>& cc)
const;
759 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
760 const Matrix<casadi_int>& rr,
const Slice& cc)
const;
761 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
762 const Matrix<casadi_int>& rr,
const Matrix<casadi_int>& cc)
const;
848 void set(
const Matrix<Scalar>& m,
bool ind1,
const Slice& rr);
849 void set(
const Matrix<Scalar>& m,
bool ind1,
const Matrix<casadi_int>& rr);
850 void set(
const Matrix<Scalar>& m,
bool ind1,
const Sparsity& sp);
944 void set(
const Matrix<Scalar>& m,
bool ind1,
const Slice& rr,
const Slice& cc);
945 void set(
const Matrix<Scalar>& m,
bool ind1,
const Slice& rr,
const Matrix<casadi_int>& cc);
946 void set(
const Matrix<Scalar>& m,
bool ind1,
const Matrix<casadi_int>& rr,
const Slice& cc);
947 void set(
const Matrix<Scalar>& m,
bool ind1,
const Matrix<casadi_int>& rr,
948 const Matrix<casadi_int>& cc);
1020 void get_nz(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Slice& k)
const;
1021 void get_nz(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Matrix<casadi_int>& k)
const;
1087 void set_nz(
const Matrix<Scalar>& m,
bool ind1,
const Slice& k);
1088 void set_nz(
const Matrix<Scalar>& m,
bool ind1,
const Matrix<casadi_int>& k);
1107 static std::vector< Matrix<Scalar> > call(
const Function& f,
1109 static std::vector< Scalar > call(
const Function& f,
const std::vector< Scalar > &x);
1127 const std::vector<std::vector<GenericType> >& passes,
const Dict& opts =
Dict());
1128 static std::vector<Matrix<Scalar> > transform(
const std::vector<
Matrix<Scalar> >& x,
1130 static std::vector<Matrix<Scalar> > transform(
const std::vector<
Matrix<Scalar> >& x,
1131 const std::vector<std::vector<GenericType> >& passes,
const Dict& opts =
Dict());
1143 static std::vector<Matrix<Scalar> >
1147 static void substitute_inplace(
const std::vector<
Matrix<Scalar> >& v,
1153 const std::string& lsolver,
const Dict& opts);
1158 const std::string& lsolver,
const Dict& opts);
1161 const std::string& lsolver,
const Dict& opts);
1165 const std::vector<std::string>& args);
1171 const std::string& v_prefix,
1172 const std::string& v_suffix);
1183 bool short_circuit=
false);
1187 bool short_circuit=
false);
1197 static std::vector<Matrix<Scalar> > symvar(
const Matrix<Scalar> &x);
1214 const Sparsity& sp,
bool intersect=
false);
1220 const std::vector<casadi_int>& dim_a,
const std::vector<casadi_int>& dim_b,
1221 const std::vector<casadi_int>& dim_c,
1222 const std::vector<casadi_int>& a,
const std::vector<casadi_int>& b,
1223 const std::vector<casadi_int>& c);
1226 const std::vector<casadi_int>& dim_a,
const std::vector<casadi_int>& dim_b,
1227 const std::vector<casadi_int>& dim_c,
1228 const std::vector<casadi_int>& a,
const std::vector<casadi_int>& b,
1229 const std::vector<casadi_int>& c);
1232 static std::vector< Matrix<Scalar> > cse(
const std::vector<
Matrix<Scalar> >& e);
1239 static std::vector<Matrix<Scalar> >
1241 const std::vector<casadi_int>& offset);
1243 static std::vector< Matrix<Scalar> >
1245 const std::vector<casadi_int>& offset);
1246 static std::vector< Matrix<Scalar> >
1248 const std::vector<casadi_int>& offset1,
1249 const std::vector<casadi_int>& offset2);
1255 const std::string& blas =
"reference");
1259 const std::string& blas =
"reference");
1292 static std::vector<std::vector<Matrix<Scalar> > >
1297 static std::vector<std::vector<Matrix<Scalar> > >
1303 casadi_int order=1,
bool tr=
false);
1311 const std::vector<casadi_int>& order_contributions);
1318 std::vector<casadi_int>& pc,
bool amd=
true);
1321 const std::vector<casadi_int>& prinv,
1322 const std::vector<casadi_int>& pc,
bool tr=
false);
1325 std::vector<casadi_int>& p,
bool amd=
true);
1339 static bool simplify_ref_count(std::vector<
Matrix<Scalar> >& arg,
1344 static bool simplify_const_folding(std::vector<
Matrix<Scalar> >& arg,
1349 static bool simplify_combine_terms(std::vector<
Matrix<Scalar> >& arg,
1361 #if !defined(SWIG) || defined(DOXYGEN)
1640 std::vector<casadi_int>& pc,
bool amd=
true) {
1690 const std::vector<casadi_int>& prinv,
1691 const std::vector<casadi_int>& pc,
bool tr=
false) {
1824 std::vector<casadi_int>& p,
bool amd=
true) {
1872 const std::vector<casadi_int>& p) {
2386 casadi_int order=5) {
2551 const std::vector<casadi_int>& order_contributions) {
2938 static std::string type_name();
2984 void print_split(std::vector<std::string>& SWIG_OUTPUT(nz),
2985 std::vector<std::string>& SWIG_OUTPUT(inter))
const;
2994 void disp(std::ostream& stream,
bool more=
false)
const;
3022 void print_scalar(std::ostream &stream)
const;
3045 void print_vector(std::ostream &stream,
bool truncate=
true)
const;
3073 void print_dense(std::ostream &stream,
bool truncate=
true)
const;
3104 void print_sparse(std::ostream &stream,
bool truncate=
true)
const;
3108 static void print_default(std::ostream &stream,
const Sparsity& sp,
const Scalar* nonzeros,
3109 bool truncate=
true);
3112 static void print_scalar(std::ostream &stream,
const Scalar& e);
3115 static void print_vector(std::ostream &stream,
const Sparsity& sp,
const Scalar* nonzeros,
3116 bool truncate=
true);
3119 static void print_canonical(std::ostream &stream,
const Sparsity& sp,
const Scalar* nonzeros,
3120 bool truncate=
true);
3123 static void print_sparse(std::ostream &stream,
const Sparsity& sp,
const Scalar* nonzeros,
3124 bool truncate=
true);
3127 static void print_split(casadi_int nnz,
const Scalar* nonzeros, std::vector<std::string>& nz,
3128 std::vector<std::string>& inter);
3131 static void print_dense(std::ostream &stream,
const Sparsity& sp,
const Scalar* nonzeros,
3132 bool truncate=
true);
3189 void resize(casadi_int nrow, casadi_int ncol);
3246 void erase(
const std::vector<casadi_int>& rr,
const std::vector<casadi_int>& cc,
3286 void erase(
const std::vector<casadi_int>& rr,
bool ind1=
false);
3322 void remove(
const std::vector<casadi_int>& rr,
const std::vector<casadi_int>& cc);
3382 const std::vector<casadi_int>& rr,
const std::vector<casadi_int>& cc,
3388 std::vector<Scalar>& nonzeros();
3389 const std::vector<Scalar>& nonzeros()
const;
3395 const Scalar* ptr()
const;
3396 friend inline Scalar* get_ptr(
Matrix<Scalar>& v) {
return v.ptr(); }
3397 friend inline const Scalar* get_ptr(
const Matrix<Scalar>& v) {
return v.ptr(); }
3401 const Sparsity& sparsity()
const;
3515 const std::vector<casadi_int>& col,
3518 const std::vector<casadi_int>& col,
3521 const std::vector<casadi_int>& col,
3523 const std::pair<casadi_int, casadi_int>& rc);
3867 bool has_duplicates()
const;
3872 void reset_input()
const;
4129 template<
typename A>
4130 std::vector<A> get_nonzeros()
const;
4166 explicit operator double()
const;
4192 explicit operator casadi_int()
const;
4198 template<
typename A>
4199 explicit operator std::vector<A>()
const;
4323 static void set_precision(casadi_int precision);
4324 static void set_width(casadi_int width);
4325 static void set_scientific(
bool scientific);
4330 static casadi_int get_precision();
4331 static casadi_int get_width();
4332 static bool get_scientific();
4370 static void rng(casadi_int seed);
4418 const std::pair<casadi_int, casadi_int>& rc);
4470 void serialize(std::ostream &stream)
const;
4597 void to_file(
const std::string& filename,
const std::string& format=
"")
const;
4599 static void to_file(
const std::string& filename,
const Sparsity& sp,
4600 const Scalar* nonzeros,
const std::string& format=
"");
4668 Matrix(
const Sparsity& sp,
const std::vector<Scalar>& d,
bool dummy);
4674 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
4675 static std::mutex& get_mutex_temp();
4683 std::vector<Scalar> nonzeros_;
4686 static casadi_int stream_precision_;
4687 static casadi_int stream_width_;
4688 static bool stream_scientific_;
4691 static std::default_random_engine rng_;
4697 template<
typename Scalar>
4698 template<
typename A>
4700 std::vector<A> ret(nnz());
4701 auto r = ret.begin();
4702 for (
auto&& e : nonzeros()) *r++ =
static_cast<A
>(e);
4707 template<
typename Scalar>
4708 template<
typename A>
4711 casadi_int size1 = this->size1(), size2 = this->size2();
4712 const casadi_int *colind = this->colind(), *row = this->row();
4714 auto it = nonzeros().begin();
4715 std::vector<A> ret(numel(), 0);
4716 for (casadi_int cc=0; cc<size2; ++cc) {
4717 for (casadi_int el=colind[cc]; el<colind[cc+1]; ++el) {
4718 ret[row[el] + cc*size1] =
static_cast<A
>(*it++);
Helper class for Serialization.
Sparse matrix class. SX and DM are specializations.
Matrix(const Matrix< A > &x)
Create a matrix from another matrix with a different entry type.
bool is_inf() const
check if the matrix is inf (note that false negative answers are possible)
static Matrix< Scalar > nan(const std::pair< casadi_int, casadi_int > &rc)
Create a matrix with every nonzero equal to nan.
bool has_zeros() const
Check if any structural nonzero is known to be numerically zero.
static Matrix< Scalar > deserialize(const std::string &s)
Build a matrix from a string produced by serialize.
bool is_one() const
check if the matrix is 1 (note that false negative answers are possible)
void remove(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc)
Delete rows and columns, shrinking the matrix in place.
bool is_regular() const
Check that no entry is nan, inf or -inf.
Matrix(double val)
Dense 1-by-1 matrix holding a number; enables implicit conversion from a numeric type.
Matrix< Scalar > T() const
Transpose the matrix.
static void set_max_depth(casadi_int eq_depth=1)
Set the depth to which SX subexpressions are compared during simplification.
Function which_function() const
Get function - only valid when is_call() is true.
static Matrix< Scalar > rand(const Sparsity &sp)
Create a matrix with uniformly distributed random numbers in [0, 1)
void resize(casadi_int nrow, casadi_int ncol)
Change the shape in place, keeping nonzeros that remain inside the new bounds.
Sparsity get_sparsity() const
Get the sparsity pattern (Python: sparsity())
static Matrix< Scalar > deserialize(std::istream &stream)
Build a matrix from a serialization stream.
casadi_int element_hash() const
Hash of the node of a 1-by-1 SX expression.
casadi_int which_output() const
Get the index of evaluation output - only valid when is_output() is true.
void erase(const std::vector< casadi_int > &rr, bool ind1=false)
Erase the elements at the given linear indices, leaving structural zeros.
void clear()
Reset to an empty 0-by-0 matrix, in place.
bool is_value(double val) const
check if the matrix is value
void export_code(const std::string &lang, std::ostream &stream=casadi::uout(), const Dict &options=Dict()) const
Export matrix in specific language.
void serialize(SerializingStream &s) const
Serialize an object.
bool is_commutative() const
Check whether a binary SX is commutative.
Dict info() const
Dictionary with the sparsity pattern and the nonzeros (DM only)
bool is_symbolic() const
Check if symbolic (Dense)
Matrix(const Sparsity &sp)
Create a matrix with the given sparsity pattern and all nonzeros equal to one.
static std::vector< Matrix< Scalar > > get_free(const Function &f)
Get the free symbols of an SX Function.
Matrix(casadi_int nrow, casadi_int ncol)
Create a matrix of the given shape with all structural zeros (no nonzeros)
static casadi_int get_max_depth()
Get the depth to which SX subexpressions are compared during simplification.
bool is_call() const
Check if function call.
Matrix< Scalar > operator+() const
bool is_smooth() const
Check whether an SX expression contains only smooth operations.
static Matrix< Scalar > nan(const Sparsity &sp)
Create a matrix with every nonzero equal to nan.
static Matrix< Scalar > triplet(const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d, casadi_int nrow, casadi_int ncol)
Construct a sparse matrix from triplet form (row indices, column indices, values)
bool is_minus_one() const
check if the matrix is -1 (note that false negative answers are possible)
void to_file(const std::string &filename, const std::string &format="") const
Write a numerical matrix to a text file (DM only)
static Matrix< Scalar > triplet(const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d)
Construct a sparse matrix from triplet form (row indices, column indices, values)
bool is_half() const
check if the matrix is 0.5 (note that false negative answers are possible)
void disp(std::ostream &stream, bool more=false) const
Print a representation of the object to a stream.
static Matrix< Scalar > rand(casadi_int nrow=1, casadi_int ncol=1)
Create a matrix with uniformly distributed random numbers in [0, 1)
bool is_eye() const
check if the matrix is an identity matrix (note that false negative answers
bool is_op(casadi_int op) const
Is it a certain operation.
bool is_minus_inf() const
check if the matrix is -inf (note that false negative answers are possible)
void erase(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
Erase the block at the given rows and columns, leaving structural zeros.
static Matrix< Scalar > inf(casadi_int nrow=1, casadi_int ncol=1)
Create a matrix with every nonzero equal to +inf.
bool is_output() const
Check if evaluation output.
std::vector< Scalar > get_elements() const
Get all elements as a flat list in column-major order (Python: elements())
static Matrix< Scalar > triplet(const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d, const std::pair< casadi_int, casadi_int > &rc)
Construct a sparse matrix from triplet form (row indices, column indices, values)
Matrix(const std::vector< std::vector< double > > &m)
Dense matrix from a list of rows.
static std::vector< Matrix< Scalar > > get_input(const Function &f)
Get the symbolic input expressions of an SX Function.
Matrix< Scalar > dep(casadi_int ch=0) const
Get operand ch of a 1-by-1 SX expression.
bool is_zero() const
check if the matrix is 0 (note that false negative answers are possible)
static Matrix< Scalar > inf(const Sparsity &sp)
Create a matrix with every nonzero equal to +inf.
static Matrix< double > from_file(const std::string &filename, const std::string &format_hint="")
Read a numerical matrix from a text file written by to_file (DM only)
std::string serialize() const
Serialize to a string.
void enlarge(casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
Enlarge the matrix in place, moving the existing rows and columns to new positions.
static Matrix< Scalar > deserialize(DeserializingStream &s)
bool is_constant() const
Check if every entry is a numerical constant (no symbols)
bool is_nonnegative() const
check if the matrix is not negative
Matrix(const std::vector< A > &x)
Dense column vector from a flat list of numbers.
bool has_output() const
Check if a multiple output node.
void reserve(casadi_int nnz)
Reserve storage for the given number of nonzeros (performance hint, no visible effect)
casadi_int op() const
Get operation type.
void reserve(casadi_int nnz, casadi_int ncol)
static Matrix< Scalar > eye(casadi_int n)
Create an n-by-n identity matrix.
bool is_valid_input() const
Check if matrix can be used to define function inputs.
Matrix(const Sparsity &sp, const Matrix< Scalar > &d)
Create a matrix with a given sparsity pattern and given nonzeros.
static Matrix< Scalar > rand(const std::pair< casadi_int, casadi_int > &rc)
Create a matrix with uniformly distributed random numbers in [0, 1)
Matrix< Scalar > get_output(casadi_int oind) const
Get an output.
bool is_integer() const
Check if the matrix is integer-valued.
Matrix< Scalar > operator-() const
casadi_int n_dep() const
Number of operands of a 1-by-1 SX expression (0 for symbols and constants)
static Matrix< Scalar > nan(casadi_int nrow=1, casadi_int ncol=1)
Create a matrix with every nonzero equal to nan.
static Matrix< Scalar > inf(const std::pair< casadi_int, casadi_int > &rc)
Create a matrix with every nonzero equal to +inf.
std::string get_str(bool more=false) const
Get the string representation that disp prints.
Matrix< Scalar > printme(const Matrix< Scalar > &y) const
bool is_leaf() const
Check if SX is a leaf of the SX graph.
std::string name() const
Name of a 1-by-1 symbolic SX primitive.
std::vector< Scalar > get_nonzeros() const
Get all structural nonzeros as a flat list in column-major order (Python: nonzeros())
Helper class for Serialization.
CASADI_EXPORT std::ostream & uout()
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.