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  bool unique_x=false, bool unique_y=false);
159  static SXElem unary(casadi_int op, const SXElem& x, bool unique=false);
160  static std::vector<SXElem> call(const Function& f, const std::vector<SXElem>& deps);
161 
167  bool __nonzero__() const;
168 
174  bool is_leaf() const;
175  bool is_constant() const;
176  bool is_integer() const;
177  bool is_symbolic() const;
181  bool is_commutative() const;
182  bool is_zero() const;
183  bool is_almost_zero(double tol) const;
184  bool is_one() const;
185  bool is_minus_one() const;
186  bool is_half() const;
187  bool is_value(double val) const;
188  bool is_nan() const;
189  bool is_inf() const;
190  bool is_minus_inf() const;
191  const std::string& name() const;
192  casadi_int op() const;
193  bool is_op(casadi_int op) const;
194  bool is_call() const;
195  bool is_output() const;
196  bool has_output() const;
197  Function which_function() const;
198  casadi_int which_output() const;
199 
201  bool is_regular() const;
202 
206  bool is_nonnegative() const;
207  SXElem dep(casadi_int ch=0) const;
208 
210  explicit operator double() const;
211 
213  explicit operator casadi_int() const;
214 
218  bool is_doubled() const;
219 
223  casadi_int n_dep() const;
224 
228  SXElem get_output(casadi_int oind) const;
229 
235  casadi_int __hash__() const;
236 
240  SXElem operator-() const;
241 
245  SXElem inv() const;
246 
250  static bool is_equal(const SXElem& x, const SXElem& y, casadi_int depth=0);
251 
254  int get_temp() const;
255 
257  void set_temp(int t) const;
258 
260  bool marked() const;
261 
263  void mark() const;
264 
270  void assignIfDuplicate(const SXElem& scalar, casadi_int depth=1);
271 
277  SXNode* assignNoDelete(const SXElem& scalar);
279 
283  inline bool is_null() {return false;}
284 
288  friend inline SXElem if_else(const SXElem& x, const SXElem& y, const SXElem& z) {
289  return if_else_zero(x, y) + if_else_zero(!x, z);
290  }
291 
295  void serialize(SerializingStream& s) const;
296 
297  static SXElem deserialize(DeserializingStream& s);
298 
299 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
300  static std::mutex mutex_temp;
301 #endif //CASADI_WITH_THREADSAFE_SYMBOLICS
302 
303  private:
305  SXNode* node;
306  };
307 
308  template<>
309  class CASADI_EXPORT casadi_limits<SXElem>{
310  public:
311  static bool is_zero(const SXElem& val);
312  static bool is_equal(const SXElem& x, const SXElem& y, casadi_int depth);
313  static bool is_almost_zero(const SXElem& val, double tol);
314  static bool is_one(const SXElem& val);
315  static bool is_minus_one(const SXElem& val);
316  static bool is_half(const SXElem& val);
317  static bool is_value(const SXElem& val, double v);
318  static bool is_nonnegative(const SXElem& val);
319  static bool is_constant(const SXElem& val);
320  static bool is_integer(const SXElem& val);
321  static bool is_inf(const SXElem& val);
322  static bool is_minus_inf(const SXElem& val);
323  static bool is_nan(const SXElem& val);
324 
325  static const SXElem zero;
326  static const SXElem one;
327  static const SXElem two;
328  static const SXElem minus_one;
329  static const SXElem nan;
330  static const SXElem inf;
331  static const SXElem minus_inf;
332  };
333 
334 #endif // SWIG
336 
337 } // namespace casadi
338 
339 #ifndef SWIG
340 
341 namespace std {
342  template<>
343  class CASADI_EXPORT numeric_limits<casadi::SXElem>{
344  public:
345  static const bool is_specialized = true;
346  static casadi::SXElem min() throw();
347  static casadi::SXElem max() throw();
348  static const int digits = 0;
349  static const int digits10 = 0;
350  static const bool is_signed = false;
351  static const bool is_integer = false;
352  static const bool is_exact = false;
353  static const int radix = 0;
354  static casadi::SXElem epsilon() throw();
355  static casadi::SXElem round_error() throw();
356  static const int min_exponent = 0;
357  static const int min_exponent10 = 0;
358  static const int max_exponent = 0;
359  static const int max_exponent10 = 0;
360 
361  static const bool has_infinity = true;
362  static const bool has_quiet_NaN = true;
363  static const bool has_signaling_NaN = false;
364  // static const float_denorm_style has_denorm = denorm absent;
365  static const bool has_denorm_loss = false;
366  static casadi::SXElem infinity() throw();
367  static casadi::SXElem quiet_NaN() throw();
368  // static SXElem signaling_NaN() throw();
369  // static SXElem denorm_min() throw();
370  static const bool is_iec559 = false;
371  static const bool is_bounded = false;
372  static const bool is_modulo = false;
373 
374  static const bool traps = false;
375  static const bool tinyness_before = false;
376  static const float_round_style round_style = round_toward_zero;
377  };
378 } //namespace std
379 
380 #endif // SWIG
381 
382 #endif // CASADI_SX_ELEMENT_HPP
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:99
The casadi namespace.
Definition: archiver.hpp:32
bool is_zero(const T &x)
bool is_regular(const std::vector< T > &v)
Checks if array does not contain NaN or Inf.