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 {
72  safe_delete(dep_.assignNoDelete(casadi_limits<SXElem>::nan));
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 
121  SXElem dep_;
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
The casadi namespace.