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)
const override;
89 void ad_forward(
const std::vector<std::vector<MX> >& fseed,
90 std::vector<std::vector<MX> >& fsens)
const override;
95 void ad_reverse(
const std::vector<std::vector<MX> >& aseed,
96 std::vector<std::vector<MX> >& asens)
const override;
101 int sp_forward(
const bvec_t** arg,
bvec_t** res, casadi_int* iw,
bvec_t* w)
const override;
123 MX get_dot(
const MX& y)
const override;
126 bool __nonzero__()
const override;
131 bool is_valid_input()
const override;
136 casadi_int n_primitives()
const override;
141 void primitives(std::vector<MX>::iterator& it)
const override;
145 void split_primitives_gen(
const T& x,
typename std::vector<T>::iterator& it)
const;
151 void split_primitives(
const MX& x, std::vector<MX>::iterator& it)
const override;
152 void split_primitives(
const SX& x, std::vector<SX>::iterator& it)
const override;
153 void split_primitives(
const DM& x, std::vector<DM>::iterator& it)
const override;
158 T join_primitives_gen(
typename std::vector<T>::const_iterator& it)
const;
164 MX join_primitives(std::vector<MX>::const_iterator& it)
const override;
165 SX join_primitives(std::vector<SX>::const_iterator& it)
const override;
166 DM join_primitives(std::vector<DM>::const_iterator& it)
const override;
205 std::string
disp(
const std::vector<std::string>& arg)
const override {
212 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override {
213 std::copy(x_->begin(), x_->end(), res[0]);
221 casadi_int* iw,
SXElem* w)
const override {
222 std::copy(x_->begin(), x_->end(), res[0]);
230 const std::vector<casadi_int>& arg,
231 const std::vector<casadi_int>& res,
232 const std::vector<bool>& arg_is_ref,
233 std::vector<bool>& res_is_ref)
const override;
239 bool is_one()
const override;
240 bool is_minus_one()
const override;
241 bool is_eye()
const override;
252 bool is_equal(
const MXNode* node, casadi_int depth)
const override;
289 void codegen_incref(
CodeGenerator& g, std::set<void*>& added)
const override;
294 std::string disp(
const std::vector<std::string>& arg)
const override;
297 double to_double()
const override;
305 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override {
306 std::copy(x_.begin(), x_.end(), res[0]);
314 casadi_int* iw,
SXElem* w)
const override {
315 std::copy(x_.begin(), x_.end(), res[0]);
323 const std::vector<casadi_int>& arg,
324 const std::vector<casadi_int>& res,
325 const std::vector<bool>& arg_is_ref,
326 std::vector<bool>& res_is_ref)
const override;
341 std::vector<double>
x_;
373 std::string disp(
const std::vector<std::string>& arg)
const override;
376 double to_double()
const override;
384 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override {
385 if (res[0]) std::copy(x_.begin(), x_.end(), res[0]);
393 casadi_int* iw,
SXElem* w)
const override {
394 casadi_error(
"eval_sx not supported");
402 const std::vector<casadi_int>& arg,
403 const std::vector<casadi_int>& res,
404 const std::vector<bool>& arg_is_ref,
405 std::vector<bool>& res_is_ref)
const override;
420 std::vector<double>
x_;
465 std::string disp(
const std::vector<std::string>& arg)
const override;
471 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override {
477 casadi_int* iw,
SXElem* w)
const override {
485 const std::vector<casadi_int>& arg,
486 const std::vector<casadi_int>& res,
487 const std::vector<bool>& arg_is_ref,
488 std::vector<bool>& res_is_ref)
const override {}
497 MX get_project(
const Sparsity& sp)
const override;
500 MX get_nzref(
const Sparsity& sp,
const std::vector<casadi_int>& nz)
const override;
503 MX get_nzassign(
const MX& y,
const std::vector<casadi_int>& nz)
const override;
506 MX get_transpose()
const override;
509 MX get_unary(casadi_int op)
const override;
512 MX _get_binary(casadi_int op,
const MX& y,
bool ScX,
bool ScY)
const override;
515 MX get_reshape(
const Sparsity& sp)
const override;
525 const std::string&
name()
const override {
526 static std::string dummyname;
555 s.
unpack(
"Constant::value", v);
590 template<
typename Value>
610 std::string disp(
const std::vector<std::string>& arg)
const override;
616 int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const override;
619 int eval_sx(
const SXElem** arg,
SXElem** res, casadi_int* iw,
SXElem* w)
const override;
625 const std::vector<casadi_int>& arg,
626 const std::vector<casadi_int>& res,
627 const std::vector<bool>& arg_is_ref,
628 std::vector<bool>& res_is_ref)
const override;
633 bool is_zero()
const override {
return v_.value==0;}
634 bool is_one()
const override {
return v_.value==1;}
635 bool is_eye()
const override {
return v_.value==1 && sparsity().is_diag();}
636 bool is_value(
double val)
const override {
return v_.value==val;}
640 return static_cast<double>(v_.value);
649 MX get_project(
const Sparsity& sp)
const override;
652 MX get_nzref(
const Sparsity& sp,
const std::vector<casadi_int>& nz)
const override;
655 MX get_nzassign(
const MX& y,
const std::vector<casadi_int>& nz)
const override;
658 MX get_transpose()
const override;
661 MX get_unary(casadi_int op)
const override;
664 MX _get_binary(casadi_int op,
const MX& y,
bool ScX,
bool ScY)
const override;
667 MX get_reshape(
const Sparsity& sp)
const override;
670 MX get_horzcat(
const std::vector<MX>& x)
const override;
673 MX get_vertcat(
const std::vector<MX>& x)
const override;
678 bool is_equal(
const MXNode* node, casadi_int depth)
const override;
692 template<
typename Value>
695 s.
pack(
"ConstantMX::type", Value::type_char());
696 v_.serialize_type(s);
699 template<
typename Value>
704 template<
typename Value>
708 template<
typename Value>
712 if (!i->is_value(to_double())) {
719 std::vector<Sparsity> sp;
720 for (
auto&& i : x) sp.push_back(i.sparsity());
721 return MX(horzcat(sp), v_.value,
false);
724 template<
typename Value>
728 if (!i->is_value(to_double())) {
735 std::vector<Sparsity> sp;
736 for (
auto&& i : x) sp.push_back(i.sparsity());
737 return MX(vertcat(sp), v_.value,
false);
740 template<
typename Value>
745 template<
typename Value>
750 template<
typename Value>
755 if (operation_checker<F0XChecker>(op) || sparsity().is_dense()) {
756 return MX(sparsity(), ret);
759 if (
is_zero() && operation_checker<F0XChecker>(op)) {
760 return MX(sparsity(), ret,
false);
762 return repmat(
MX(ret), size1(), size2());
767 return DM(sparsity(), ret,
false)
768 +
DM(sparsity().pattern_inverse(), ret2,
false);
772 template<
typename Value>
774 casadi_assert_dev(sparsity()==y.
sparsity() || ScX || ScY);
776 if (ScX && !operation_checker<FX0Checker>(op)) {
782 MX yy = project(y, f);
783 return MX(f, shared_from_this<MX>())->
_get_binary(op, yy,
false,
false);
785 }
else if (ScY && !operation_checker<F0XChecker>(op)) {
794 MX xx = project(shared_from_this<MX>(), f);
801 if (v_.value==0)
return ScY && !y->
is_zero() ? repmat(y, size1(), size2()) : y;
804 if (v_.value==0)
return ScY && !y->
is_zero() ? repmat(-y, size1(), size2()) : -y;
807 if (v_.value==1)
return y;
808 if (v_.value==-1)
return -y;
837 template<
typename Value>
839 std::fill(res[0], res[0]+nnz(), to_double());
843 template<
typename Value>
846 std::fill(res[0], res[0]+nnz(),
SXElem(v_.value));
850 template<
typename Value>
852 const std::vector<casadi_int>& arg,
853 const std::vector<casadi_int>& res,
854 const std::vector<bool>& arg_is_ref,
855 std::vector<bool>& res_is_ref)
const {
858 }
else if (nnz()==1) {
859 g << g.
workel(res[0]) <<
" = " << g.
constant(to_double()) <<
";\n";
861 if (to_double()==0) {
862 g << g.
clear(g.
work(res[0], nnz(),
false), nnz()) <<
'\n';
864 g << g.
fill(g.
work(res[0], nnz(),
false), nnz(), g.
constant(to_double())) <<
'\n';
869 template<
typename Value>
873 for (std::vector<casadi_int>::const_iterator k=nz.begin(); k!=nz.end(); ++k) {
883 template<
typename Value>
893 template<
typename Value>
898 return densify(get_DM());
904 template<
typename Value>
907 std::stringstream ss;
908 if (sparsity().is_scalar()) {
910 if (sparsity().nnz()==0) {
915 }
else if (sparsity().is_empty()) {
922 }
else if (v_.value==1) {
924 }
else if (v_.value!=v_.value) {
926 }
else if (v_.value==std::numeric_limits<double>::infinity()) {
928 }
else if (v_.value==-std::numeric_limits<double>::infinity()) {
931 ss <<
"all_" << v_.value <<
"(";
941 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
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)
A constant to be read from a file.
~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 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_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_binary(casadi_int op, const MX &y, bool ScX, bool ScY) const override
Get a binary operation operation.
MX get_reshape(const Sparsity &sp) const override
Reshape.
MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz) const override
Get the nonzeros of matrix.
bool is_eye() const override
Check if identity matrix.
double to_double() const override
Get the value (only for scalar constant nodes)
MX get_unary(casadi_int op) const override
Get a unary operation.
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.
~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.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
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_project(const Sparsity &sp) const override
Get densification.
MX get_vertcat(const std::vector< MX > &x) const override
Create a vertical concatenation node (vectors only)
MX get_transpose() const override
Transpose.
MX get_horzcat(const std::vector< MX > &x) const override
Create a horizontal concatenation node.
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 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) const
Get a unary operation.
const Sparsity & sparsity() const
Get the sparsity.
virtual MX get_project(const Sparsity &sp) const
Create set sparse.
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 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 MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz) const
Get the nonzeros of matrix.
virtual MX _get_binary(casadi_int op, const MX &y, bool scX, bool scY) const
Get a binary operation operation (matrix-matrix)
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.
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)
bool is_equal(double x, double y, casadi_int depth=0)
template class CASADI_EXPORT Matrix< double >
unsigned long long bvec_t
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)