binary_sx.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_BINARY_SX_HPP
27 #define CASADI_BINARY_SX_HPP
28 
29 #include "sx_node.hpp"
30 #include "serializing_stream.hpp"
31 
33 namespace casadi {
34 
41 class BinarySX : public SXNode {
42  private:
43 
47  BinarySX(unsigned char op, const SXElem& dep0, const SXElem& dep1) :
48  op_(op), dep0_(dep0), dep1_(dep1) {}
49 
50  public:
51 
55  inline static SXElem create(unsigned char op, const SXElem& dep0, const SXElem& dep1) {
56  if (dep0.is_constant() && dep1.is_constant()) {
57  // Evaluate constant
58  double dep0_val(dep0);
59  double dep1_val(dep1);
60  double ret_val;
61  casadi_math<double>::fun(op, dep0_val, dep1_val, ret_val);
62  return ret_val;
63  } else {
64  // Expression containing free variables
65  return SXElem::create(new BinarySX(op, dep0, dep1));
66  }
67  }
68 
75  ~BinarySX() override {
76  safe_delete(dep0_.assignNoDelete(casadi_limits<SXElem>::nan));
77  safe_delete(dep1_.assignNoDelete(casadi_limits<SXElem>::nan));
78  }
79 
80  // Class name
81  std::string class_name() const override {return "BinarySX";}
82 
83  bool is_smooth() const override { return operation_checker<SmoothChecker>(op_);}
84 
85  bool is_op(casadi_int op) const override { return op_==op; }
86 
90  bool is_equal(const SXNode* node, casadi_int depth) const override {
91  const BinarySX* n = dynamic_cast<const BinarySX*>(node);
92  if (n==nullptr) return false;
93  if (n->op_ != op_) return false;
94  if (SXElem::is_equal(n->dep0_, dep0_, depth-1)
95  && SXElem::is_equal(n->dep1_, dep1_, depth-1)) return true;
96  if (operation_checker<CommChecker>(op_)
97  && SXElem::is_equal(n->dep1_, dep0_, depth-1)
98  && SXElem::is_equal(n->dep0_, dep1_, depth-1)) return true;
99  return false;
100  }
101 
105  casadi_int n_dep() const override { return 2;}
106 
110  const SXElem& dep(casadi_int i) const override { return i==0 ? dep0_ : dep1_;}
111  SXElem& dep(casadi_int i) override { return i==0 ? dep0_ : dep1_;}
112 
116  casadi_int op() const override { return op_;}
117 
121  std::string print(const std::string& arg1, const std::string& arg2) const override {
122  return casadi_math<double>::print(op_, arg1, arg2);
123  }
124 
128  unsigned char op_;
129 
133  SXElem dep0_, dep1_;
134 
135  void serialize_node(SerializingStream& s) const override {
136  s.pack("UnarySX::dep0", dep0_);
137  s.pack("UnarySX::dep1", dep1_);
138  }
139 
143  static SXNode* deserialize(DeserializingStream& s, casadi_int op) {
144  SXElem dep0, dep1;
145  s.unpack("UnarySX::dep0", dep0);
146  s.unpack("UnarySX::dep1", dep1);
147  return new BinarySX(op, dep0, dep1);
148  }
149 };
150 
151 } // namespace casadi
153 
154 #endif // CASADI_BINARY_SX_HPP
The casadi namespace.