Eigen-Contrib  5.0.1-dev+master
 
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
23template <>
24struct ScalarBinaryOpTraits<mpfr::mpreal, mpfr::mpreal, internal::scalar_product_op<mpfr::mpreal, mpfr::mpreal>> {
25 using ReturnType = mpfr::mpreal;
26 static constexpr bool IsCommutative = true;
27};
28
72
73template <>
74struct NumTraits<mpfr::mpreal> : GenericNumTraits<mpfr::mpreal> {
75 enum {
76 IsInteger = 0,
77 IsSigned = 1,
78 IsComplex = 0,
79 RequireInitialization = 1,
80 ReadCost = HugeCost,
81 AddCost = HugeCost,
82 MulCost = HugeCost
83 };
84
85 using Real = mpfr::mpreal;
86 using NonInteger = mpfr::mpreal;
87
88 static inline Real highest(long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::maxval(Precision); }
89 static inline Real lowest(long Precision = mpfr::mpreal::get_default_prec()) { return -mpfr::maxval(Precision); }
90
91 // Constants
92 static inline Real Pi(long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_pi(Precision); }
93 static inline Real Euler(long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_euler(Precision); }
94 static inline Real Log2(long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_log2(Precision); }
95 static inline Real Catalan(long Precision = mpfr::mpreal::get_default_prec()) {
96 return mpfr::const_catalan(Precision);
97 }
98
99 static inline Real epsilon(long Precision = mpfr::mpreal::get_default_prec()) {
100 return mpfr::machine_epsilon(Precision);
101 }
102 static inline Real epsilon(const Real& x) { return mpfr::machine_epsilon(x); }
103
104#ifdef MPREAL_HAVE_DYNAMIC_STD_NUMERIC_LIMITS
105 static inline int digits10(long Precision = mpfr::mpreal::get_default_prec()) {
106 return std::numeric_limits<Real>::digits10(Precision);
107 }
108 static inline int digits10(const Real& x) { return std::numeric_limits<Real>::digits10(x); }
109
110 static inline int max_digits10(long Precision = mpfr::mpreal::get_default_prec()) {
111 return std::numeric_limits<Real>::max_digits10(Precision);
112 }
113
114 static inline int digits() { return std::numeric_limits<Real>::digits(); }
115 static inline int digits(const Real& x) { return std::numeric_limits<Real>::digits(x); }
116#endif
117
118 static inline Real dummy_precision() {
119 mpfr_prec_t weak_prec = ((mpfr::mpreal::get_default_prec() - 1) * 90) / 100;
120 return mpfr::machine_epsilon(weak_prec);
121 }
122};
123
124namespace internal {
125
126template <>
127inline mpfr::mpreal random<mpfr::mpreal>() {
128 return mpfr::random();
129}
130
131template <>
132inline mpfr::mpreal random<mpfr::mpreal>(const mpfr::mpreal& a, const mpfr::mpreal& b) {
133 return a + (b - a) * random<mpfr::mpreal>();
134}
135
136inline bool isMuchSmallerThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& eps) {
137 return mpfr::abs(a) <= mpfr::abs(b) * eps;
138}
139
140inline bool isApprox(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& eps) {
141 return mpfr::isEqualFuzzy(a, b, eps);
142}
143
144inline bool isApproxOrLessThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& eps) {
145 return a <= b || mpfr::isEqualFuzzy(a, b, eps);
146}
147
148template <>
149inline long double cast<mpfr::mpreal, long double>(const mpfr::mpreal& x) {
150 return x.toLDouble();
151}
152
153template <>
154inline double cast<mpfr::mpreal, double>(const mpfr::mpreal& x) {
155 return x.toDouble();
156}
157
158template <>
159inline long cast<mpfr::mpreal, long>(const mpfr::mpreal& x) {
160 return x.toLong();
161}
162
163template <>
164inline int cast<mpfr::mpreal, int>(const mpfr::mpreal& x) {
165 return int(x.toLong());
166}
167
168// Specialize GEBP kernel and traits for mpreal (no need for peeling, nor complicated stuff)
169// This also permits to directly call mpfr's routines and avoid many temporaries produced by mpreal
170template <>
171class gebp_traits<mpfr::mpreal, mpfr::mpreal, false, false> {
172 public:
173 using ResScalar = mpfr::mpreal;
174 enum {
175 Vectorizable = false,
176 LhsPacketSize = 1,
177 RhsPacketSize = 1,
178 ResPacketSize = 1,
179 NumberOfRegisters = 1,
180 nr = 1,
181 mr = 1,
182 LhsProgress = 1,
183 RhsProgress = 1
184 };
185 using LhsPacket = ResScalar;
186 using RhsPacket = ResScalar;
187 using ResPacket = ResScalar;
188 using LhsPacket4Packing = LhsPacket;
189};
190
191template <typename Index, typename DataMapper, bool ConjugateLhs, bool ConjugateRhs>
192struct gebp_kernel<mpfr::mpreal, mpfr::mpreal, Index, DataMapper, 1, 1, ConjugateLhs, ConjugateRhs> {
193 using mpreal = mpfr::mpreal;
194
195 EIGEN_DONT_INLINE void operator()(const DataMapper& res, const mpreal* blockA, const mpreal* blockB, Index rows,
196 Index depth, Index cols, const mpreal& alpha, Index strideA = -1,
197 Index strideB = -1, Index offsetA = 0, Index offsetB = 0) {
198 if (rows == 0 || cols == 0 || depth == 0) return;
199
200 mpreal acc1(0, mpfr_get_prec(blockA[0].mpfr_srcptr())), tmp(0, mpfr_get_prec(blockA[0].mpfr_srcptr()));
201
202 if (strideA == -1) strideA = depth;
203 if (strideB == -1) strideB = depth;
204
205 for (Index i = 0; i < rows; ++i) {
206 for (Index j = 0; j < cols; ++j) {
207 const mpreal* A = blockA + i * strideA + offsetA;
208 const mpreal* B = blockB + j * strideB + offsetB;
209
210 acc1 = 0;
211 for (Index k = 0; k < depth; k++) {
212 mpfr_mul(tmp.mpfr_ptr(), A[k].mpfr_srcptr(), B[k].mpfr_srcptr(), mpreal::get_default_rnd());
213 mpfr_add(acc1.mpfr_ptr(), acc1.mpfr_ptr(), tmp.mpfr_ptr(), mpreal::get_default_rnd());
214 }
215
216 mpfr_mul(acc1.mpfr_ptr(), acc1.mpfr_srcptr(), alpha.mpfr_srcptr(), mpreal::get_default_rnd());
217 mpfr_add(res(i, j).mpfr_ptr(), res(i, j).mpfr_srcptr(), acc1.mpfr_srcptr(), mpreal::get_default_rnd());
218 }
219 }
220 }
221};
222} // end namespace internal
223} // namespace Eigen
224
225#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
226
227#endif // EIGEN_MPREALSUPPORT_MODULE_H
Namespace containing all symbols from the Eigen library.