Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
MPRealSupport
1// This file is part of a joint effort between Eigen, a lightweight C++ template library
2// for linear algebra, and MPFR C++, a C++ interface to MPFR library (http://www.holoborodko.com/pavel/)
3//
4// Copyright (C) 2010-2012 Pavel Holoborodko <pavel@holoborodko.com>
5// Copyright (C) 2010 Konstantin Holoborodko <konstantin@holoborodko.com>
6// Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
7//
8// This Source Code Form is subject to the terms of the Mozilla
9// Public License v. 2.0. If a copy of the MPL was not distributed
10// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11// SPDX-License-Identifier: MPL-2.0
12
13#ifndef EIGEN_MPREALSUPPORT_MODULE_H
14#define EIGEN_MPREALSUPPORT_MODULE_H
15
16#include "../../Eigen/Core"
17#include <mpreal.h>
18
19#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
20
21namespace Eigen {
22
64
65template <>
66struct NumTraits<mpfr::mpreal> : GenericNumTraits<mpfr::mpreal> {
67 enum {
68 IsInteger = 0,
69 IsSigned = 1,
70 IsComplex = 0,
71 RequireInitialization = 1,
72 ReadCost = HugeCost,
73 AddCost = HugeCost,
74 MulCost = HugeCost
75 };
76
77 typedef mpfr::mpreal Real;
78 typedef mpfr::mpreal NonInteger;
79
80 static inline Real highest(long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::maxval(Precision); }
81 static inline Real lowest(long Precision = mpfr::mpreal::get_default_prec()) { return -mpfr::maxval(Precision); }
82
83 // Constants
84 static inline Real Pi(long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_pi(Precision); }
85 static inline Real Euler(long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_euler(Precision); }
86 static inline Real Log2(long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_log2(Precision); }
87 static inline Real Catalan(long Precision = mpfr::mpreal::get_default_prec()) {
88 return mpfr::const_catalan(Precision);
89 }
90
91 static inline Real epsilon(long Precision = mpfr::mpreal::get_default_prec()) {
92 return mpfr::machine_epsilon(Precision);
93 }
94 static inline Real epsilon(const Real& x) { return mpfr::machine_epsilon(x); }
95
96#ifdef MPREAL_HAVE_DYNAMIC_STD_NUMERIC_LIMITS
97 static inline int digits10(long Precision = mpfr::mpreal::get_default_prec()) {
98 return std::numeric_limits<Real>::digits10(Precision);
99 }
100 static inline int digits10(const Real& x) { return std::numeric_limits<Real>::digits10(x); }
101
102 static inline int max_digits10(long Precision = mpfr::mpreal::get_default_prec()) {
103 return std::numeric_limits<Real>::max_digits10(Precision);
104 }
105
106 static inline int digits() { return std::numeric_limits<Real>::digits(); }
107 static inline int digits(const Real& x) { return std::numeric_limits<Real>::digits(x); }
108#endif
109
110 static inline Real dummy_precision() {
111 mpfr_prec_t weak_prec = ((mpfr::mpreal::get_default_prec() - 1) * 90) / 100;
112 return mpfr::machine_epsilon(weak_prec);
113 }
114};
115
116namespace internal {
117
118template <>
119inline mpfr::mpreal random<mpfr::mpreal>() {
120 return mpfr::random();
121}
122
123template <>
124inline mpfr::mpreal random<mpfr::mpreal>(const mpfr::mpreal& a, const mpfr::mpreal& b) {
125 return a + (b - a) * random<mpfr::mpreal>();
126}
127
128inline bool isMuchSmallerThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& eps) {
129 return mpfr::abs(a) <= mpfr::abs(b) * eps;
130}
131
132inline bool isApprox(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& eps) {
133 return mpfr::isEqualFuzzy(a, b, eps);
134}
135
136inline bool isApproxOrLessThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& eps) {
137 return a <= b || mpfr::isEqualFuzzy(a, b, eps);
138}
139
140template <>
141inline long double cast<mpfr::mpreal, long double>(const mpfr::mpreal& x) {
142 return x.toLDouble();
143}
144
145template <>
146inline double cast<mpfr::mpreal, double>(const mpfr::mpreal& x) {
147 return x.toDouble();
148}
149
150template <>
151inline long cast<mpfr::mpreal, long>(const mpfr::mpreal& x) {
152 return x.toLong();
153}
154
155template <>
156inline int cast<mpfr::mpreal, int>(const mpfr::mpreal& x) {
157 return int(x.toLong());
158}
159
160// Specialize GEBP kernel and traits for mpreal (no need for peeling, nor complicated stuff)
161// This also permits to directly call mpfr's routines and avoid many temporaries produced by mpreal
162template <>
163class gebp_traits<mpfr::mpreal, mpfr::mpreal, false, false> {
164 public:
165 typedef mpfr::mpreal ResScalar;
166 enum {
167 Vectorizable = false,
168 LhsPacketSize = 1,
169 RhsPacketSize = 1,
170 ResPacketSize = 1,
171 NumberOfRegisters = 1,
172 nr = 1,
173 mr = 1,
174 LhsProgress = 1,
175 RhsProgress = 1
176 };
177 typedef ResScalar LhsPacket;
178 typedef ResScalar RhsPacket;
179 typedef ResScalar ResPacket;
180 typedef LhsPacket LhsPacket4Packing;
181};
182
183template <typename Index, typename DataMapper, bool ConjugateLhs, bool ConjugateRhs>
184struct gebp_kernel<mpfr::mpreal, mpfr::mpreal, Index, DataMapper, 1, 1, ConjugateLhs, ConjugateRhs> {
185 typedef mpfr::mpreal mpreal;
186
187 EIGEN_DONT_INLINE void operator()(const DataMapper& res, const mpreal* blockA, const mpreal* blockB, Index rows,
188 Index depth, Index cols, const mpreal& alpha, Index strideA = -1,
189 Index strideB = -1, Index offsetA = 0, Index offsetB = 0) {
190 if (rows == 0 || cols == 0 || depth == 0) return;
191
192 mpreal acc1(0, mpfr_get_prec(blockA[0].mpfr_srcptr())), tmp(0, mpfr_get_prec(blockA[0].mpfr_srcptr()));
193
194 if (strideA == -1) strideA = depth;
195 if (strideB == -1) strideB = depth;
196
197 for (Index i = 0; i < rows; ++i) {
198 for (Index j = 0; j < cols; ++j) {
199 const mpreal* A = blockA + i * strideA + offsetA;
200 const mpreal* B = blockB + j * strideB + offsetB;
201
202 acc1 = 0;
203 for (Index k = 0; k < depth; k++) {
204 mpfr_mul(tmp.mpfr_ptr(), A[k].mpfr_srcptr(), B[k].mpfr_srcptr(), mpreal::get_default_rnd());
205 mpfr_add(acc1.mpfr_ptr(), acc1.mpfr_ptr(), tmp.mpfr_ptr(), mpreal::get_default_rnd());
206 }
207
208 mpfr_mul(acc1.mpfr_ptr(), acc1.mpfr_srcptr(), alpha.mpfr_srcptr(), mpreal::get_default_rnd());
209 mpfr_add(res(i, j).mpfr_ptr(), res(i, j).mpfr_srcptr(), acc1.mpfr_srcptr(), mpreal::get_default_rnd());
210 }
211 }
212 }
213};
214} // end namespace internal
215} // namespace Eigen
216
217#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
218
219#endif // EIGEN_MPREALSUPPORT_MODULE_H
Namespace containing all symbols from the Eigen library.