26 #ifndef CASADI_CONSTANT_MX_HPP
27 #define CASADI_CONSTANT_MX_HPP
29 #include "mx_node.hpp"
32 #include "serializing_stream.hpp"
59 return create(sp,
static_cast<casadi_int
>(val));
75 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override = 0;
79 casadi_int* iw,
SXElem* w)
const override = 0;
84 void eval_mx(
const std::vector<MX>& arg, std::vector<MX>& res,
85 const std::vector<bool>& unique={})
const override;
90 void ad_forward(
const std::vector<std::vector<MX> >& fseed,
91 std::vector<std::vector<MX> >& fsens)
const override;
96 void ad_reverse(
const std::vector<std::vector<MX> >& aseed,
97 std::vector<std::vector<MX> >& asens)
const override;
102 int sp_forward(
const bvec_t** arg,
bvec_t** res, casadi_int* iw,
bvec_t* w)
const override;
107 int eval_activity(
const bvec_t** arg,
bvec_t** res, casadi_int* iw,
bvec_t* w)
const override;
112 void nonzeros_to_activity(
const double* v,
bvec_t* res)
const;
137 MX get_dot(
const MX& y)
const override;
140 bool __nonzero__()
const override;
145 bool is_valid_input()
const override;
150 casadi_int n_primitives()
const override;
155 void primitives(std::vector<MX>::iterator& it)
const override;
159 void split_primitives_gen(
const T& x,
typename std::vector<T>::iterator& it)
const;
165 void split_primitives(
const MX& x, std::vector<MX>::iterator& it)
const override;
166 void split_primitives(
const SX& x, std::vector<SX>::iterator& it)
const override;
167 void split_primitives(
const DM& x, std::vector<DM>::iterator& it)
const override;
172 T join_primitives_gen(
typename std::vector<T>::const_iterator& it)
const;
178 MX join_primitives(std::vector<MX>::const_iterator& it)
const override;
179 SX join_primitives(std::vector<SX>::const_iterator& it)
const override;
180 DM join_primitives(std::vector<DM>::const_iterator& it)
const override;
219 std::string
disp(
const std::vector<std::string>& arg)
const override {
226 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override {
227 std::copy(x_->begin(), x_->end(), res[0]);
235 casadi_int* iw,
SXElem* w)
const override {
236 std::copy(x_->begin(), x_->end(), res[0]);
244 const std::vector<casadi_int>& arg,
245 const std::vector<casadi_int>& res,
246 const std::vector<bool>& arg_is_ref,
247 std::vector<bool>& res_is_ref)
const override;
253 bool is_one()
const override;
254 bool is_minus_one()
const override;
255 bool is_inf()
const override;
256 bool is_minus_inf()
const override;
257 bool is_half()
const override;
258 bool is_value(
double val)
const override;
259 bool is_nonnegative()
const override;
260 bool is_integer()
const override;
261 bool is_eye()
const override;
267 casadi_int
to_int()
const override {
return static_cast<casadi_int
>(x_.scalar());}
276 nonzeros_to_activity(x_->data(), res[0]);
283 bool is_equal(
const MXNode* node, casadi_int depth)
const override;
325 void codegen_incref(
CodeGenerator& g, std::set<void*>& added)
const override;
330 std::string disp(
const std::vector<std::string>& arg)
const override;
333 double to_double()
const override;
336 casadi_int
to_int()
const override;
344 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override {
345 std::copy(x_.begin(), x_.end(), res[0]);
353 casadi_int* iw,
SXElem* w)
const override {
354 std::copy(x_.begin(), x_.end(), res[0]);
362 nonzeros_to_activity(x_.data(), res[0]);
370 const std::vector<casadi_int>& arg,
371 const std::vector<casadi_int>& res,
372 const std::vector<bool>& arg_is_ref,
373 std::vector<bool>& res_is_ref)
const override;
388 std::vector<double>
x_;
420 std::string disp(
const std::vector<std::string>& arg)
const override;
423 double to_double()
const override;
426 casadi_int
to_int()
const override;
434 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override {
435 if (res[0]) std::copy(x_.begin(), x_.end(), res[0]);
443 nonzeros_to_activity(x_.data(), res[0]);
451 casadi_int* iw,
SXElem* w)
const override {
452 casadi_error(
"eval_sx not supported");
460 const std::vector<casadi_int>& arg,
461 const std::vector<casadi_int>& res,
462 const std::vector<bool>& arg_is_ref,
463 std::vector<bool>& res_is_ref)
const override;
478 std::vector<double>
x_;
523 std::string disp(
const std::vector<std::string>& arg)
const override;
529 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override {
535 casadi_int* iw,
SXElem* w)
const override {
543 const std::vector<casadi_int>& arg,
544 const std::vector<casadi_int>& res,
545 const std::vector<bool>& arg_is_ref,
546 std::vector<bool>& res_is_ref)
const override {}
552 casadi_int
to_int()
const override {
return 0;}
558 MX get_project(
const Sparsity& sp,
bool unique=
false)
const override;
561 MX get_nzref(
const Sparsity& sp,
const std::vector<casadi_int>& nz,
562 bool unique=
false)
const override;
565 MX get_nzassign(
const MX& y,
const std::vector<casadi_int>& nz)
const override;
568 MX get_transpose()
const override;
571 MX get_unary(casadi_int op,
bool unique)
const override;
574 MX _get_binary(casadi_int op,
const MX& y,
bool ScX,
bool ScY,
575 bool unique_x=
false,
bool unique_y=
false)
const override;
578 MX get_reshape(
const Sparsity& sp)
const override;
588 const std::string&
name()
const override {
589 static std::string dummyname;
618 s.
unpack(
"Constant::value", v);
653 template<
typename Value>
673 std::string disp(
const std::vector<std::string>& arg)
const override;
679 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override;
682 int eval_sx(
const SXElem** arg,
SXElem** res, casadi_int* iw,
SXElem* w)
const override;
688 std::fill_n(res[0], nnz(), v_.value!=0 ? ~
static_cast<bvec_t>(0) : 0);
696 const std::vector<casadi_int>& arg,
697 const std::vector<casadi_int>& res,
698 const std::vector<bool>& arg_is_ref,
699 std::vector<bool>& res_is_ref)
const override;
705 bool is_one()
const override;
706 bool is_minus_one()
const override;
707 bool is_half()
const override;
708 bool is_inf()
const override;
709 bool is_minus_inf()
const override;
710 bool is_nonnegative()
const override;
711 bool is_integer()
const override;
712 bool is_eye()
const override;
713 bool is_value(
double val)
const override;
717 return static_cast<double>(v_.value);
722 return static_cast<casadi_int
>(v_.value);
731 MX get_project(
const Sparsity& sp,
bool unique=
false)
const override;
734 MX get_nzref(
const Sparsity& sp,
const std::vector<casadi_int>& nz,
735 bool unique=
false)
const override;
738 MX get_nzassign(
const MX& y,
const std::vector<casadi_int>& nz)
const override;
741 MX get_transpose()
const override;
744 MX get_unary(casadi_int op,
bool unique=
false)
const override;
747 MX _get_binary(casadi_int op,
const MX& y,
bool ScX,
bool ScY,
748 bool unique_x=
false,
bool unique_y=
false)
const override;
751 MX get_reshape(
const Sparsity& sp)
const override;
754 MX get_horzcat(
const std::vector<MX>& x)
const override;
757 MX get_vertcat(
const std::vector<MX>& x)
const override;
762 bool is_equal(
const MXNode* node, casadi_int depth)
const override;
776 template<
typename Value>
781 template<
typename Value>
783 return sparsity().is_dense() && v_.value==1;
786 template<
typename Value>
788 return sparsity().is_dense() && v_.value==-1;
791 template<
typename Value>
793 return sparsity().is_dense() && v_.value==0.5;
796 template<
typename Value>
801 template<
typename Value>
806 template<
typename Value>
811 template<
typename Value>
816 template<
typename Value>
818 return v_.value==1 && sparsity().is_diag();
821 template<
typename Value>
824 return sparsity().is_dense() && v_.value==val;
827 template<
typename Value>
830 s.
pack(
"ConstantMX::type", Value::type_char());
831 v_.serialize_type(s);
834 template<
typename Value>
839 template<
typename Value>
843 template<
typename Value>
847 if (!i->is_value(to_double())) {
854 std::vector<Sparsity> sp;
855 for (
auto&& i : x) sp.push_back(i.sparsity());
856 return MX(horzcat(sp), v_.value,
false);
859 template<
typename Value>
863 if (!i->is_value(to_double())) {
870 std::vector<Sparsity> sp;
871 for (
auto&& i : x) sp.push_back(i.sparsity());
872 return MX(vertcat(sp), v_.value,
false);
875 template<
typename Value>
880 template<
typename Value>
885 template<
typename Value>
890 if (operation_checker<F0XChecker>(op) || sparsity().is_dense()) {
891 return MX(sparsity(), ret);
894 if (
is_zero() && operation_checker<F0XChecker>(op)) {
895 return MX(sparsity(), ret,
false);
897 return repmat(
MX(ret), size1(), size2());
902 return DM(sparsity(), ret,
false)
903 +
DM(sparsity().pattern_inverse(), ret2,
false);
907 template<
typename Value>
909 bool unique_x,
bool unique_y)
const {
910 casadi_assert_dev(sparsity()==y.
sparsity() || ScX || ScY);
912 if (ScX && !operation_checker<FX0Checker>(op)) {
918 MX yy = project(y, f);
919 return MX(f, shared_from_this<MX>())->
_get_binary(op, yy,
false,
false, unique_x, unique_y);
921 }
else if (ScY && !operation_checker<F0XChecker>(op)) {
930 MX xx = project(shared_from_this<MX>(), f);
931 return xx->
_get_binary(op,
MX(f, y),
false,
false, unique_x, unique_y);
937 if (v_.value==0)
return ScY && !y->
is_zero() ? repmat(y, size1(), size2()) : y;
940 if (v_.value==0)
return ScY && !y->
is_zero() ? repmat(-y, size1(), size2()) : -y;
943 if (v_.value==1)
return y;
944 if (v_.value==-1)
return -y;
973 template<
typename Value>
975 std::fill(res[0], res[0]+nnz(), to_double());
979 template<
typename Value>
982 std::fill(res[0], res[0]+nnz(),
SXElem(v_.value));
986 template<
typename Value>
988 const std::vector<casadi_int>& arg,
989 const std::vector<casadi_int>& res,
990 const std::vector<bool>& arg_is_ref,
991 std::vector<bool>& res_is_ref)
const {
994 }
else if (nnz()==1) {
995 g << g.
workel(res[0]) <<
" = " << g.
constant(to_double()) <<
";\n";
997 if (to_double()==0) {
998 g << g.
clear(g.
work(res[0], nnz(),
false), nnz()) <<
'\n';
1000 g << g.
fill(g.
work(res[0], nnz(),
false), nnz(), g.
constant(to_double())) <<
'\n';
1005 template<
typename Value>
1007 bool unique)
const {
1010 for (std::vector<casadi_int>::const_iterator k=nz.begin(); k!=nz.end(); ++k) {
1020 template<
typename Value>
1030 template<
typename Value>
1035 return densify(get_DM());
1041 template<
typename Value>
1044 std::stringstream ss;
1048 ss.setf(std::ios::scientific);
1050 ss.unsetf(std::ios::scientific);
1052 if (sparsity().is_scalar()) {
1054 if (sparsity().nnz()==0) {
1059 }
else if (sparsity().is_empty()) {
1061 sparsity().disp(ss);
1066 }
else if (v_.value==1) {
1068 }
else if (v_.value!=v_.value) {
1070 }
else if (v_.value==std::numeric_limits<double>::infinity()) {
1072 }
else if (v_.value==-std::numeric_limits<double>::infinity()) {
1075 ss <<
"all_" << v_.value <<
"(";
1079 sparsity().disp(ss);
1085 template<
typename Value>
Helper class for C code generation.
std::string fill(const std::string &res, std::size_t n, const std::string &v)
Create a fill operation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
std::string workel(casadi_int n) const
std::string clear(const std::string &res, std::size_t n)
Create a fill operation.
A constant given as a DM.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Matrix< double > x_
data member
casadi_int to_int() const override
Get the value (only for scalar constant nodes)
ConstantDM(const Matrix< double > &x)
Constructor.
~ConstantDM() override
Destructor.
double to_double() const override
Get the value (only for scalar constant nodes)
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Matrix< double > get_DM() const override
Get the value (only for constant nodes)
int eval_activity(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Per-nonzero activity (no DM copy)
A constant to be read from a file.
bool has_refcount() const override
Is reference counting needed in codegen?
int eval_activity(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Per-nonzero activity.
~ConstantFile() override
Destructor.
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
std::vector< double > x_
nonzeros
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
std::string fname_
file to read from
Represents an MX that is only composed of a constant.
void reset_input() const override
Reset the marker for an input expression.
casadi_int to_int() const override=0
Get the value (only for scalar constant nodes)
casadi_int op() const override
Get the operation.
static ConstantMX * create(const Sparsity &sp, int val)
Matrix< double > get_DM() const override=0
Get the value (only for constant nodes)
double to_double() const override=0
Get the value (only for scalar constant nodes)
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override=0
Evaluate the function symbolically (SX)
bool has_duplicates() const override
Detect duplicate symbolic expressions.
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override=0
Evaluate the function numerically.
ConstantMX(DeserializingStream &s)
Deserializing constructor.
A constant to be managed by a pool.
~ConstantPool() override
Destructor.
std::vector< double > x_
nonzeros
int eval_activity(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Per-nonzero activity.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
std::string name_
pool identifier
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
A constant with all entries identical.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
MX get_reshape(const Sparsity &sp) const override
Reshape.
MX get_project(const Sparsity &sp, bool unique=false) const override
Get densification.
bool is_half() const override
Check if identically 0.5.
bool is_nonnegative() const override
Check if not negative.
bool is_eye() const override
Check if identity matrix.
double to_double() const override
Get the value (only for scalar constant nodes)
MX get_nzassign(const MX &y, const std::vector< casadi_int > &nz) const override
Assign the nonzeros of a matrix to another matrix.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Matrix< double > get_DM() const override
Get the value (only for constant nodes)
bool is_zero() const override
Check if a particular integer value.
casadi_int to_int() const override
Get the value (only for scalar constant nodes)
~Constant() override
Destructor.
void serialize_type(SerializingStream &s) const override
Serialize type information.
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
MX _get_binary(casadi_int op, const MX &y, bool ScX, bool ScY, bool unique_x=false, bool unique_y=false) const override
Get a binary operation operation.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz, bool unique=false) const override
Get the nonzeros of matrix.
bool is_value(double val) const override
Check if a certain value.
bool is_equal(const MXNode *node, casadi_int depth) const override
Check if two nodes are equivalent up to a given depth.
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
bool is_one() const override
Check if identically one.
Constant(const Sparsity &sp, Value v=Value())
Constructor.
MX get_unary(casadi_int op, bool unique=false) const override
Get a unary operation.
bool is_minus_one() const override
Check if identically minus one.
MX get_vertcat(const std::vector< MX > &x) const override
Create a vertical concatenation node (vectors only)
bool is_inf() const override
Check if identically inf.
MX get_transpose() const override
Transpose.
int eval_activity(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Activity: uniform value, no DM allocation.
bool is_minus_inf() const override
Check if identically -inf.
MX get_horzcat(const std::vector< MX > &x) const override
Create a horizontal concatenation node.
bool is_integer() const override
Check if integer.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
casadi_int nnz() const
Get the number of (structural) non-zero elements.
casadi_int size2() const
Get the second dimension (i.e. number of columns)
casadi_int size1() const
Get the first dimension (i.e. number of rows)
static MX ones(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries one.
Node class for MX objects.
virtual MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz, bool unique=false) const
Get the nonzeros of matrix.
virtual void serialize_type(SerializingStream &s) const
Serialize type information.
virtual MX get_nzassign(const MX &y, const std::vector< casadi_int > &nz) const
Assign the nonzeros of a matrix to another matrix.
virtual bool is_zero() const
Check if identically zero.
virtual MX get_unary(casadi_int op, bool unique=false) const
Get a unary operation.
virtual MX _get_binary(casadi_int op, const MX &y, bool scX, bool scY, bool unique_x=false, bool unique_y=false) const
Get a binary operation operation (matrix-matrix)
const Sparsity & sparsity() const
Get the sparsity.
virtual bool is_value(double val) const
Check if a certain value.
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
virtual MX get_horzcat(const std::vector< MX > &x) const
Create a horizontal concatenation node.
virtual MX get_project(const Sparsity &sp, bool unique=false) const
Create set sparse.
virtual casadi_int op() const =0
Get the operation.
virtual MX get_vertcat(const std::vector< MX > &x) const
Create a vertical concatenation node (vectors only)
virtual double to_double() const
Get the value (only for scalar constant nodes)
static MX create(MXNode *node)
Create from node.
const Sparsity & sparsity() const
Get the sparsity pattern.
bool is_constant() const
Check if constant.
MXNode * get() const
Get a const pointer to the node.
The basic scalar symbolic class of CasADi.
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
bool is_dense() const
Is dense?
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
DM get_DM() const override
Get the value (only for constant nodes)
static ZeroByZero * getInstance()
Get a pointer to the singleton.
casadi_int to_int() const override
Get the value (only for scalar constant nodes)
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
~ZeroByZero() override
Destructor.
bool is_valid_input() const override
Check if valid function input.
const std::string & name() const override
Get the name.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
double to_double() const override
Get the value (only for scalar constant nodes)
static bool is_inf(const T &val)
static bool is_nonnegative(const T &val)
static bool is_minus_inf(const T &val)
static bool is_integer(const T &val)
bool is_equal(double x, double y, casadi_int depth=0)
template class CASADI_EXPORT Matrix< double >
unsigned long long bvec_t
int to_int(casadi_int rhs)
static CompiletimeConst deserialize(DeserializingStream &s)
void serialize_type(SerializingStream &s) const
Constant known at runtime.
static RuntimeConst deserialize(DeserializingStream &s)
void serialize_type(SerializingStream &s) const
static void fun(unsigned char op, const T &x, const T &y, T &f)
Evaluate a built in function (scalar-scalar)