10 #ifndef EIGEN_STABLENORM_H
11 #define EIGEN_STABLENORM_H
13 #if EIGEN_HAS_CXX11_ATOMIC
21 template<
typename ExpressionType,
typename Scalar>
22 inline void stable_norm_kernel(
const ExpressionType& bl, Scalar& ssq, Scalar& scale, Scalar& invScale)
24 Scalar maxCoeff = bl.cwiseAbs().maxCoeff();
28 ssq = ssq * numext::abs2(scale/maxCoeff);
29 Scalar tmp = Scalar(1)/maxCoeff;
30 if(tmp > NumTraits<Scalar>::highest())
32 invScale = NumTraits<Scalar>::highest();
33 scale = Scalar(1)/invScale;
35 else if(maxCoeff>NumTraits<Scalar>::highest())
46 else if(maxCoeff!=maxCoeff)
54 ssq += (bl*invScale).squaredNorm();
57 template<
typename VectorType,
typename RealScalar>
58 void stable_norm_impl_inner_step(
const VectorType &vec, RealScalar& ssq, RealScalar& scale, RealScalar& invScale)
60 typedef typename VectorType::Scalar Scalar;
61 const Index blockSize = 4096;
63 typedef typename internal::nested_eval<VectorType,2>::type VectorTypeCopy;
64 typedef typename internal::remove_all<VectorTypeCopy>::type VectorTypeCopyClean;
65 const VectorTypeCopy copy(vec);
69 || (
int(internal::evaluator<VectorTypeCopyClean>::Alignment)>0)
70 ) && (blockSize*
sizeof(Scalar)*2<EIGEN_STACK_ALLOCATION_LIMIT)
71 && (EIGEN_MAX_STATIC_ALIGN_BYTES>0)
73 typedef typename internal::conditional<CanAlign, Ref<const Matrix<Scalar,Dynamic,1,0,blockSize,1>, internal::evaluator<VectorTypeCopyClean>::Alignment>,
74 typename VectorTypeCopyClean::ConstSegmentReturnType>::type SegmentWrapper;
77 Index bi = internal::first_default_aligned(copy);
79 internal::stable_norm_kernel(copy.head(bi), ssq, scale, invScale);
80 for (; bi<n; bi+=blockSize)
81 internal::stable_norm_kernel(SegmentWrapper(copy.segment(bi,numext::mini(blockSize, n - bi))), ssq, scale, invScale);
84 template<
typename VectorType>
85 typename VectorType::RealScalar
86 stable_norm_impl(
const VectorType &vec,
typename enable_if<VectorType::IsVectorAtCompileTime>::type* = 0 )
94 return abs(vec.coeff(0));
96 typedef typename VectorType::RealScalar RealScalar;
98 RealScalar invScale(1);
101 stable_norm_impl_inner_step(vec, ssq, scale, invScale);
103 return scale * sqrt(ssq);
106 template<
typename MatrixType>
107 typename MatrixType::RealScalar
108 stable_norm_impl(
const MatrixType &mat,
typename enable_if<!MatrixType::IsVectorAtCompileTime>::type* = 0 )
112 typedef typename MatrixType::RealScalar RealScalar;
114 RealScalar invScale(1);
117 for(
Index j=0; j<mat.outerSize(); ++j)
118 stable_norm_impl_inner_step(mat.innerVector(j), ssq, scale, invScale);
119 return scale * sqrt(ssq);
122 template<
typename Derived>
123 inline typename NumTraits<typename traits<Derived>::Scalar>::Real
124 blueNorm_impl(
const EigenBase<Derived>& _vec)
126 typedef typename Derived::RealScalar RealScalar;
139 static const int ibeta = std::numeric_limits<RealScalar>::radix;
140 static const int it = NumTraits<RealScalar>::digits();
141 static const int iemin = std::numeric_limits<RealScalar>::min_exponent;
142 static const int iemax = std::numeric_limits<RealScalar>::max_exponent;
143 static const RealScalar rbig = (std::numeric_limits<RealScalar>::max)();
144 static const RealScalar b1 = RealScalar(pow(RealScalar(ibeta),RealScalar(-((1-iemin)/2))));
145 static const RealScalar b2 = RealScalar(pow(RealScalar(ibeta),RealScalar((iemax + 1 - it)/2)));
146 static const RealScalar s1m = RealScalar(pow(RealScalar(ibeta),RealScalar((2-iemin)/2)));
147 static const RealScalar s2m = RealScalar(pow(RealScalar(ibeta),RealScalar(- ((iemax+it)/2))));
148 static const RealScalar eps = RealScalar(pow(
double(ibeta), 1-it));
149 static const RealScalar relerr = sqrt(eps);
151 const Derived& vec(_vec.derived());
152 Index n = vec.size();
153 RealScalar ab2 = b2 / RealScalar(n);
154 RealScalar asml = RealScalar(0);
155 RealScalar amed = RealScalar(0);
156 RealScalar abig = RealScalar(0);
158 for(
Index j=0; j<vec.outerSize(); ++j)
160 for(
typename Derived::InnerIterator iter(vec, j); iter; ++iter)
162 RealScalar ax = abs(iter.value());
163 if(ax > ab2) abig += numext::abs2(ax*s2m);
164 else if(ax < b1) asml += numext::abs2(ax*s1m);
165 else amed += numext::abs2(ax);
170 if(abig > RealScalar(0))
175 if(amed > RealScalar(0))
183 else if(asml > RealScalar(0))
185 if (amed > RealScalar(0))
188 amed = sqrt(asml) / s1m;
191 return sqrt(asml)/s1m;
195 asml = numext::mini(abig, amed);
196 abig = numext::maxi(abig, amed);
197 if(asml <= abig*relerr)
200 return abig * sqrt(RealScalar(1) + numext::abs2(asml/abig));
215 template<
typename Derived>
216 inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
219 return internal::stable_norm_impl(derived());
231 template<
typename Derived>
235 return internal::blueNorm_impl(*
this);
243 template<
typename Derived>
248 return numext::abs(coeff(0,0));
255 #endif // EIGEN_STABLENORM_H