boostmultiprec.cpp
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) 2016 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 #include <sstream>
11 
12 #ifdef EIGEN_TEST_MAX_SIZE
13 #undef EIGEN_TEST_MAX_SIZE
14 #endif
15 
16 #define EIGEN_TEST_MAX_SIZE 50
17 
18 #ifdef EIGEN_TEST_PART_1
19 #include "cholesky.cpp"
20 #endif
21 
22 #ifdef EIGEN_TEST_PART_2
23 #include "lu.cpp"
24 #endif
25 
26 #ifdef EIGEN_TEST_PART_3
27 #include "qr.cpp"
28 #endif
29 
30 #ifdef EIGEN_TEST_PART_4
31 #include "qr_colpivoting.cpp"
32 #endif
33 
34 #ifdef EIGEN_TEST_PART_5
35 #include "qr_fullpivoting.cpp"
36 #endif
37 
38 #ifdef EIGEN_TEST_PART_6
40 #endif
41 
42 #ifdef EIGEN_TEST_PART_7
43 #include "eigensolver_generic.cpp"
44 #endif
45 
46 #ifdef EIGEN_TEST_PART_8
48 #endif
49 
50 #ifdef EIGEN_TEST_PART_9
51 #include "jacobisvd.cpp"
52 #endif
53 
54 #ifdef EIGEN_TEST_PART_10
55 #include "bdcsvd.cpp"
56 #endif
57 
58 #ifdef EIGEN_TEST_PART_11
59 #include "simplicial_cholesky.cpp"
60 #endif
61 
62 #include <Eigen/Dense>
63 
64 #undef min
65 #undef max
66 #undef isnan
67 #undef isinf
68 #undef isfinite
69 #undef I
70 
71 #include <boost/serialization/nvp.hpp>
72 #include <boost/multiprecision/cpp_dec_float.hpp>
73 #include <boost/multiprecision/number.hpp>
74 #include <boost/math/special_functions.hpp>
75 #include <boost/math/complex.hpp>
76 
77 namespace mp = boost::multiprecision;
78 typedef mp::number<mp::cpp_dec_float<100>, mp::et_on> Real;
79 
80 namespace Eigen {
81  template<> struct NumTraits<Real> : GenericNumTraits<Real> {
82  static inline Real dummy_precision() { return 1e-50; }
83  };
84 
85  template<typename T1,typename T2,typename T3,typename T4,typename T5>
86  struct NumTraits<boost::multiprecision::detail::expression<T1,T2,T3,T4,T5> > : NumTraits<Real> {};
87 
88  template<>
89  Real test_precision<Real>() { return 1e-50; }
90 
91  // needed in C++93 mode where number does not support explicit cast.
92  namespace internal {
93  template<typename NewType>
94  struct cast_impl<Real,NewType> {
95  static inline NewType run(const Real& x) {
96  return x.template convert_to<NewType>();
97  }
98  };
99 
100  template<>
101  struct cast_impl<Real,std::complex<Real> > {
102  static inline std::complex<Real> run(const Real& x) {
103  return std::complex<Real>(x);
104  }
105  };
106  }
107 }
108 
109 namespace boost {
110 namespace multiprecision {
111  // to make ADL works as expected:
112  using boost::math::isfinite;
113  using boost::math::isnan;
114  using boost::math::isinf;
115  using boost::math::copysign;
116  using boost::math::hypot;
117 
118  // The following is needed for std::complex<Real>:
119  Real fabs(const Real& a) { return abs EIGEN_NOT_A_MACRO (a); }
120  Real fmax(const Real& a, const Real& b) { using std::max; return max(a,b); }
121 
122  // some specialization for the unit tests:
123  inline bool test_isMuchSmallerThan(const Real& a, const Real& b) {
125  }
126 
127  inline bool test_isApprox(const Real& a, const Real& b) {
129  }
130 
131  inline bool test_isApproxOrLessThan(const Real& a, const Real& b) {
133  }
134 
136  return test_precision<Real>();
137  }
138 
139  Real test_relative_error(const Real &a, const Real &b) {
140  using Eigen::numext::abs2;
141  return sqrt(abs2<Real>(a-b)/Eigen::numext::mini<Real>(abs2(a),abs2(b)));
142  }
143 }
144 }
145 
146 namespace Eigen {
147 
148 }
149 
150 EIGEN_DECLARE_TEST(boostmultiprec)
151 {
152  typedef Matrix<Real,Dynamic,Dynamic> Mat;
153  typedef Matrix<std::complex<Real>,Dynamic,Dynamic> MatC;
154 
155  std::cout << "NumTraits<Real>::epsilon() = " << NumTraits<Real>::epsilon() << std::endl;
156  std::cout << "NumTraits<Real>::dummy_precision() = " << NumTraits<Real>::dummy_precision() << std::endl;
157  std::cout << "NumTraits<Real>::lowest() = " << NumTraits<Real>::lowest() << std::endl;
158  std::cout << "NumTraits<Real>::highest() = " << NumTraits<Real>::highest() << std::endl;
159  std::cout << "NumTraits<Real>::digits10() = " << NumTraits<Real>::digits10() << std::endl;
160 
161  // check stream output
162  {
163  Mat A(10,10);
164  A.setRandom();
165  std::stringstream ss;
166  ss << A;
167  }
168  {
169  MatC A(10,10);
170  A.setRandom();
171  std::stringstream ss;
172  ss << A;
173  }
174 
175  for(int i = 0; i < g_repeat; i++) {
176  int s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
177 
178  CALL_SUBTEST_1( cholesky(Mat(s,s)) );
179 
180  CALL_SUBTEST_2( lu_non_invertible<Mat>() );
181  CALL_SUBTEST_2( lu_invertible<Mat>() );
182  CALL_SUBTEST_2( lu_non_invertible<MatC>() );
183  CALL_SUBTEST_2( lu_invertible<MatC>() );
184 
185  CALL_SUBTEST_3( qr(Mat(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
186  CALL_SUBTEST_3( qr_invertible<Mat>() );
187 
188  CALL_SUBTEST_4( qr<Mat>() );
189  CALL_SUBTEST_4( cod<Mat>() );
190  CALL_SUBTEST_4( qr_invertible<Mat>() );
191 
192  CALL_SUBTEST_5( qr<Mat>() );
193  CALL_SUBTEST_5( qr_invertible<Mat>() );
194 
196 
197  CALL_SUBTEST_7( eigensolver(Mat(s,s)) );
198 
200 
202  }
203 
204  CALL_SUBTEST_9(( jacobisvd(Mat(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
205  CALL_SUBTEST_10(( bdcsvd(Mat(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
206 
207  CALL_SUBTEST_11(( test_simplicial_cholesky_T<Real,int,ColMajor>() ));
208 }
void selfadjointeigensolver(const MatrixType &m)
#define CALL_SUBTEST_9(FUNC)
#define max(a, b)
Definition: datatypes.h:20
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
#define EIGEN_NOT_A_MACRO
Definition: Macros.h:896
NumTraits< typename T::Scalar >::Real get_test_precision(const T &, const typename T::Scalar *=0)
Definition: main.h:572
Scalar * b
Definition: benchVecAdd.cpp:17
Real test_precision< Real >()
#define CALL_SUBTEST_3(FUNC)
#define CALL_SUBTEST_7(FUNC)
#define CALL_SUBTEST_11(FUNC)
bool test_isApproxOrLessThan(const long double &a, const long double &b)
Definition: main.h:479
void generalized_eigensolver_real(const MatrixType &m)
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
CleanedUpDerType< DerType >::type() max(const AutoDiffScalar< DerType > &x, const T &y)
Definition: BFloat16.h:88
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
#define isfinite(X)
Definition: main.h:95
#define isinf(X)
Definition: main.h:94
Real fabs(const Real &a)
HouseholderQR< MatrixXf > qr(A)
static double epsilon
Definition: testRot3.cpp:37
#define CALL_SUBTEST_10(FUNC)
void eigensolver(const MatrixType &m)
void bdcsvd(const MatrixType &a=MatrixType(), bool pickrandom=true)
Definition: bdcsvd.cpp:29
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Abs2ReturnType abs2() const
bool test_isApprox(const std::complex< float > &a, const std::complex< float > &b)
Definition: main.h:449
#define CALL_SUBTEST_1(FUNC)
Real fmax(const Real &a, const Real &b)
static int g_repeat
Definition: main.h:169
#define CALL_SUBTEST_8(FUNC)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
bool test_isMuchSmallerThan(const std::complex< float > &a, const std::complex< float > &b)
Definition: main.h:451
static std::stringstream ss
Definition: testBTree.cpp:31
static std::complex< Real > run(const Real &x)
mp::number< mp::cpp_dec_float< 100 >, mp::et_on > Real
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
void cholesky(const MatrixType &m)
#define CALL_SUBTEST_5(FUNC)
void jacobisvd(const MatrixType &a=MatrixType(), bool pickrandom=true)
Definition: jacobisvd.cpp:23
EIGEN_DECLARE_TEST(boostmultiprec)
#define EIGEN_TEST_MAX_SIZE
NumTraits< typename T1::RealScalar >::NonInteger test_relative_error(const EigenBase< T1 > &a, const EigenBase< T2 > &b)
Definition: main.h:485
Matrix< Scalar, Dynamic, Dynamic > Mat
Definition: gemm_common.h:15
#define CALL_SUBTEST_2(FUNC)
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
const int Dynamic
Definition: Constants.h:22
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
EIGEN_DEVICE_FUNC bool isApproxOrLessThan(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
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
#define abs(x)
Definition: datatypes.h:17
Derived & setRandom(Index size)
Definition: Random.h:151
EIGEN_DEVICE_FUNC bool abs2(bool x)
#define isnan(X)
Definition: main.h:93


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:59