将 libquadmath 与特征一起使用
Using libquadmath with eigen
我想将 __float128 与 eigen 的 erf() 函数一起使用,但是 found out 目前它只支持 float 和 double:
This function supports only float and double scalar types in c++11
mode. To support other scalar types, or float/double in non c++11
mode, the user has to provide implementations of erf(T) for any scalar
type T to be supported.
因为我想使用 __float128,如果可能的话,我想依赖 libquadmath
s erfq
implementation。但是怎么做呢?
我目前能想到的唯一(丑陋?)方法是使用特征 unaryExpr()
。还有其他可能吗?
您可以专门化 Eigen::internal::erf_impl
(当然对于任何其他功能也类似):
#include <quadmath.h>
#include <iostream>
#include <unsupported/Eigen/SpecialFunctions>
namespace Eigen { namespace internal {
template<>
struct erf_impl<__float128> {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE __float128 run(__float128 x) { return ::erfq(x); }
};
}}
int main()
{
typedef Eigen::Array<__float128, Eigen::Dynamic, 1> ArrayXF;
ArrayXF a(4); a << 0, 0.25, 0.5, 0.75;
ArrayXF b = a.erf();
for(int i=0; i<4; ++i){
char buf[100];
quadmath_snprintf(buf, 100, "%.50Qe", b[i]); std::cout << buf << '\n';
}
}
输出:
0.00000000000000000000000000000000000000000000000000e+00
2.76326390168236932985068267764815703534647315720851e-01
5.20499877813046537682746653891964513119913394193564e-01
7.11155633653515131598937834591410814324096358715387e-01
我想将 __float128 与 eigen 的 erf() 函数一起使用,但是 found out 目前它只支持 float 和 double:
This function supports only float and double scalar types in c++11 mode. To support other scalar types, or float/double in non c++11 mode, the user has to provide implementations of erf(T) for any scalar type T to be supported.
因为我想使用 __float128,如果可能的话,我想依赖 libquadmath
s erfq
implementation。但是怎么做呢?
我目前能想到的唯一(丑陋?)方法是使用特征 unaryExpr()
。还有其他可能吗?
您可以专门化 Eigen::internal::erf_impl
(当然对于任何其他功能也类似):
#include <quadmath.h>
#include <iostream>
#include <unsupported/Eigen/SpecialFunctions>
namespace Eigen { namespace internal {
template<>
struct erf_impl<__float128> {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE __float128 run(__float128 x) { return ::erfq(x); }
};
}}
int main()
{
typedef Eigen::Array<__float128, Eigen::Dynamic, 1> ArrayXF;
ArrayXF a(4); a << 0, 0.25, 0.5, 0.75;
ArrayXF b = a.erf();
for(int i=0; i<4; ++i){
char buf[100];
quadmath_snprintf(buf, 100, "%.50Qe", b[i]); std::cout << buf << '\n';
}
}
输出:
0.00000000000000000000000000000000000000000000000000e+00 2.76326390168236932985068267764815703534647315720851e-01 5.20499877813046537682746653891964513119913394193564e-01 7.11155633653515131598937834591410814324096358715387e-01