sx_elem.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_ELEMENT_HPP
27 #define CASADI_SX_ELEMENT_HPP
28 
29 // exception class
30 #include "printable.hpp"
31 #include "exception.hpp"
32 #include "casadi_limits.hpp"
33 #include "matrix_fwd.hpp"
34 #include "generic_expression.hpp"
35 #include "serializing_stream.hpp"
36 
40 #include <cmath>
41 #include <iostream>
42 #include <limits>
43 #include <sstream>
44 #include <string>
45 #include <vector>
46 
47 namespace casadi {
48 
52  class SXNode; // include will follow in the end
53 
55 #ifdef SWIG
56  struct SXElem {};
57 #else // SWIG
58 
65  class CASADI_EXPORT SXElem : public GenericExpression<SXElem>,
66  public Printable<SXElem> {
67  friend class SXNode;
68  friend class BinarySXNode;
69  friend class Matrix<SXElem>;
70  public:
71 
73 
78  SXElem();
80 
86  SXElem(double val);
87 
97  static SXElem sym(const std::string& name);
98 
101  SXElem(SXNode* node, bool dummy);
103 
107  SXElem(const SXElem& scalar); // copy constructor
108 
110  ~SXElem();
111 
114  static SXElem create(SXNode* node);
116 
118  SXElem& operator=(const SXElem& scalar);
119  SXElem& operator=(double scalar); // needed since otherwise both a = SXElem(double)
120  // and a = Matrix(double) would be ok
121 
123  operator Matrix<SXElem>() const;
124 
126  static std::string type_name() {return "SXElem";}
127 
129  void disp(std::ostream& stream, bool more=false) const;
130 
132 
135  SXNode* get() const; // note: constant pointer, not pointer to constant object!
136  // (to allow access to the counter)
137 
141  const SXNode* operator->() const;
142  SXNode* operator->();
144 
148  static SXElem binary(casadi_int op, const SXElem& x, const SXElem& y);
149  static SXElem unary(casadi_int op, const SXElem& x);
150 
156  bool __nonzero__() const;
157 
163  bool is_leaf() const;
164  bool is_constant() const;
165  bool is_integer() const;
166  bool is_symbolic() const;
170  bool is_commutative() const;
171  bool is_zero() const;
172  bool is_almost_zero(double tol) const;
173  bool is_one() const;
174  bool is_minus_one() const;
175  bool is_nan() const;
176  bool is_inf() const;
177  bool is_minus_inf() const;
178  const std::string& name() const;
179  casadi_int op() const;
180  bool is_op(casadi_int op) const;
181 
183  bool is_regular() const;
184 
188  bool is_nonnegative() const;
189  SXElem dep(casadi_int ch=0) const;
190 
192  explicit operator double() const;
193 
195  explicit operator casadi_int() const;
196 
200  bool is_doubled() const;
201 
205  casadi_int n_dep() const;
206 
212  casadi_int __hash__() const;
213 
217  SXElem operator-() const;
218 
222  SXElem inv() const;
223 
227  static bool is_equal(const SXElem& x, const SXElem& y, casadi_int depth=0);
228 
231  int get_temp() const;
232 
234  void set_temp(int t) const;
235 
237  bool marked() const;
238 
240  void mark() const;
241 
247  void assignIfDuplicate(const SXElem& scalar, casadi_int depth=1);
248 
254  SXNode* assignNoDelete(const SXElem& scalar);
256 
260  inline bool is_null() {return false;}
261 
265  friend inline SXElem if_else(const SXElem& x, const SXElem& y, const SXElem& z) {
266  return if_else_zero(x, y) + if_else_zero(!x, z);
267  }
268 
272  void serialize(SerializingStream& s) const;
273 
274  static SXElem deserialize(DeserializingStream& s);
275  private:
277  SXNode* node;
278  };
279 
280  template<>
281  class CASADI_EXPORT casadi_limits<SXElem>{
282  public:
283  static bool is_zero(const SXElem& val);
284  static bool is_equal(const SXElem& x, const SXElem& y, casadi_int depth);
285  static bool is_almost_zero(const SXElem& val, double tol);
286  static bool is_one(const SXElem& val);
287  static bool is_minus_one(const SXElem& val);
288  static bool is_constant(const SXElem& val);
289  static bool is_integer(const SXElem& val);
290  static bool is_inf(const SXElem& val);
291  static bool is_minus_inf(const SXElem& val);
292  static bool is_nan(const SXElem& val);
293 
294  static const SXElem zero;
295  static const SXElem one;
296  static const SXElem two;
297  static const SXElem minus_one;
298  static const SXElem nan;
299  static const SXElem inf;
300  static const SXElem minus_inf;
301  };
302 
303 #endif // SWIG
305 
306 } // namespace casadi
307 
308 #ifndef SWIG
309 
310 namespace std {
311  template<>
312  class CASADI_EXPORT numeric_limits<casadi::SXElem>{
313  public:
314  static const bool is_specialized = true;
315  static casadi::SXElem min() throw();
316  static casadi::SXElem max() throw();
317  static const int digits = 0;
318  static const int digits10 = 0;
319  static const bool is_signed = false;
320  static const bool is_integer = false;
321  static const bool is_exact = false;
322  static const int radix = 0;
323  static casadi::SXElem epsilon() throw();
324  static casadi::SXElem round_error() throw();
325  static const int min_exponent = 0;
326  static const int min_exponent10 = 0;
327  static const int max_exponent = 0;
328  static const int max_exponent10 = 0;
329 
330  static const bool has_infinity = true;
331  static const bool has_quiet_NaN = true;
332  static const bool has_signaling_NaN = false;
333  // static const float_denorm_style has_denorm = denorm absent;
334  static const bool has_denorm_loss = false;
335  static casadi::SXElem infinity() throw();
336  static casadi::SXElem quiet_NaN() throw();
337  // static SXElem signaling_NaN() throw();
338  // static SXElem denorm_min() throw();
339  static const bool is_iec559 = false;
340  static const bool is_bounded = false;
341  static const bool is_modulo = false;
342 
343  static const bool traps = false;
344  static const bool tinyness_before = false;
345  static const float_round_style round_style = round_toward_zero;
346  };
347 } //namespace std
348 
349 #endif // SWIG
350 
351 #endif // CASADI_SX_ELEMENT_HPP
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:92
The casadi namespace.
bool is_zero(const T &x)
bool is_regular(const std::vector< T > &v)
Checks if array does not contain NaN or Inf.