26 #ifndef CASADI_BINARY_MX_IMPL_HPP
27 #define CASADI_BINARY_MX_IMPL_HPP
29 #include "binary_mx.hpp"
30 #include "casadi_misc.hpp"
31 #include "global_options.hpp"
32 #include "serializing_stream.hpp"
38 template<
bool ScX,
bool ScY>
48 template<
bool ScX,
bool ScY>
52 template<
bool ScX,
bool ScY>
57 template<
bool ScX,
bool ScY>
62 template<
bool ScX,
bool ScY>
64 std::vector<std::array<MX, 3> >& res)
const {
68 template<
bool ScX,
bool ScY>
70 std::vector<std::vector<MX> >& fsens)
const {
76 for (casadi_int d=0; d<fsens.size(); ++d) {
80 fsens[d][0] = pd[0]*fseed[d][0] + pd[1]*fseed[d][1];
85 template<
bool ScX,
bool ScY>
87 std::vector<std::vector<MX> >& asens)
const {
93 for (casadi_int d=0; d<aseed.size(); ++d) {
97 if (!s.
is_scalar() && dep(1).is_scalar()) {
98 asens[d][1] +=
dot(dep(0), s);
104 for (casadi_int c=0; c<2; ++c) {
110 if (pd[c].size()!=s.
size()) pd[c] =
MX(s.sparsity(), pd[c]);
121 template<
bool ScX,
bool ScY>
124 const std::vector<casadi_int>& arg,
const std::vector<casadi_int>& res,
125 const std::vector<bool>& arg_is_ref, std::vector<bool>& res_is_ref)
const {
127 if (nnz()==0)
return;
136 inplace = res[0]==arg[0] && !arg_is_ref[0];
144 std::string r = g.
workel(res[0]);
145 std::string x = g.
workel(arg[0]);
146 std::string y = g.
workel(arg[1]);
156 g.
local(
"rr",
"casadi_real",
"*");
157 g.
local(
"i",
"casadi_int");
158 g <<
"for (i=0, " <<
"rr=" << g.
work(res[0], nnz(),
false);
162 if (!ScX && !inplace) {
163 g.
local(
"cr",
"const casadi_real",
"*");
164 g <<
", cr=" << g.
work(arg[0], dep(0).nnz(), arg_is_ref[0]);
176 g.
local(
"cs",
"const casadi_real",
"*");
177 g <<
", cs=" << g.
work(arg[1], dep(1).nnz(), arg_is_ref[1]);
187 g <<
"; i<" << nnz() <<
"; ++i) ";
193 g << casadi_math<double>::sep(op_) <<
"= " << y;
195 g <<
" = " << g.
print_op(op_, x, y);
200 template<
bool ScX,
bool ScY>
202 eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const {
203 return eval_gen<double>(arg, res, iw, w);
206 template<
bool ScX,
bool ScY>
209 return eval_gen<SXElem>(arg, res, iw, w);
212 template<
bool ScX,
bool ScY>
215 eval_gen(
const T*
const* arg, T*
const* res, casadi_int* iw, T* w)
const {
218 const T* input0 = arg[0];
219 const T* input1 = arg[1];
231 template<
bool ScX,
bool ScY>
234 const bvec_t *a0=arg[0], *a1=arg[1];
237 for (casadi_int i=0; i<n; ++i) {
240 else if (ScX && !ScY)
242 else if (!ScX && ScY)
245 *r++ = *a0++ | *a1++;
250 template<
bool ScX,
bool ScY>
253 bvec_t *a0=arg[0], *a1=arg[1], *r = res[0];
255 for (casadi_int i=0; i<n; ++i) {
270 template<
bool ScX,
bool ScY>
280 template<
bool ScX,
bool ScY>
300 template<
bool ScX,
bool ScY>
303 s.
pack(
"BinaryMX::op",
static_cast<int>(op_));
306 template<
bool ScX,
bool ScY>
311 char type = type_x | (type_y << 1);
312 s.
pack(
"BinaryMX::scalar_flags", type);
315 template<
bool ScX,
bool ScY>
318 s.
unpack(
"BinaryMX::scalar_flags", t);
331 template<
bool ScX,
bool ScY>
338 template<
bool ScX,
bool ScY>
341 if (!ScX && !ScY && op_ ==
OP_SUB) {
343 if (dep(0).is_op(
OP_PROJECT) && dep(0).dep(0).is_eye()) {
345 if (dep(1).is_op(
OP_PROJECT) && dep(1).dep(0).sparsity().is_triu(
true)) {
354 template<
bool ScX,
bool ScY>
357 if (!ScX && !ScY && op_ ==
OP_SUB) {
359 if (dep(0).is_op(
OP_PROJECT) && dep(0).dep(0).is_eye()) {
361 if (dep(1).is_op(
OP_PROJECT) && dep(1).dep(0).sparsity().is_tril(
true)) {
Represents any binary operation that involves two matrices.
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
~BinaryMX() override
Destructor.
casadi_int op() const override
Get the operation.
static MXNode * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
MX get_solve_tril(const MX &r, bool tr) const override
Solve a system of linear equations, lower triangular A.
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
MX get_unary(casadi_int op) const override
Get a unary operation.
MX _get_binary(casadi_int op, const MX &y, bool scX, bool scY) const override
Get a binary operation operation.
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
MX get_solve_triu(const MX &r, bool tr) const override
Solve a system of linear equations, upper triangular A.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
BinaryMX(Operation op, const MX &x, const MX &y)
Constructor.
int eval_gen(const T *const *arg, T *const *res, casadi_int *iw, T *w) const
Evaluate the function (template)
void eval_linear(const std::vector< std::array< MX, 3 > > &arg, std::vector< std::array< MX, 3 > > &res) const override
Evaluate the MX node on a const/linear/nonlinear partition.
void serialize_type(SerializingStream &s) const override
Serialize type information.
void serialize_body(SerializingStream &s) const override
Serialize an object without 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.
Helper class for C code generation.
bool codegen_scalars
Codegen scalar.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string print_op(casadi_int op, const std::string &a0)
Print an operation to a c file.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
std::string workel(casadi_int n) const
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
std::pair< casadi_int, casadi_int > size() const
Get the shape.
bool is_scalar(bool scalar_and_dense=false) const
Check if the matrix expression is scalar.
static bool simplification_on_the_fly
Indicates whether simplifications should be made on the fly.
Node class for MX objects.
virtual void serialize_type(SerializingStream &s) const
Serialize type information.
virtual MX get_solve_triu(const MX &r, bool tr) const
Solve a system of linear equations, upper triangular A.
virtual MX get_solve_tril_unity(const MX &r, bool tr) const
Solve a system of linear equations, lower triangular A, unity diagnal.
virtual MX get_solve_tril(const MX &r, bool tr) const
Solve a system of linear equations, lower triangular A.
virtual MX get_unary(casadi_int op) const
Get a unary operation.
virtual MX get_solve_triu_unity(const MX &r, bool tr) const
Solve a system of linear equations, upper triangular A, unity diagonal.
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
void set_dep(const MX &dep)
Set unary dependency.
virtual MX _get_binary(casadi_int op, const MX &y, bool scX, bool scY) const
Get a binary operation operation (matrix-matrix)
const Sparsity & sparsity() const
Get the sparsity pattern.
static bool is_equal(const MX &x, const MX &y, casadi_int depth=0)
MX dep(casadi_int ch=0) const
Get the nth dependency as MX.
The basic scalar symbolic class of CasADi.
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
double if_else_zero(double x, double y)
Conditional assignment.
unsigned long long bvec_t
T dot(const std::vector< T > &a, const std::vector< T > &b)
Operation
Enum for quick access to any node.
static void der(unsigned char op, const T &x, const T &y, const T &f, T *d)
Evaluate a built in derivative function.
static void fun_linear(unsigned char op, const T *x, const T *y, T *f)
Evaluate function on a const/linear/nonlinear partition.
static std::string print(unsigned char op, const std::string &x, const std::string &y)
Print.
static void fun(unsigned char op, const T &x, const T &y, T &f)
Evaluate a built in function (scalar-scalar)