boostmultiprec.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include <sstream>
  10. #ifdef EIGEN_TEST_MAX_SIZE
  11. #undef EIGEN_TEST_MAX_SIZE
  12. #endif
  13. #define EIGEN_TEST_MAX_SIZE 50
  14. #ifdef EIGEN_TEST_PART_1
  15. #include "cholesky.cpp"
  16. #endif
  17. #ifdef EIGEN_TEST_PART_2
  18. #include "lu.cpp"
  19. #endif
  20. #ifdef EIGEN_TEST_PART_3
  21. #include "qr.cpp"
  22. #endif
  23. #ifdef EIGEN_TEST_PART_4
  24. #include "qr_colpivoting.cpp"
  25. #endif
  26. #ifdef EIGEN_TEST_PART_5
  27. #include "qr_fullpivoting.cpp"
  28. #endif
  29. #ifdef EIGEN_TEST_PART_6
  30. #include "eigensolver_selfadjoint.cpp"
  31. #endif
  32. #ifdef EIGEN_TEST_PART_7
  33. #include "eigensolver_generic.cpp"
  34. #endif
  35. #ifdef EIGEN_TEST_PART_8
  36. #include "eigensolver_generalized_real.cpp"
  37. #endif
  38. #ifdef EIGEN_TEST_PART_9
  39. #include "jacobisvd.cpp"
  40. #endif
  41. #ifdef EIGEN_TEST_PART_10
  42. #include "bdcsvd.cpp"
  43. #endif
  44. #include <Eigen/Dense>
  45. #undef min
  46. #undef max
  47. #undef isnan
  48. #undef isinf
  49. #undef isfinite
  50. #include <boost/multiprecision/cpp_dec_float.hpp>
  51. #include <boost/multiprecision/number.hpp>
  52. #include <boost/math/special_functions.hpp>
  53. #include <boost/math/complex.hpp>
  54. namespace mp = boost::multiprecision;
  55. typedef mp::number<mp::cpp_dec_float<100>, mp::et_on> Real;
  56. namespace Eigen {
  57. template<> struct NumTraits<Real> : GenericNumTraits<Real> {
  58. static inline Real dummy_precision() { return 1e-50; }
  59. };
  60. template<typename T1,typename T2,typename T3,typename T4,typename T5>
  61. struct NumTraits<boost::multiprecision::detail::expression<T1,T2,T3,T4,T5> > : NumTraits<Real> {};
  62. template<>
  63. Real test_precision<Real>() { return 1e-50; }
  64. // needed in C++93 mode where number does not support explicit cast.
  65. namespace internal {
  66. template<typename NewType>
  67. struct cast_impl<Real,NewType> {
  68. static inline NewType run(const Real& x) {
  69. return x.template convert_to<NewType>();
  70. }
  71. };
  72. template<>
  73. struct cast_impl<Real,std::complex<Real> > {
  74. static inline std::complex<Real> run(const Real& x) {
  75. return std::complex<Real>(x);
  76. }
  77. };
  78. }
  79. }
  80. namespace boost {
  81. namespace multiprecision {
  82. // to make ADL works as expected:
  83. using boost::math::isfinite;
  84. using boost::math::isnan;
  85. using boost::math::isinf;
  86. using boost::math::copysign;
  87. using boost::math::hypot;
  88. // The following is needed for std::complex<Real>:
  89. Real fabs(const Real& a) { return abs EIGEN_NOT_A_MACRO (a); }
  90. Real fmax(const Real& a, const Real& b) { using std::max; return max(a,b); }
  91. // some specialization for the unit tests:
  92. inline bool test_isMuchSmallerThan(const Real& a, const Real& b) {
  93. return internal::isMuchSmallerThan(a, b, test_precision<Real>());
  94. }
  95. inline bool test_isApprox(const Real& a, const Real& b) {
  96. return internal::isApprox(a, b, test_precision<Real>());
  97. }
  98. inline bool test_isApproxOrLessThan(const Real& a, const Real& b) {
  99. return internal::isApproxOrLessThan(a, b, test_precision<Real>());
  100. }
  101. Real get_test_precision(const Real&) {
  102. return test_precision<Real>();
  103. }
  104. Real test_relative_error(const Real &a, const Real &b) {
  105. using Eigen::numext::abs2;
  106. return sqrt(abs2<Real>(a-b)/Eigen::numext::mini<Real>(abs2(a),abs2(b)));
  107. }
  108. }
  109. }
  110. namespace Eigen {
  111. }
  112. void test_boostmultiprec()
  113. {
  114. typedef Matrix<Real,Dynamic,Dynamic> Mat;
  115. typedef Matrix<std::complex<Real>,Dynamic,Dynamic> MatC;
  116. std::cout << "NumTraits<Real>::epsilon() = " << NumTraits<Real>::epsilon() << std::endl;
  117. std::cout << "NumTraits<Real>::dummy_precision() = " << NumTraits<Real>::dummy_precision() << std::endl;
  118. std::cout << "NumTraits<Real>::lowest() = " << NumTraits<Real>::lowest() << std::endl;
  119. std::cout << "NumTraits<Real>::highest() = " << NumTraits<Real>::highest() << std::endl;
  120. std::cout << "NumTraits<Real>::digits10() = " << NumTraits<Real>::digits10() << std::endl;
  121. // chekc stream output
  122. {
  123. Mat A(10,10);
  124. A.setRandom();
  125. std::stringstream ss;
  126. ss << A;
  127. }
  128. {
  129. MatC A(10,10);
  130. A.setRandom();
  131. std::stringstream ss;
  132. ss << A;
  133. }
  134. for(int i = 0; i < g_repeat; i++) {
  135. int s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  136. CALL_SUBTEST_1( cholesky(Mat(s,s)) );
  137. CALL_SUBTEST_2( lu_non_invertible<Mat>() );
  138. CALL_SUBTEST_2( lu_invertible<Mat>() );
  139. CALL_SUBTEST_2( lu_non_invertible<MatC>() );
  140. CALL_SUBTEST_2( lu_invertible<MatC>() );
  141. CALL_SUBTEST_3( qr(Mat(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  142. CALL_SUBTEST_3( qr_invertible<Mat>() );
  143. CALL_SUBTEST_4( qr<Mat>() );
  144. CALL_SUBTEST_4( cod<Mat>() );
  145. CALL_SUBTEST_4( qr_invertible<Mat>() );
  146. CALL_SUBTEST_5( qr<Mat>() );
  147. CALL_SUBTEST_5( qr_invertible<Mat>() );
  148. CALL_SUBTEST_6( selfadjointeigensolver(Mat(s,s)) );
  149. CALL_SUBTEST_7( eigensolver(Mat(s,s)) );
  150. CALL_SUBTEST_8( generalized_eigensolver_real(Mat(s,s)) );
  151. TEST_SET_BUT_UNUSED_VARIABLE(s)
  152. }
  153. 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))) ));
  154. 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))) ));
  155. }