AnnoyingScalar.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011-2018 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_TEST_ANNOYING_SCALAR_H
11 #define EIGEN_TEST_ANNOYING_SCALAR_H
12 
13 #include <ostream>
14 
15 #if EIGEN_COMP_GNUC
16 #pragma GCC diagnostic ignored "-Wshadow"
17 #endif
18 
19 #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
21 {
24 };
25 #endif
26 
27 // An AnnoyingScalar is a pseudo scalar type that:
28 // - can randomly through an exception in operator +
29 // - randomly allocate on the heap or initialize a reference to itself making it non trivially copyable, nor movable, nor relocatable.
30 
32 {
33  public:
34  AnnoyingScalar() { init(); *v = 0; }
35  AnnoyingScalar(long double _v) { init(); *v = _v; }
36  AnnoyingScalar(double _v) { init(); *v = _v; }
37  AnnoyingScalar(float _v) { init(); *v = _v; }
38  AnnoyingScalar(int _v) { init(); *v = _v; }
39  AnnoyingScalar(long _v) { init(); *v = _v; }
40  #if EIGEN_HAS_CXX11
41  AnnoyingScalar(long long _v) { init(); *v = _v; }
42  #endif
43  AnnoyingScalar(const AnnoyingScalar& other) { init(); *v = *(other.v); }
45  if(v!=&data)
46  delete v;
47  instances--;
48  }
49 
50  void init() {
51  if(internal::random<bool>())
52  v = new float;
53  else
54  v = &data;
55  instances++;
56  }
57 
59  {
60  #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
61  countdown--;
62  if(countdown<=0 && !dont_throw)
63  throw my_exception();
64  #endif
65  return AnnoyingScalar(*v+*other.v);
66  }
67 
69  { return AnnoyingScalar(-*v); }
70 
72  { return AnnoyingScalar(*v-*other.v); }
73 
75  { return AnnoyingScalar((*v)*(*other.v)); }
76 
78  { return AnnoyingScalar((*v)/(*other.v)); }
79 
80  AnnoyingScalar& operator+=(const AnnoyingScalar& other) { *v += *other.v; return *this; }
81  AnnoyingScalar& operator-=(const AnnoyingScalar& other) { *v -= *other.v; return *this; }
82  AnnoyingScalar& operator*=(const AnnoyingScalar& other) { *v *= *other.v; return *this; }
83  AnnoyingScalar& operator/=(const AnnoyingScalar& other) { *v /= *other.v; return *this; }
84  AnnoyingScalar& operator= (const AnnoyingScalar& other) { *v = *other.v; return *this; }
85 
86  bool operator==(const AnnoyingScalar& other) const { return *v == *other.v; }
87  bool operator!=(const AnnoyingScalar& other) const { return *v != *other.v; }
88  bool operator<=(const AnnoyingScalar& other) const { return *v <= *other.v; }
89  bool operator< (const AnnoyingScalar& other) const { return *v < *other.v; }
90  bool operator>=(const AnnoyingScalar& other) const { return *v >= *other.v; }
91  bool operator> (const AnnoyingScalar& other) const { return *v > *other.v; }
92 
93  float* v;
94  float data;
95  static int instances;
96 #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
97  static int countdown;
98  static bool dont_throw;
99 #endif
100 };
101 
102 AnnoyingScalar real(const AnnoyingScalar &x) { return x; }
103 AnnoyingScalar imag(const AnnoyingScalar & ) { return 0; }
104 AnnoyingScalar conj(const AnnoyingScalar &x) { return x; }
106 AnnoyingScalar abs (const AnnoyingScalar &x) { return std::abs(*x.v); }
107 AnnoyingScalar cos (const AnnoyingScalar &x) { return std::cos(*x.v); }
108 AnnoyingScalar sin (const AnnoyingScalar &x) { return std::sin(*x.v); }
110 AnnoyingScalar atan2(const AnnoyingScalar &y,const AnnoyingScalar &x) { return std::atan2(*y.v,*x.v); }
111 
112 std::ostream& operator<<(std::ostream& stream,const AnnoyingScalar& x) {
113  stream << (*(x.v));
114  return stream;
115 }
116 
118 
119 #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
121 bool AnnoyingScalar::dont_throw = false;
122 #endif
123 
124 namespace Eigen {
125 template<>
127 {
128  enum {
130  };
135 };
136 
138 
139 namespace numext {
140 template<>
142 bool (isfinite)(const AnnoyingScalar& x) {
143  return (numext::isfinite)(*x.v);
144 }
145 }
146 
147 namespace internal {
148  template<> EIGEN_STRONG_INLINE double cast(const AnnoyingScalar& x) { return double(*x.v); }
149  template<> EIGEN_STRONG_INLINE float cast(const AnnoyingScalar& x) { return *x.v; }
150 }
151 } // namespace Eigen
152 
155 
157 { return test_relative_error(*a.v, *b.v); }
158 
159 inline bool test_isApprox(const AnnoyingScalar &a, const AnnoyingScalar &b)
160 { return internal::isApprox(*a.v, *b.v, test_precision<float>()); }
161 
163 { return test_isMuchSmallerThan(*a.v, *b.v); }
164 
165 #endif // EIGEN_TEST_ANNOYING_SCALAR_H
Eigen::numext::x
EIGEN_DEVICE_FUNC const Scalar & x
Definition: SpecialFunctionsImpl.h:1990
AnnoyingScalar::operator-=
AnnoyingScalar & operator-=(const AnnoyingScalar &other)
Definition: AnnoyingScalar.h:81
AnnoyingScalar::data
float data
Definition: AnnoyingScalar.h:94
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
test_relative_error
AnnoyingScalar test_relative_error(const AnnoyingScalar &a, const AnnoyingScalar &b)
Definition: AnnoyingScalar.h:156
Eigen::NumTraits< AnnoyingScalar >::Literal
AnnoyingScalar Literal
Definition: AnnoyingScalar.h:133
AnnoyingScalar::init
void init()
Definition: AnnoyingScalar.h:50
AnnoyingScalar::AnnoyingScalar
AnnoyingScalar(const AnnoyingScalar &other)
Definition: AnnoyingScalar.h:43
AnnoyingScalar::operator+
AnnoyingScalar operator+(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:58
AnnoyingScalar::dont_throw
static bool dont_throw
Definition: AnnoyingScalar.h:98
AnnoyingScalar::countdown
static int countdown
Definition: AnnoyingScalar.h:97
AnnoyingScalar::AnnoyingScalar
AnnoyingScalar(int _v)
Definition: AnnoyingScalar.h:38
Eigen::numext::isfinite
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool() isfinite(const Eigen::bfloat16 &h)
Definition: BFloat16.h:671
gtsam.examples.SFMExample_bal.stream
stream
Definition: SFMExample_bal.py:24
b
Scalar * b
Definition: benchVecAdd.cpp:17
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
AnnoyingScalar::operator>
bool operator>(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:91
AnnoyingScalar::operator<
bool operator<(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:89
Eigen::NumTraits< AnnoyingScalar >::Real
AnnoyingScalar Real
Definition: AnnoyingScalar.h:131
AnnoyingScalar::operator==
bool operator==(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:86
Eigen::internal::isApprox
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1947
Eigen::DenseBase::operator<<
std::ostream & operator<<(std::ostream &s, const DenseBase< Derived > &m)
Definition: IO.h:250
test_isMuchSmallerThan
bool test_isMuchSmallerThan(const AnnoyingScalar &a, const AnnoyingScalar &b)
Definition: AnnoyingScalar.h:162
AnnoyingScalar::operator>=
bool operator>=(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:90
AnnoyingScalar::v
float * v
Definition: AnnoyingScalar.h:93
test_precision< float >
float test_precision< float >()
Definition: spbenchsolver.h:91
test_isApprox
bool test_isApprox(const AnnoyingScalar &a, const AnnoyingScalar &b)
Definition: AnnoyingScalar.h:159
real
AnnoyingScalar real(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:102
get_test_precision
AnnoyingScalar get_test_precision(const AnnoyingScalar &)
Definition: AnnoyingScalar.h:153
AnnoyingScalar::~AnnoyingScalar
~AnnoyingScalar()
Definition: AnnoyingScalar.h:44
AnnoyingScalar::AnnoyingScalar
AnnoyingScalar(long double _v)
Definition: AnnoyingScalar.h:35
AnnoyingScalar::AnnoyingScalar
AnnoyingScalar(float _v)
Definition: AnnoyingScalar.h:37
Eigen::test_precision< float >
float test_precision< float >()
Definition: main.h:416
AnnoyingScalar::operator-
AnnoyingScalar operator-() const
Definition: AnnoyingScalar.h:68
acos
AnnoyingScalar acos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:109
Eigen::test_precision< AnnoyingScalar >
AnnoyingScalar test_precision< AnnoyingScalar >()
Definition: AnnoyingScalar.h:137
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
abs
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:106
imag
AnnoyingScalar imag(const AnnoyingScalar &)
Definition: AnnoyingScalar.h:103
my_exception::~my_exception
~my_exception()
Definition: AnnoyingScalar.h:23
EIGEN_ALWAYS_INLINE
#define EIGEN_ALWAYS_INLINE
Definition: Macros.h:932
my_exception::my_exception
my_exception()
Definition: AnnoyingScalar.h:22
AnnoyingScalar::operator/
AnnoyingScalar operator/(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:77
conj
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:104
AnnoyingScalar::AnnoyingScalar
AnnoyingScalar()
Definition: AnnoyingScalar.h:34
y
Scalar * y
Definition: level1_cplx_impl.h:124
sqrt
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:105
AnnoyingScalar
Definition: AnnoyingScalar.h:31
atan2
AnnoyingScalar atan2(const AnnoyingScalar &y, const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:110
Eigen::GenericNumTraits::RequireInitialization
@ RequireInitialization
Definition: NumTraits.h:158
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
AnnoyingScalar::operator+=
AnnoyingScalar & operator+=(const AnnoyingScalar &other)
Definition: AnnoyingScalar.h:80
AnnoyingScalar::instances
static int instances
Definition: AnnoyingScalar.h:95
AnnoyingScalar::operator*=
AnnoyingScalar & operator*=(const AnnoyingScalar &other)
Definition: AnnoyingScalar.h:82
AnnoyingScalar::operator=
AnnoyingScalar & operator=(const AnnoyingScalar &other)
Definition: AnnoyingScalar.h:84
Eigen::NumTraits< AnnoyingScalar >::NonInteger
AnnoyingScalar NonInteger
Definition: AnnoyingScalar.h:134
AnnoyingScalar::operator-
AnnoyingScalar operator-(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:71
AnnoyingScalar::operator<=
bool operator<=(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:88
gtsam.examples.DogLegOptimizerExample.float
float
Definition: DogLegOptimizerExample.py:113
AnnoyingScalar::AnnoyingScalar
AnnoyingScalar(long _v)
Definition: AnnoyingScalar.h:39
internal
Definition: BandTriangularSolver.h:13
AnnoyingScalar::operator/=
AnnoyingScalar & operator/=(const AnnoyingScalar &other)
Definition: AnnoyingScalar.h:83
cos
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:107
AnnoyingScalar::operator*
AnnoyingScalar operator*(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:74
AnnoyingScalar::operator!=
bool operator!=(const AnnoyingScalar &other) const
Definition: AnnoyingScalar.h:87
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Eigen::NumTraits< AnnoyingScalar >::Nested
AnnoyingScalar Nested
Definition: AnnoyingScalar.h:132
AnnoyingScalar::AnnoyingScalar
AnnoyingScalar(double _v)
Definition: AnnoyingScalar.h:36
Eigen::internal::cast
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Definition: Eigen/src/Core/MathFunctions.h:460
my_exception
Definition: AnnoyingScalar.h:20
sin
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:108


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:31:52