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 typedef typename VectorType::Scalar Scalar;
52 const Index blockSize = 4096;
54 typedef typename internal::nested_eval<VectorType, 2>::type VectorTypeCopy;
55 typedef internal::remove_all_t<VectorTypeCopy> VectorTypeCopyClean;
56 const VectorTypeCopy copy(vec);
61 (
int(internal::evaluator<VectorTypeCopyClean>::Alignment) > 0)
63 (blockSize *
sizeof(Scalar) * 2 < EIGEN_STACK_ALLOCATION_LIMIT) &&
64 (EIGEN_MAX_STATIC_ALIGN_BYTES >
67 typedef std::conditional_t<
69 Ref<const Matrix<Scalar, Dynamic, 1, 0, blockSize, 1>, internal::evaluator<VectorTypeCopyClean>::Alignment>,
70 typename VectorTypeCopyClean::ConstSegmentReturnType>
74 Index bi = internal::first_default_aligned(copy);
75 if (bi > 0) internal::stable_norm_kernel(copy.head(bi), ssq, scale, invScale);
76 for (; bi < n; bi += blockSize)
77 internal::stable_norm_kernel(SegmentWrapper(copy.segment(bi, numext::mini(blockSize, n - bi))), ssq, scale,
81template <
typename VectorType>
82typename VectorType::RealScalar stable_norm_impl(
const VectorType& vec,
83 std::enable_if_t<VectorType::IsVectorAtCompileTime>* = 0) {
89 if (n == 1)
return abs(vec.coeff(0));
91 typedef typename VectorType::RealScalar RealScalar;
93 RealScalar invScale(1);
96 stable_norm_impl_inner_step(vec, ssq, scale, invScale);
98 return scale *
sqrt(ssq);
101template <
typename MatrixType>
102typename MatrixType::RealScalar stable_norm_impl(
const MatrixType& mat,
103 std::enable_if_t<!MatrixType::IsVectorAtCompileTime>* = 0) {
106 typedef typename MatrixType::RealScalar RealScalar;
108 RealScalar invScale(1);
111 for (Index j = 0; j < mat.outerSize(); ++j) stable_norm_impl_inner_step(mat.innerVector(j), ssq, scale, invScale);
112 return scale *
sqrt(ssq);
115template <
typename Derived>
116inline typename NumTraits<typename traits<Derived>::Scalar>::Real blueNorm_impl(
const EigenBase<Derived>& _vec) {
117 typedef typename Derived::RealScalar RealScalar;
130 static const int ibeta = std::numeric_limits<RealScalar>::radix;
131 static const int it = NumTraits<RealScalar>::digits();
132 static const int iemin = NumTraits<RealScalar>::min_exponent();
133 static const int iemax = NumTraits<RealScalar>::max_exponent();
134 static const RealScalar rbig = NumTraits<RealScalar>::highest();
135 static const RealScalar b1 =
136 RealScalar(pow(RealScalar(ibeta), RealScalar(-((1 - iemin) / 2))));
137 static const RealScalar b2 =
138 RealScalar(pow(RealScalar(ibeta), RealScalar((iemax + 1 - it) / 2)));
139 static const RealScalar s1m =
140 RealScalar(pow(RealScalar(ibeta), RealScalar((2 - iemin) / 2)));
141 static const RealScalar s2m =
142 RealScalar(pow(RealScalar(ibeta), RealScalar(-((iemax + it) / 2))));
143 static const RealScalar eps = RealScalar(pow(
double(ibeta), 1 - it));
144 static const RealScalar relerr =
sqrt(eps);
146 const Derived& vec(_vec.derived());
147 Index n = vec.size();
148 RealScalar ab2 = b2 / RealScalar(n);
149 RealScalar asml = RealScalar(0);
150 RealScalar amed = RealScalar(0);
151 RealScalar abig = RealScalar(0);
153 for (Index j = 0; j < vec.outerSize(); ++j) {
154 for (
typename Derived::InnerIterator iter(vec, j); iter; ++iter) {
155 RealScalar ax =
abs(iter.value());
157 abig += numext::abs2(ax * s2m);
159 asml += numext::abs2(ax * s1m);
161 amed += numext::abs2(ax);
164 if (amed != amed)
return amed;
165 if (abig > RealScalar(0)) {
169 if (amed > RealScalar(0)) {
174 }
else if (asml > RealScalar(0)) {
175 if (amed > RealScalar(0)) {
177 amed =
sqrt(asml) / s1m;
179 return sqrt(asml) / s1m;
182 asml = numext::mini(abig, amed);
183 abig = numext::maxi(abig, amed);
184 if (asml <= abig * relerr)
187 return abig *
sqrt(RealScalar(1) + numext::abs2(asml / abig));
202template <
typename Derived>
204 return internal::stable_norm_impl(derived());
216template <
typename Derived>
218 return internal::blueNorm_impl(*
this);
226template <
typename Derived>
229 return numext::abs(coeff(0, 0));
231 return this->cwiseAbs().redux(internal::scalar_hypot_op<RealScalar>());
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
const unsigned int DirectAccessBit
Definition Constants.h:159
Namespace containing all symbols from the Eigen library.
Definition Core:137
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)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:523