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>
39 BinaryMX<ScX, ScY>::BinaryMX(Operation op,
const MX& x,
const MX& y) : op_(op) {
42 set_sparsity(y.sparsity());
44 set_sparsity(x.sparsity());
48 template<
bool ScX,
bool ScY>
49 BinaryMX<ScX, ScY>::~BinaryMX() {
52 template<
bool ScX,
bool ScY>
53 std::string BinaryMX<ScX, ScY>::disp(
const std::vector<std::string>& arg)
const {
54 return casadi_math<double>::print(op_, arg.at(0), arg.at(1));
57 template<
bool ScX,
bool ScY>
58 void BinaryMX<ScX, ScY>::eval_mx(
const std::vector<MX>& arg, std::vector<MX>& res,
59 const std::vector<bool>& unique)
const {
60 bool unique_x = !unique.empty() && unique[0];
61 bool unique_y = unique.size() >= 2 && unique[1];
62 res[0] = MX::binary(op_, arg[0], arg[1], unique_x, unique_y);
65 template<
bool ScX,
bool ScY>
66 void BinaryMX<ScX, ScY>::eval_linear(
const std::vector<std::array<MX, 3> >& arg,
67 std::vector<std::array<MX, 3> >& res)
const {
68 casadi_math<MX>::fun_linear(op_, arg[0].data(), arg[1].data(), res[0].data());
71 template<
bool ScX,
bool ScY>
72 void BinaryMX<ScX, ScY>::ad_forward(
const std::vector<std::vector<MX> >& fseed,
73 std::vector<std::vector<MX> >& fsens)
const {
76 casadi_math<MX>::der(op_, dep(0), dep(1), shared_from_this<MX>(), pd);
79 for (casadi_int d=0; d<fsens.size(); ++d) {
80 if (op_ == OP_IF_ELSE_ZERO) {
81 fsens[d][0] = if_else_zero(pd[1], fseed[d][1]);
83 fsens[d][0] = pd[0]*fseed[d][0] + pd[1]*fseed[d][1];
88 template<
bool ScX,
bool ScY>
89 void BinaryMX<ScX, ScY>::ad_reverse(
const std::vector<std::vector<MX> >& aseed,
90 std::vector<std::vector<MX> >& asens)
const {
93 casadi_math<MX>::der(op_, dep(0), dep(1), shared_from_this<MX>(), pd);
96 for (casadi_int d=0; d<aseed.size(); ++d) {
98 if (op_ == OP_IF_ELSE_ZERO) {
100 if (!s.is_scalar() && dep(1).is_scalar()) {
101 asens[d][1] += dot(dep(0), s);
103 asens[d][1] += if_else_zero(dep(0), s);
107 for (casadi_int c=0; c<2; ++c) {
112 if (!t.is_scalar() && t.size() != dep(c).size()) {
113 if (pd[c].size()!=s.size()) pd[c] = MX(s.sparsity(), pd[c]);
124 template<
bool ScX,
bool ScY>
125 void BinaryMX<ScX, ScY>::
126 generate(CodeGenerator& g,
127 const std::vector<casadi_int>& arg,
const std::vector<casadi_int>& res,
128 const std::vector<bool>& arg_is_ref, std::vector<bool>& res_is_ref)
const {
130 if (nnz()==0)
return;
139 inplace = res[0]==arg[0] && !arg_is_ref[0];
147 std::string r = g.workel(res[0]);
148 std::string x = g.workel(arg[0]);
149 std::string y = g.workel(arg[1]);
152 if (op_==OP_DIV && g.codegen_scalars && dep(1).nnz()==1) {
159 g.local(
"rr",
"casadi_real",
"*");
160 g.local(
"i",
"casadi_int");
161 g <<
"for (i=0, " <<
"rr=" << g.work(res[0], nnz(),
false);
165 if (!ScX && !inplace) {
166 g.local(
"cr",
"const casadi_real",
"*");
167 g <<
", cr=" << g.work(arg[0], dep(0).nnz(), arg_is_ref[0]);
168 if (op_==OP_OR || op_==OP_AND) {
179 g.local(
"cs",
"const casadi_real",
"*");
180 g <<
", cs=" << g.work(arg[1], dep(1).nnz(), arg_is_ref[1]);
181 if (op_==OP_OR || op_==OP_AND || op_==OP_IF_ELSE_ZERO) {
190 g <<
"; i<" << nnz() <<
"; ++i) ";
196 g << casadi_math<double>::sep(op_) <<
"= " << y;
198 g <<
" = " << g.print_op(op_, x, y);
203 template<
bool ScX,
bool ScY>
204 int BinaryMX<ScX, ScY>::
205 eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const {
206 return eval_gen<double>(arg, res, iw, w);
209 template<
bool ScX,
bool ScY>
210 int BinaryMX<ScX, ScY>::
211 eval_sx(
const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w)
const {
212 return eval_gen<SXElem>(arg, res, iw, w);
215 template<
bool ScX,
bool ScY>
217 int BinaryMX<ScX, ScY>::
218 eval_gen(
const T*
const* arg, T*
const* res, casadi_int* iw, T* w)
const {
221 const T* input0 = arg[0];
222 const T* input1 = arg[1];
225 casadi_math<T>::fun(op_, input0, input1, output0, nnz());
227 casadi_math<T>::fun(op_, *input0, input1, output0, nnz());
229 casadi_math<T>::fun(op_, input0, *input1, output0, nnz());
234 template<
bool ScX,
bool ScY>
235 int BinaryMX<ScX, ScY>::
236 sp_forward(
const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w)
const {
237 const bvec_t *a0=arg[0], *a1=arg[1];
240 for (casadi_int i=0; i<n; ++i) {
243 else if (ScX && !ScY)
245 else if (!ScX && ScY)
248 *r++ = *a0++ | *a1++;
253 template<
bool ScX,
bool ScY>
254 int BinaryMX<ScX, ScY>::
255 eval_activity(
const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w)
const {
257 const bool f00 = operation_checker<F00Checker>(op_);
258 const bool f0x = operation_checker<F0XChecker>(op_);
259 const bool fx0 = operation_checker<FX0Checker>(op_);
260 const bvec_t nz = ~static_cast<bvec_t>(0);
261 const bvec_t *a0=arg[0], *a1=arg[1];
264 for (casadi_int i=0; i<n; ++i) {
265 const bool z0 = (*a0)!=0;
266 const bool z1 = (*a1)!=0;
268 if (!z0 && !z1) out = f00 ? 0 : nz;
269 else if (!z0 && z1) out = f0x ? 0 : nz;
270 else if ( z0 && !z1) out = fx0 ? 0 : nz;
279 template<
bool ScX,
bool ScY>
280 int BinaryMX<ScX, ScY>::
281 sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w)
const {
282 bvec_t *a0=arg[0], *a1=arg[1], *r = res[0];
284 for (casadi_int i=0; i<n; ++i) {
299 template<
bool ScX,
bool ScY>
300 MX BinaryMX<ScX, ScY>::_get_binary(casadi_int op,
const MX& y,
bool scX,
bool scY,
301 bool unique_x,
bool unique_y)
const {
302 if (!GlobalOptions::simplification_on_the_fly)
303 return MXNode::_get_binary(op, y, scX, scY, unique_x, unique_y);
307 if (op==OP_SUB && MX::is_equal(y, dep(0), maxDepth()))
return dep(1);
308 if (op==OP_SUB && MX::is_equal(y, dep(1), maxDepth()))
return dep(0);
311 if (op==OP_SUB && MX::is_equal(y, dep(0), maxDepth()))
return -dep(1);
312 if (op==OP_ADD && MX::is_equal(y, dep(1), maxDepth()))
return dep(0);
318 return MXNode::_get_binary(op, y, scX, scY, unique_x, unique_y);
321 template<
bool ScX,
bool ScY>
322 void BinaryMX<ScX, ScY>::serialize_body(SerializingStream& s)
const {
323 MXNode::serialize_body(s);
324 s.pack(
"BinaryMX::op",
static_cast<int>(op_));
327 template<
bool ScX,
bool ScY>
328 void BinaryMX<ScX, ScY>::serialize_type(SerializingStream& s)
const {
329 MXNode::serialize_type(s);
332 char type = type_x | (type_y << 1);
333 s.pack(
"BinaryMX::scalar_flags", type);
336 template<
bool ScX,
bool ScY>
337 MXNode* BinaryMX<ScX, ScY>::deserialize(DeserializingStream& s) {
339 s.unpack(
"BinaryMX::scalar_flags", t);
344 if (scY)
return new BinaryMX<true, true>(s);
345 return new BinaryMX<true, false>(s);
347 if (scY)
return new BinaryMX<false, true>(s);
348 return new BinaryMX<false, false>(s);
352 template<
bool ScX,
bool ScY>
353 BinaryMX<ScX, ScY>::BinaryMX(DeserializingStream& s) : MXNode(s) {
355 s.unpack(
"BinaryMX::op", op);
359 template<
bool ScX,
bool ScY>
360 MX BinaryMX<ScX, ScY>::get_solve_triu(
const MX& r,
bool tr)
const {
362 if (!ScX && !ScY && op_ == OP_SUB) {
364 if (dep(0).is_op(OP_PROJECT) && dep(0).dep(0).is_eye()) {
366 if (dep(1).is_op(OP_PROJECT) && dep(1).dep(0).sparsity().is_triu(
true)) {
367 return dep(1).dep(0)->get_solve_triu_unity(r, tr);
372 return MXNode::get_solve_triu(r, tr);
375 template<
bool ScX,
bool ScY>
376 MX BinaryMX<ScX, ScY>::get_solve_tril(
const MX& r,
bool tr)
const {
378 if (!ScX && !ScY && op_ == OP_SUB) {
380 if (dep(0).is_op(OP_PROJECT) && dep(0).dep(0).is_eye()) {
382 if (dep(1).is_op(OP_PROJECT) && dep(1).dep(0).sparsity().is_tril(
true)) {
383 return dep(1).dep(0)->get_solve_tril_unity(r, tr);
388 return MXNode::get_solve_tril(r, tr);