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 #ifdef CASADI_WITH_THREAD
48 #include <atomic>
49 #ifdef CASADI_WITH_THREAD_MINGW
50 #include <mingw.mutex.h>
51 #else // CASADI_WITH_THREAD_MINGW
52 #include <mutex>
53 #endif // CASADI_WITH_THREAD_MINGW
54 #endif //CASADI_WITH_THREAD
55 
56 namespace casadi {
57 
61  class SXNode; // include will follow in the end
62 
64 #ifdef SWIG
65  struct SXElem {};
66 #else // SWIG
67 
74  class CASADI_EXPORT SXElem : public GenericExpression<SXElem>,
75  public Printable<SXElem> {
76  friend class SXNode;
77  friend class BinarySXNode;
78  friend class Matrix<SXElem>;
79  public:
80 
82 
87  SXElem();
89 
95  SXElem(double val);
96 
106  static SXElem sym(const std::string& name);
107 
110  SXElem(SXNode* node, bool dummy);
112 
116  SXElem(const SXElem& scalar); // copy constructor
117 
119  ~SXElem();
120 
123  static SXElem create(SXNode* node);
125 
127  SXElem& operator=(const SXElem& scalar);
128  SXElem& operator=(double scalar); // needed since otherwise both a = SXElem(double)
129  // and a = Matrix(double) would be ok
130 
132  operator Matrix<SXElem>() const;
133 
135  static std::string type_name() {return "SXElem";}
136 
138  void disp(std::ostream& stream, bool more=false) const;
139 
141 
144  SXNode* get() const; // note: constant pointer, not pointer to constant object!
145  // (to allow access to the counter)
146 
150  const SXNode* operator->() const;
151  SXNode* operator->();
153 
157  static SXElem binary(casadi_int op, const SXElem& x, const SXElem& y);
158  static SXElem unary(casadi_int op, const SXElem& x);
159  static std::vector<SXElem> call(const Function& f, const std::vector<SXElem>& deps);
160 
166  bool __nonzero__() const;
167 
173  bool is_leaf() const;
174  bool is_constant() const;
175  bool is_integer() const;
176  bool is_symbolic() const;
180  bool is_commutative() const;
181  bool is_zero() const;
182  bool is_almost_zero(double tol) const;
183  bool is_one() const;
184  bool is_minus_one() const;
185  bool is_nan() const;
186  bool is_inf() const;
187  bool is_minus_inf() const;
188  const std::string& name() const;
189  casadi_int op() const;
190  bool is_op(casadi_int op) const;
191  bool is_call() const;
192  bool is_output() const;
193  bool has_output() const;
194  Function which_function() const;
195  casadi_int which_output() const;
196 
198  bool is_regular() const;
199 
203  bool is_nonnegative() const;
204  SXElem dep(casadi_int ch=0) const;
205 
207  explicit operator double() const;
208 
210  explicit operator casadi_int() const;
211 
215  bool is_doubled() const;
216 
220  casadi_int n_dep() const;
221 
225  SXElem get_output(casadi_int oind) const;
226 
232  casadi_int __hash__() const;
233 
237  SXElem operator-() const;
238 
242  SXElem inv() const;
243 
247  static bool is_equal(const SXElem& x, const SXElem& y, casadi_int depth=0);
248 
251  int get_temp() const;
252 
254  void set_temp(int t) const;
255 
257  bool marked() const;
258 
260  void mark() const;
261 
267  void assignIfDuplicate(const SXElem& scalar, casadi_int depth=1);
268 
274  SXNode* assignNoDelete(const SXElem& scalar);
276 
280  inline bool is_null() {return false;}
281 
285  friend inline SXElem if_else(const SXElem& x, const SXElem& y, const SXElem& z) {
286  return if_else_zero(x, y) + if_else_zero(!x, z);
287  }
288 
292  void serialize(SerializingStream& s) const;
293 
294  static SXElem deserialize(DeserializingStream& s);
295 
296 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
297  static std::mutex mutex_temp;
298 #endif //CASADI_WITH_THREADSAFE_SYMBOLICS
299 
300  private:
302  SXNode* node;
303  };
304 
305  template<>
306  class CASADI_EXPORT casadi_limits<SXElem>{
307  public:
308  static bool is_zero(const SXElem& val);
309  static bool is_equal(const SXElem& x, const SXElem& y, casadi_int depth);
310  static bool is_almost_zero(const SXElem& val, double tol);
311  static bool is_one(const SXElem& val);
312  static bool is_minus_one(const SXElem& val);
313  static bool is_constant(const SXElem& val);
314  static bool is_integer(const SXElem& val);
315  static bool is_inf(const SXElem& val);
316  static bool is_minus_inf(const SXElem& val);
317  static bool is_nan(const SXElem& val);
318 
319  static const SXElem zero;
320  static const SXElem one;
321  static const SXElem two;
322  static const SXElem minus_one;
323  static const SXElem nan;
324  static const SXElem inf;
325  static const SXElem minus_inf;
326  };
327 
328 #endif // SWIG
330 
331 } // namespace casadi
332 
333 #ifndef SWIG
334 
335 namespace std {
336  template<>
337  class CASADI_EXPORT numeric_limits<casadi::SXElem>{
338  public:
339  static const bool is_specialized = true;
340  static casadi::SXElem min() throw();
341  static casadi::SXElem max() throw();
342  static const int digits = 0;
343  static const int digits10 = 0;
344  static const bool is_signed = false;
345  static const bool is_integer = false;
346  static const bool is_exact = false;
347  static const int radix = 0;
348  static casadi::SXElem epsilon() throw();
349  static casadi::SXElem round_error() throw();
350  static const int min_exponent = 0;
351  static const int min_exponent10 = 0;
352  static const int max_exponent = 0;
353  static const int max_exponent10 = 0;
354 
355  static const bool has_infinity = true;
356  static const bool has_quiet_NaN = true;
357  static const bool has_signaling_NaN = false;
358  // static const float_denorm_style has_denorm = denorm absent;
359  static const bool has_denorm_loss = false;
360  static casadi::SXElem infinity() throw();
361  static casadi::SXElem quiet_NaN() throw();
362  // static SXElem signaling_NaN() throw();
363  // static SXElem denorm_min() throw();
364  static const bool is_iec559 = false;
365  static const bool is_bounded = false;
366  static const bool is_modulo = false;
367 
368  static const bool traps = false;
369  static const bool tinyness_before = false;
370  static const float_round_style round_style = round_toward_zero;
371  };
372 } //namespace std
373 
374 #endif // SWIG
375 
376 #endif // CASADI_SX_ELEMENT_HPP
Helper class for Serialization.
Function object.
Definition: function.hpp:60
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:99
Base class for objects that have a natural string representation.
Definition: printable.hpp:62
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
friend SXElem if_else(const SXElem &x, const SXElem &y, const SXElem &z)
Ternary if_else: x ? y : z.
Definition: sx_elem.hpp:285
bool is_null()
SXElem nodes are not allowed to be null.
Definition: sx_elem.hpp:280
static std::string type_name()
Type name.
Definition: sx_elem.hpp:135
Internal node class for SX.
Definition: sx_node.hpp:49
Helper class for Serialization.
static const SXElem two
Definition: sx_elem.hpp:321
static const SXElem one
Definition: sx_elem.hpp:320
static const SXElem minus_inf
Definition: sx_elem.hpp:325
static const SXElem nan
Definition: sx_elem.hpp:323
static const SXElem inf
Definition: sx_elem.hpp:324
static const SXElem minus_one
Definition: sx_elem.hpp:322
static const SXElem zero
Definition: sx_elem.hpp:319
casadi_limits class
The casadi namespace.
Definition: archiver.cpp:28
bool is_equal(double x, double y, casadi_int depth=0)
Definition: calculus.hpp:281
double if_else_zero(double x, double y)
Conditional assignment.
Definition: calculus.hpp:289
template class CASADI_EXPORT Matrix< SXElem >
bool is_zero(const T &x)
bool is_regular(const std::vector< T > &v)
Checks if array does not contain NaN or Inf.