sx_node.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_SX_NODE_HPP
27 #define CASADI_SX_NODE_HPP
28 
29 #include <iostream>
30 #include <math.h>
31 #include <sstream>
32 #include <string>
33 
37 #include "sx_elem.hpp"
38 
39 
41 namespace casadi {
42 
49  class SXNode {
50  friend class SXElem;
51  friend class Matrix<SXElem>;
52  friend class UniversalNodeOwner;
53  public:
54 
58  SXNode();
59 
63  virtual ~SXNode();
64 
66 
69  virtual bool is_constant() const { return false; }
70  virtual bool is_integer() const { return false; }
71  virtual bool is_symbolic() const { return false; }
72  virtual bool is_zero() const { return false; }
73  virtual bool is_op(casadi_int op) const { return false; }
74  virtual bool is_almost_zero(double tol) const { return false; }
75  virtual bool is_one() const { return false; }
76  virtual bool is_minus_one() const { return false; }
77  virtual bool is_half() const { return false; }
78  virtual bool is_value(double val) const { return false; }
79  virtual bool is_nan() const { return false; }
80  virtual bool is_inf() const { return false; }
81  virtual bool is_minus_inf() const { return false; }
82  virtual bool is_call() const { return false; }
83  virtual bool is_output() const { return false; }
84  virtual bool has_output() const { return false; }
86 
88 
91  virtual double to_double() const; // only works for constant nodes
92  virtual casadi_int to_int() const; // only works for integer nodes
94 
95  // get the name
96  virtual const std::string& name() const;
97 
101  virtual std::string class_name() const = 0;
102 
106  virtual casadi_int op() const=0;
107 
111  virtual Function which_function() const;
112 
116  virtual casadi_int which_output() const;
117 
121  virtual bool is_equal(const SXNode* node, casadi_int depth) const;
122 
126  virtual casadi_int n_dep() const { return 0;}
127 
131  virtual SXElem get_output(casadi_int oind) const;
132 
136  virtual const SXElem& dep(casadi_int i) const;
137 
141  virtual SXElem& dep(casadi_int i);
142 
146  virtual bool is_smooth() const { return true; }
147 
151  virtual void disp(std::ostream& stream, bool more) const;
152 
156  void can_inline(std::map<const SXNode*, casadi_int>& nodeind) const;
157 
161  std::string print_compact(std::map<const SXNode*, casadi_int>& nodeind,
162  std::vector<std::string>& intermed) const;
163 
167  virtual std::string print(const std::string& arg1, const std::string& arg2) const = 0;
168 
169  // Check if marked (i.e. temporary is negative)
170  bool marked() const;
171 
172  // Mark by flipping the sign of the temporary and decreasing by one
173  void mark() const;
174 
178  static void safe_delete(SXNode* n);
179 
180  // Depth when checking equalities
181  static casadi_int eq_depth_;
182 
184  SXElem shared_from_this();
185 
187  const SXElem shared_from_this() const;
188 
193  mutable int temp;
194 
195  // Reference counter -- counts the number of parents of the node
196 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
197  std::atomic<unsigned int> count;
198 #else
199  unsigned int count;
200 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
201 
205  void serialize(SerializingStream& s) const;
206 
207  virtual void serialize_node(SerializingStream& s) const;
208 
209  static SXNode* deserialize(DeserializingStream& s);
210 
211  static std::map<casadi_int, SXNode* (*)(DeserializingStream&)> deserialize_map;
212 
213 
214  };
215 
216 } // namespace casadi
218 #endif // CASADI_SX_NODE_HPP
The casadi namespace.
Definition: archiver.hpp:32
bool is_zero(const T &x)