unary_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 UNARY_SX_HPP
27 #define UNARY_SX_HPP
28 
29 #include "sx_node.hpp"
30 #include "serializing_stream.hpp"
31 
33 
34 namespace casadi {
35 
42 class UnarySX : public SXNode {
43  private:
44 
48  UnarySX(unsigned char op, const SXElem& dep) : op_(op), dep_(dep) {}
49 
50  public:
51 
55  inline static SXElem create(unsigned char op, const SXElem& dep) {
56  if (dep.is_constant()) {
57  // Evaluate constant
58  double dep_val(dep);
59  double ret_val;
60  casadi_math<double>::fun(op, dep_val, dep_val, ret_val);
61  return ret_val;
62  } else {
63  // Expression containing free variables
64  return SXElem::create(new UnarySX(op, dep));
65  }
66  }
67 
71  ~UnarySX() override {
73  }
74 
75  // Class name
76  std::string class_name() const override {return "UnarySX";}
77 
78  bool is_smooth() const override { return operation_checker<SmoothChecker>(op_);}
79 
80  bool is_op(casadi_int op) const override { return op_==op; }
81 
85  bool is_equal(const SXNode* node, casadi_int depth) const override {
86  const UnarySX* n = dynamic_cast<const UnarySX*>(node);
87  return n && n->op_ == op_ && SXElem::is_equal(n->dep_, dep_, depth-1);
88  }
89 
93  casadi_int n_dep() const override { return 1;}
94 
98  const SXElem& dep(casadi_int i) const override { return dep_; }
99  SXElem& dep(casadi_int i) override { return dep_; }
100 
104  casadi_int op() const override { return op_;}
105 
109  std::string print(const std::string& arg1, const std::string& arg2) const override {
110  return casadi_math<double>::print(op_, arg1);
111  }
112 
116  unsigned char op_;
117 
122 
123  void serialize_node(SerializingStream& s) const override {
124  s.pack("UnarySX::dep", dep_);
125  }
126 
127  static SXNode* deserialize(DeserializingStream& s, casadi_int op) {
128  SXElem dep;
129  s.unpack("UnarySX::dep", dep);
130  return new UnarySX(op, dep);
131  }
132 };
133 
134 } // namespace casadi
135 
137 #endif // UNARY_SX_HPP
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.
Represents a basic unary operation on an SXElem node.
Definition: unary_sx.hpp:42
casadi_int op() const override
Get the operation.
Definition: unary_sx.hpp:104
casadi_int n_dep() const override
Number of dependencies.
Definition: unary_sx.hpp:93
SXElem dep_
The dependencies of the node.
Definition: unary_sx.hpp:121
void serialize_node(SerializingStream &s) const override
Definition: unary_sx.hpp:123
bool is_smooth() const override
Check if smooth.
Definition: unary_sx.hpp:78
SXElem & dep(casadi_int i) override
get the reference of a child
Definition: unary_sx.hpp:99
static SXElem create(unsigned char op, const SXElem &dep)
Create a unary expression.
Definition: unary_sx.hpp:55
std::string print(const std::string &arg1, const std::string &arg2) const override
Print expression.
Definition: unary_sx.hpp:109
bool is_op(casadi_int op) const override
check properties of a node
Definition: unary_sx.hpp:80
unsigned char op_
The binary operation as an 1 byte integer (allows 256 values)
Definition: unary_sx.hpp:116
~UnarySX() override
Destructor.
Definition: unary_sx.hpp:71
static SXNode * deserialize(DeserializingStream &s, casadi_int op)
Definition: unary_sx.hpp:127
bool is_equal(const SXNode *node, casadi_int depth) const override
Check if two nodes are equivalent up to a given depth.
Definition: unary_sx.hpp:85
std::string class_name() const override
Get type name.
Definition: unary_sx.hpp:76
const SXElem & dep(casadi_int i) const override
get the reference of a dependency
Definition: unary_sx.hpp:98
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