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>
56 template <
typename Scalar>
inline std::string matrixName()
57 {
return std::string(
"Matrix<") +
typeid(Scalar).name() + std::string(
">");}
58 template<>
inline std::string matrixName<double>() {
return "DM"; }
59 template<>
inline std::string matrixName<casadi_int>() {
return "IM"; }
87 template<
typename Scalar>
90 public SWIG_IF_ELSE(GenericExpressionCommon, GenericExpression<Matrix<Scalar> >),
92 public SWIG_IF_ELSE(PrintableCommon, Printable<Matrix<Scalar> >) {
112 Matrix(casadi_int nrow, casadi_int ncol);
118 explicit Matrix(
const std::pair<casadi_int, casadi_int>& rc);
123 std::vector<Scalar>* operator->();
128 const std::vector<Scalar>* operator->()
const;
146 #if !(defined(SWIG) && defined(SWIGMATLAB))
148 explicit Matrix(
const std::vector< std::vector<double> >& m);
155 nonzeros_(std::vector<Scalar>(x.size())) {
156 auto x_it = x.begin();
157 for (
auto&& d : nonzeros_) d =
static_cast<Scalar
>(*x_it++);
167 Matrix(
const Matrix<A>& x) : sparsity_(x.sparsity()), nonzeros_(std::vector<Scalar>(x.nnz())) {
168 auto x_it = x->begin();
169 for (
auto&& d : nonzeros_) d =
static_cast<Scalar
>(*x_it++);
174 Matrix(
const std::vector<Scalar>& x);
177 Matrix(std::initializer_list<Scalar> x) :
Matrix<Scalar>(std::vector<Scalar>(x)) {}
180 const Scalar scalar()
const;
183 typedef Scalar ScalarType;
186 typedef GenericMatrix<Matrix<Scalar> > B;
219 bool has_nz(casadi_int rr, casadi_int cc)
const;
222 bool __nonzero__()
const;
226 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Slice& rr)
const;
227 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Matrix<casadi_int>& rr)
const;
228 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Sparsity& sp)
const;
233 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
234 const Slice& rr,
const Slice& cc)
const;
235 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
236 const Slice& rr,
const Matrix<casadi_int>& cc)
const;
237 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
238 const Matrix<casadi_int>& rr,
const Slice& cc)
const;
239 void get(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
240 const Matrix<casadi_int>& rr,
const Matrix<casadi_int>& cc)
const;
245 void set(
const Matrix<Scalar>& m,
bool ind1,
const Slice& rr);
246 void set(
const Matrix<Scalar>& m,
bool ind1,
const Matrix<casadi_int>& rr);
247 void set(
const Matrix<Scalar>& m,
bool ind1,
const Sparsity& sp);
252 void set(
const Matrix<Scalar>& m,
bool ind1,
const Slice& rr,
const Slice& cc);
253 void set(
const Matrix<Scalar>& m,
bool ind1,
const Slice& rr,
const Matrix<casadi_int>& cc);
254 void set(
const Matrix<Scalar>& m,
bool ind1,
const Matrix<casadi_int>& rr,
const Slice& cc);
255 void set(
const Matrix<Scalar>& m,
bool ind1,
const Matrix<casadi_int>& rr,
256 const Matrix<casadi_int>& cc);
261 void get_nz(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Slice& k)
const;
262 void get_nz(Matrix<Scalar>& SWIG_OUTPUT(m),
bool ind1,
const Matrix<casadi_int>& k)
const;
267 void set_nz(
const Matrix<Scalar>& m,
bool ind1,
const Slice& k);
268 void set_nz(
const Matrix<Scalar>& m,
bool ind1,
const Matrix<casadi_int>& k);
313 static std::vector<Matrix<Scalar> >
317 static void substitute_inplace(
const std::vector<
Matrix<Scalar> >& v,
323 const std::string& lsolver,
const Dict& opts);
328 const std::string& lsolver,
const Dict& opts);
331 const std::string& lsolver,
const Dict& opts);
335 const std::vector<std::string>& args);
341 const std::string& v_prefix,
342 const std::string& v_suffix);
353 bool short_circuit=
false);
357 bool short_circuit=
false);
361 static std::vector<Matrix<Scalar> > symvar(
const Matrix<Scalar> &x);
376 const Sparsity& sp,
bool intersect=
false);
382 const std::vector<casadi_int>& dim_a,
const std::vector<casadi_int>& dim_b,
383 const std::vector<casadi_int>& dim_c,
384 const std::vector<casadi_int>& a,
const std::vector<casadi_int>& b,
385 const std::vector<casadi_int>& c);
388 const std::vector<casadi_int>& dim_a,
const std::vector<casadi_int>& dim_b,
389 const std::vector<casadi_int>& dim_c,
390 const std::vector<casadi_int>& a,
const std::vector<casadi_int>& b,
391 const std::vector<casadi_int>& c);
394 static std::vector< Matrix<Scalar> > cse(
const std::vector<
Matrix<Scalar> >& e);
401 static std::vector<Matrix<Scalar> >
403 const std::vector<casadi_int>& offset);
405 static std::vector< Matrix<Scalar> >
407 const std::vector<casadi_int>& offset);
408 static std::vector< Matrix<Scalar> >
410 const std::vector<casadi_int>& offset1,
411 const std::vector<casadi_int>& offset2);
443 static std::vector<std::vector<Matrix<Scalar> > >
448 static std::vector<std::vector<Matrix<Scalar> > >
454 casadi_int order=1,
bool tr=
false);
462 const std::vector<casadi_int>& order_contributions);
469 std::vector<casadi_int>& pc,
bool amd=
true);
472 const std::vector<casadi_int>& prinv,
473 const std::vector<casadi_int>& pc,
bool tr=
false);
476 std::vector<casadi_int>& p,
bool amd=
true);
496 #if !defined(SWIG) || defined(DOXYGEN)
541 std::vector<casadi_int>& pc,
bool amd=
true) {
551 const std::vector<casadi_int>& prinv,
552 const std::vector<casadi_int>& pc,
bool tr=
false) {
574 std::vector<casadi_int>& p,
bool amd=
true) {
583 const std::vector<casadi_int>& p) {
728 casadi_int order=5) {
803 const std::vector<casadi_int>& order_contributions) {
870 static std::string type_name();
873 void print_split(std::vector<std::string>& SWIG_OUTPUT(nz),
874 std::vector<std::string>& SWIG_OUTPUT(inter))
const;
877 void disp(std::ostream& stream,
bool more=
false)
const;
883 void print_scalar(std::ostream &stream)
const;
886 void print_vector(std::ostream &stream,
bool truncate=
true)
const;
889 void print_dense(std::ostream &stream,
bool truncate=
true)
const;
892 void print_sparse(std::ostream &stream,
bool truncate=
true)
const;
896 static void print_default(std::ostream &stream,
const Sparsity& sp,
const Scalar* nonzeros,
900 static void print_scalar(std::ostream &stream,
const Scalar& e);
903 static void print_vector(std::ostream &stream,
const Sparsity& sp,
const Scalar* nonzeros,
907 static void print_sparse(std::ostream &stream,
const Sparsity& sp,
const Scalar* nonzeros,
911 static void print_split(casadi_int nnz,
const Scalar* nonzeros, std::vector<std::string>& nz,
912 std::vector<std::string>& inter);
915 static void print_dense(std::ostream &stream,
const Sparsity& sp,
const Scalar* nonzeros,
920 void resize(casadi_int nrow, casadi_int ncol);
922 void reserve(casadi_int nnz, casadi_int ncol);
929 void erase(
const std::vector<casadi_int>& rr,
const std::vector<casadi_int>& cc,
937 void erase(
const std::vector<casadi_int>& rr,
bool ind1=
false);
944 void remove(
const std::vector<casadi_int>& rr,
const std::vector<casadi_int>& cc);
952 void enlarge(casadi_int nrow, casadi_int ncol,
953 const std::vector<casadi_int>& rr,
const std::vector<casadi_int>& cc,
959 std::vector<Scalar>& nonzeros();
960 const std::vector<Scalar>& nonzeros()
const;
966 const Scalar* ptr()
const;
967 friend inline Scalar* get_ptr(
Matrix<Scalar>& v) {
return v.ptr(); }
968 friend inline const Scalar* get_ptr(
const Matrix<Scalar>& v) {
return v.ptr(); }
972 const Sparsity& sparsity()
const;
988 const std::vector<casadi_int>& col,
991 const std::vector<casadi_int>& col,
994 const std::vector<casadi_int>& col,
996 const std::pair<casadi_int, casadi_int>& rc);
1089 bool has_duplicates()
const;
1094 void reset_input()
const;
1145 std::vector<Scalar> get_nonzeros()
const;
1156 template<
typename A>
1157 std::vector<A> get_nonzeros()
const;
1163 explicit operator double()
const;
1168 explicit operator casadi_int()
const;
1174 template<
typename A>
1175 explicit operator std::vector<A>()
const;
1200 static void set_precision(casadi_int precision);
1201 static void set_width(casadi_int width);
1202 static void set_scientific(
bool scientific);
1206 static void rng(casadi_int seed);
1217 const std::pair<casadi_int, casadi_int>& rc);
1243 void serialize(std::ostream &stream)
const;
1284 void to_file(
const std::string& filename,
const std::string& format=
"")
const;
1286 static void to_file(
const std::string& filename,
const Sparsity& sp,
1287 const Scalar* nonzeros,
const std::string& format=
"");
1298 Matrix(
const Sparsity& sp,
const std::vector<Scalar>& d,
bool dummy);
1309 std::vector<Scalar> nonzeros_;
1312 static casadi_int stream_precision_;
1313 static casadi_int stream_width_;
1314 static bool stream_scientific_;
1317 static std::default_random_engine rng_;
1323 template<
typename Scalar>
1324 template<
typename A>
1326 std::vector<A> ret(nnz());
1327 auto r = ret.begin();
1328 for (
auto&& e : nonzeros()) *r++ =
static_cast<A
>(e);
1333 template<
typename Scalar>
1334 template<
typename A>
1337 casadi_int size1 = this->size1(), size2 = this->size2();
1338 const casadi_int *colind = this->colind(), *row = this->row();
1340 auto it = nonzeros().begin();
1341 std::vector<A> ret(numel(), 0);
1342 for (casadi_int cc=0; cc<size2; ++cc) {
1343 for (casadi_int el=colind[cc]; el<colind[cc+1]; ++el) {
1344 ret[row[el] + cc*size1] =
static_cast<A
>(*it++);
Helper class for Serialization.
Sparse matrix class. SX and DM are specializations.
static Matrix< Scalar > rand(const Sparsity &sp)
Create a matrix with uniformly distributed random numbers.
Matrix(const Matrix< A > &x)
Create a matrix from another matrix with a different entry type.
static Matrix< Scalar > nan(const std::pair< casadi_int, casadi_int > &rc)
create a matrix with all nan
bool has_zeros() const
Check if the matrix has any zero entries which are not structural zeros.
static Matrix< Scalar > deserialize(const std::string &s)
Build Sparsity from serialization.
bool is_one() const
check if the matrix is 1 (note that false negative answers are possible)
void remove(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc)
Remove columns and rows.
bool is_regular() const
Checks if expression does not contain NaN or Inf.
Matrix(double val)
This constructor enables implicit type conversion from a numeric type.
Matrix< Scalar > T() const
Transpose the matrix.
static void set_max_depth(casadi_int eq_depth=1)
Set or reset the depth to which equalities are being checked for simplifications.
void resize(casadi_int nrow, casadi_int ncol)
Sparsity get_sparsity() const
Get an owning reference to the sparsity pattern.
static Matrix< Scalar > deserialize(std::istream &stream)
Build Sparsity from serialization.
casadi_int element_hash() const
Returns a number that is unique for a given symbolic scalar.
void erase(const std::vector< casadi_int > &rr, bool ind1=false)
Erase a submatrix (leaving structural zeros in its place)
void serialize(SerializingStream &s) const
Serialize an object.
bool is_commutative() const
Check whether a binary SX is commutative.
bool is_symbolic() const
Check if symbolic (Dense)
Matrix(const Sparsity &sp)
Create a sparse matrix from a sparsity pattern.
static std::vector< Matrix< Scalar > > get_free(const Function &f)
Get free.
Matrix(casadi_int nrow, casadi_int ncol)
Create a sparse matrix with all structural zeros.
static casadi_int get_max_depth()
Get the depth to which equalities are being checked for simplifications.
Matrix< Scalar > operator+() const
bool is_smooth() const
Check if smooth.
static Matrix< Scalar > nan(const Sparsity &sp)
create a matrix with all nan
static Matrix< Scalar > triplet(const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d, casadi_int nrow, casadi_int ncol)
Construct a sparse matrix from triplet form.
bool is_minus_one() const
check if the matrix is -1 (note that false negative answers are possible)
void to_file(const std::string &filename, const std::string &format="") const
static Matrix< Scalar > triplet(const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d)
Construct a sparse matrix from triplet form.
void disp(std::ostream &stream, bool more=false) const
Print a representation of the object.
static Matrix< Scalar > rand(casadi_int nrow=1, casadi_int ncol=1)
Create a matrix with uniformly distributed random numbers.
bool is_eye() const
check if the matrix is an identity matrix (note that false negative answers
bool is_op(casadi_int op) const
Is it a certain operation.
void erase(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
Erase a submatrix (leaving structural zeros in its place)
static Matrix< Scalar > inf(casadi_int nrow=1, casadi_int ncol=1)
create a matrix with all inf
std::vector< Scalar > get_elements() const
Get all elements.
static Matrix< Scalar > triplet(const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d, const std::pair< casadi_int, casadi_int > &rc)
Construct a sparse matrix from triplet form.
Matrix(const std::vector< std::vector< double > > &m)
Dense matrix constructor with data given as vector of vectors.
static std::vector< Matrix< Scalar > > get_input(const Function &f)
Get function input.
Matrix< Scalar > dep(casadi_int ch=0) const
Get expressions of the children of the expression.
bool is_zero() const
check if the matrix is 0 (note that false negative answers are possible)
static Matrix< Scalar > inf(const Sparsity &sp)
create a matrix with all inf
std::string serialize() const
Serialize.
void enlarge(casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
Enlarge matrix.
static Matrix< Scalar > deserialize(DeserializingStream &s)
bool is_constant() const
Check if the matrix is constant (note that false negative answers are possible)
Matrix(const std::vector< A > &x)
Create an expression from a vector.
void reserve(casadi_int nnz)
void export_code(const std::string &lang, std::ostream &stream=casadi::uout(), const Dict &options=Dict()) const
Export matrix in specific language.
casadi_int op() const
Get operation type.
void reserve(casadi_int nnz, casadi_int ncol)
static Matrix< Scalar > eye(casadi_int n)
create an n-by-n identity matrix
bool is_valid_input() const
Check if matrix can be used to define function inputs.
Matrix(const Sparsity &sp, const Matrix< Scalar > &d)
Construct matrix with a given sparsity and nonzeros.
static Matrix< Scalar > rand(const std::pair< casadi_int, casadi_int > &rc)
Create a matrix with uniformly distributed random numbers.
bool is_integer() const
Check if the matrix is integer-valued.
Matrix< Scalar > operator-() const
casadi_int n_dep() const
Get the number of dependencies of a binary SXElem.
static Matrix< Scalar > nan(casadi_int nrow=1, casadi_int ncol=1)
create a matrix with all nan
static Matrix< Scalar > inf(const std::pair< casadi_int, casadi_int > &rc)
create a matrix with all inf
std::string get_str(bool more=false) const
Get string representation.
Matrix< Scalar > printme(const Matrix< Scalar > &y) const
bool is_leaf() const
Check if SX is a leaf of the SX graph.
std::string name() const
Get name (only if symbolic scalar)
std::vector< Scalar > get_nonzeros() const
Get all nonzeros.
static Matrix< double > from_file(const std::string &filename, const std::string &format_hint="")
Helper class for Serialization.
CASADI_EXPORT std::ostream & uout()
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.