casadi_limits.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_CASADI_LIMITS_HPP
27 #define CASADI_CASADI_LIMITS_HPP
28 
29 #include <cmath>
30 #include <limits>
31 
35 namespace casadi {
36 
47  template<class T>
48  class CASADI_EXPORT casadi_limits {
49  public:
50  static bool is_zero(const T& val) {
51  return val==0;
52  }
53  static bool is_equal(const T& x, const T& y, casadi_int depth) {
54  return x==y;
55  }
56  static bool is_almost_zero(const T& val, double tol) {
57  return val<=tol && val>=-tol;
58  }
59  static bool is_one(const T& val) {
60  return val==1;
61  }
62  static bool is_minus_one(const T& val) {
63  return val==-1;
64  }
65  static bool is_value(const T& val, T v) {
66  return val==v;
67  }
68  static bool is_half(const T& val) {
69  return val==0.5;
70  }
71  static bool is_constant(const T& val) {
72  return true;
73  }
74  static bool is_integer(const T& val) {
75  return val==static_cast<casadi_int>(val);
76  }
77  static bool is_inf(const T& val) {
78  return std::numeric_limits<T>::has_infinity && val==std::numeric_limits<T>::infinity();
79  }
80  static bool is_minus_inf(const T& val) {
81  return std::numeric_limits<T>::has_infinity && val==-std::numeric_limits<T>::infinity();
82  }
83  static bool is_nonnegative(const T& val) {
84  return val>=0;
85  }
86  static bool is_nan(const T& val) {
87  return std::numeric_limits<T>::has_quiet_NaN && val!=val;
88  }
89  static const T zero;
90  static const T one;
91  static const T two;
92  static const T minus_one;
93  };
94 
95  template<class T>
96  inline bool is_zero(const T& x) {
97  return casadi_limits<T>::is_zero(x);
98  }
99 
100  template<class T>
101  const T casadi_limits<T>::zero = T(0);
102 
103  template<class T>
104  const T casadi_limits<T>::one = 1;
105 
106  template<class T>
107  const T casadi_limits<T>::two = 2;
108 
109  template<class T>
110  const T casadi_limits<T>::minus_one = -1;
111 
112 } // namespace casadi
113 #endif // CASADI_CASADI_LIMITS_HPP
casadi_limits class
static bool is_inf(const T &val)
static const T minus_one
static bool is_almost_zero(const T &val, double tol)
static bool is_constant(const T &val)
static bool is_nonnegative(const T &val)
static bool is_equal(const T &x, const T &y, casadi_int depth)
static bool is_half(const T &val)
static bool is_one(const T &val)
static bool is_minus_inf(const T &val)
static bool is_integer(const T &val)
static bool is_minus_one(const T &val)
static bool is_nan(const T &val)
static bool is_value(const T &val, T v)
static bool is_zero(const T &val)
The casadi namespace.
Definition: archiver.hpp:32
bool is_zero(const T &x)