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 {
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 
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
Represents a basic binary operation on two SXElem nodes.
Definition: binary_sx.hpp:41
void serialize_node(SerializingStream &s) const override
Definition: binary_sx.hpp:135
bool is_smooth() const override
Check if smooth.
Definition: binary_sx.hpp:83
bool is_op(casadi_int op) const override
check properties of a node
Definition: binary_sx.hpp:85
static SXNode * deserialize(DeserializingStream &s, casadi_int op)
Deserialize without type information.
Definition: binary_sx.hpp:143
bool is_equal(const SXNode *node, casadi_int depth) const override
Check if two nodes are equivalent up to a given depth.
Definition: binary_sx.hpp:90
SXElem dep0_
The dependencies of the node.
Definition: binary_sx.hpp:133
casadi_int n_dep() const override
Number of dependencies.
Definition: binary_sx.hpp:105
unsigned char op_
The binary operation as an 1 byte integer (allows 256 values)
Definition: binary_sx.hpp:128
SXElem & dep(casadi_int i) override
get the reference of a child
Definition: binary_sx.hpp:111
std::string class_name() const override
Get type name.
Definition: binary_sx.hpp:81
const SXElem & dep(casadi_int i) const override
get the reference of a dependency
Definition: binary_sx.hpp:110
~BinarySX() override
Destructor.
Definition: binary_sx.hpp:75
std::string print(const std::string &arg1, const std::string &arg2) const override
Print expression.
Definition: binary_sx.hpp:121
static SXElem create(unsigned char op, const SXElem &dep0, const SXElem &dep1)
Create a binary expression.
Definition: binary_sx.hpp:55
casadi_int op() const override
Get the operation.
Definition: binary_sx.hpp:116
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
static SXElem create(SXNode *node)
Definition: sx_elem.cpp:62
bool is_constant() const
Definition: sx_elem.cpp:457
SXNode * assignNoDelete(const SXElem &scalar)
Assign the node to something, without invoking the deletion of the node,.
Definition: sx_elem.cpp:118
static bool is_equal(const SXElem &x, const SXElem &y, casadi_int depth=0)
Check equality up to a given depth.
Definition: sx_elem.cpp:529
Internal node class for SX.
Definition: sx_node.hpp:49
static void safe_delete(SXNode *n)
Non-recursive delete.
Definition: sx_node.cpp:184
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
casadi_limits class
The casadi namespace.
Definition: archiver.cpp:28
static std::string print(unsigned char op, const std::string &x, const std::string &y)
Print.
Definition: calculus.hpp:1641
static void fun(unsigned char op, const T &x, const T &y, T &f)
Evaluate a built in function (scalar-scalar)
Definition: calculus.hpp:1289