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_nan() const { return false; }
78  virtual bool is_inf() const { return false; }
79  virtual bool is_minus_inf() const { return false; }
81 
83 
86  virtual double to_double() const; // only works for constant nodes
87  virtual casadi_int to_int() const; // only works for integer nodes
89 
90  // get the name
91  virtual const std::string& name() const;
92 
96  virtual std::string class_name() const = 0;
97 
101  virtual casadi_int op() const=0;
102 
106  virtual bool is_equal(const SXNode* node, casadi_int depth) const;
107 
111  virtual casadi_int n_dep() const { return 0;}
112 
116  virtual const SXElem& dep(casadi_int i) const;
117 
121  virtual SXElem& dep(casadi_int i);
122 
126  virtual bool is_smooth() const { return true; }
127 
131  virtual void disp(std::ostream& stream, bool more) const;
132 
136  void can_inline(std::map<const SXNode*, casadi_int>& nodeind) const;
137 
141  std::string print_compact(std::map<const SXNode*, casadi_int>& nodeind,
142  std::vector<std::string>& intermed) const;
143 
147  virtual std::string print(const std::string& arg1, const std::string& arg2) const = 0;
148 
149  // Check if marked (i.e. temporary is negative)
150  bool marked() const;
151 
152  // Mark by flipping the sign of the temporary and decreasing by one
153  void mark() const;
154 
158  static void safe_delete(SXNode* n);
159 
160  // Depth when checking equalities
161  static casadi_int eq_depth_;
162 
167  mutable int temp;
168 
169  // Reference counter -- counts the number of parents of the node
170  unsigned int count;
171 
175  void serialize(SerializingStream& s) const;
176 
177  virtual void serialize_node(SerializingStream& s) const;
178 
179  static SXNode* deserialize(DeserializingStream& s);
180 
181  static std::map<casadi_int, SXNode* (*)(DeserializingStream&)> deserialize_map;
182 
183 
184  };
185 
186 } // namespace casadi
188 #endif // CASADI_SX_NODE_HPP
The casadi namespace.
bool is_zero(const T &x)