10#ifndef EIGEN_STABLENORM_H
11#define EIGEN_STABLENORM_H
14#include "./InternalHeaderCheck.h"
20template <
typename ExpressionType,
typename Scalar>
21inline void stable_norm_kernel(
const ExpressionType& bl, Scalar& ssq, Scalar& scale, Scalar& invScale) {
22 Scalar maxCoeff = bl.cwiseAbs().maxCoeff();
24 if (maxCoeff > scale) {
25 ssq = ssq * numext::abs2(scale / maxCoeff);
26 Scalar tmp = Scalar(1) / maxCoeff;
27 if (tmp > NumTraits<Scalar>::highest()) {
28 invScale = NumTraits<Scalar>::highest();
29 scale = Scalar(1) / invScale;
30 }
else if (maxCoeff > NumTraits<Scalar>::highest())
38 }
else if (maxCoeff != maxCoeff)
45 if (scale > Scalar(0))
46 ssq += (bl * invScale).squaredNorm();
49template <
typename VectorType,
typename RealScalar>
50void stable_norm_impl_inner_step(
const VectorType& vec, RealScalar& ssq, RealScalar& scale, RealScalar& invScale) {
51 const Index blockSize = 4096;
54 Index blockEnd = numext::round_down(n, blockSize);
55 for (
Index i = 0; i < blockEnd; i += blockSize) {
56 internal::stable_norm_kernel(vec.template segment<blockSize>(i), ssq, scale, invScale);
59 internal::stable_norm_kernel(vec.tail(n - blockEnd), ssq, scale, invScale);
63template <
typename VectorType>
64typename VectorType::RealScalar stable_norm_impl(
const VectorType& vec,
65 std::enable_if_t<VectorType::IsVectorAtCompileTime>* = 0) {
70 if (EIGEN_PREDICT_FALSE(n == 1))
return abs(vec.coeff(0));
72 typedef typename VectorType::RealScalar RealScalar;
74 RealScalar invScale(1);
77 stable_norm_impl_inner_step(vec, ssq, scale, invScale);
79 return scale *
sqrt(ssq);
82template <
typename MatrixType>
83typename MatrixType::RealScalar stable_norm_impl(
const MatrixType& mat,
84 std::enable_if_t<!MatrixType::IsVectorAtCompileTime>* = 0) {
87 typedef typename MatrixType::RealScalar RealScalar;
89 RealScalar invScale(1);
92 for (
Index j = 0; j < mat.outerSize(); ++j) stable_norm_impl_inner_step(mat.innerVector(j), ssq, scale, invScale);
93 return scale *
sqrt(ssq);
96template <
typename Derived>
97inline typename NumTraits<typename traits<Derived>::Scalar>::Real blueNorm_impl(
const EigenBase<Derived>& _vec) {
98 typedef typename Derived::RealScalar RealScalar;
111 static const int ibeta = std::numeric_limits<RealScalar>::radix;
112 static const int it = NumTraits<RealScalar>::digits();
113 static const int iemin = NumTraits<RealScalar>::min_exponent();
114 static const int iemax = NumTraits<RealScalar>::max_exponent();
115 static const RealScalar rbig = NumTraits<RealScalar>::highest();
116 static const RealScalar b1 =
117 RealScalar(
pow(RealScalar(ibeta), RealScalar(-((1 - iemin) / 2))));
118 static const RealScalar b2 =
119 RealScalar(
pow(RealScalar(ibeta), RealScalar((iemax + 1 - it) / 2)));
120 static const RealScalar s1m =
121 RealScalar(
pow(RealScalar(ibeta), RealScalar((2 - iemin) / 2)));
122 static const RealScalar s2m =
123 RealScalar(
pow(RealScalar(ibeta), RealScalar(-((iemax + it) / 2))));
124 static const RealScalar eps = RealScalar(
pow(
double(ibeta), 1 - it));
125 static const RealScalar relerr =
sqrt(eps);
127 const Derived& vec(_vec.derived());
128 Index n = vec.size();
129 RealScalar ab2 = b2 / RealScalar(n);
130 RealScalar asml = RealScalar(0);
131 RealScalar amed = RealScalar(0);
132 RealScalar abig = RealScalar(0);
134 for (
Index j = 0; j < vec.outerSize(); ++j) {
135 for (
typename Derived::InnerIterator iter(vec, j); iter; ++iter) {
136 RealScalar ax =
abs(iter.value());
138 abig += numext::abs2(ax * s2m);
140 asml += numext::abs2(ax * s1m);
142 amed += numext::abs2(ax);
145 if (amed != amed)
return amed;
146 if (abig > RealScalar(0)) {
150 if (amed > RealScalar(0)) {
155 }
else if (asml > RealScalar(0)) {
156 if (amed > RealScalar(0)) {
158 amed =
sqrt(asml) / s1m;
160 return sqrt(asml) / s1m;
163 asml = numext::mini(abig, amed);
164 abig = numext::maxi(abig, amed);
165 if (asml <= abig * relerr)
168 return abig *
sqrt(RealScalar(1) + numext::abs2(asml / abig));
183template <
typename Derived>
185 return internal::stable_norm_impl(derived());
197template <
typename Derived>
199 return internal::blueNorm_impl(*
this);
207template <
typename Derived>
210 return numext::abs(coeff(0, 0));
212 return this->
cwiseAbs().redux(internal::scalar_hypot_op<RealScalar>());
const GlobalUnaryPowReturnType< Derived, ScalarExponent > pow(const Eigen::ArrayBase< Derived > &x, const ScalarExponent &exponent)
RealScalar blueNorm() const
Definition StableNorm.h:198
RealScalar hypotNorm() const
Definition StableNorm.h:208
RealScalar stableNorm() const
Definition StableNorm.h:184
const CwiseAbsReturnType cwiseAbs() const
Definition MatrixBase.h:34
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:232