Loading...
Searching...
No Matches
gpr::extended::scaled Namespace Reference

Classes

struct  ScaledNorm

Functions

int leadingExponent (long double value)
int leadingExponent (const DoubleDouble &value)
long double scaleByPowerOfTwo (long double value, int shift, bool &exact)
DoubleDouble scaleByPowerOfTwo (const DoubleDouble &value, int shift, bool &exact)
template<typename Work>
Work limbDenormMin ()
template<typename Work, typename Plain>
ScaledNorm< Work > scaledFrobeniusNorm (const Plain &matrix, long long norm_ops)

Variables

constexpr int kSafeLow = -450
constexpr int kSafeHigh = 450
constexpr int kDroppedExp = -460

Function Documentation

◆ leadingExponent() [1/2]

int gpr::extended::scaled::leadingExponent ( const DoubleDouble & value)
inline

Definition at line 584 of file ExtendedReal.h.

585{
586 int exponent = 0;
587 std::frexp(value.hi != 0.0 ? value.hi : value.lo, &exponent);
588 return exponent;
589}

◆ leadingExponent() [2/2]

int gpr::extended::scaled::leadingExponent ( long double value)
inline

Definition at line 577 of file ExtendedReal.h.

578{
579 int exponent = 0;
580 std::frexp(value, &exponent);
581 return exponent;
582}

◆ limbDenormMin()

template<typename Work>
Work gpr::extended::scaled::limbDenormMin ( )

Definition at line 614 of file ExtendedReal.h.

615{
616 if constexpr (std::is_same_v<Work, DoubleDouble>)
617 return Work(std::numeric_limits<double>::denorm_min());
618 else
619 return std::numeric_limits<Work>::denorm_min();
620}

◆ scaleByPowerOfTwo() [1/2]

DoubleDouble gpr::extended::scaled::scaleByPowerOfTwo ( const DoubleDouble & value,
int shift,
bool & exact )
inline

Definition at line 601 of file ExtendedReal.h.

603{
604 const double hi = std::ldexp(value.hi, shift);
605 const double lo = std::ldexp(value.lo, shift);
606 if (std::ldexp(hi, -shift) != value.hi ||
607 std::ldexp(lo, -shift) != value.lo)
608 exact = false;
609 return DoubleDouble(hi, lo);
610}

◆ scaleByPowerOfTwo() [2/2]

long double gpr::extended::scaled::scaleByPowerOfTwo ( long double value,
int shift,
bool & exact )
inline

Definition at line 594 of file ExtendedReal.h.

595{
596 const long double scaled = std::ldexp(value, shift);
597 if (std::ldexp(scaled, -shift) != value) exact = false;
598 return scaled;
599}

◆ scaledFrobeniusNorm()

template<typename Work, typename Plain>
ScaledNorm< Work > gpr::extended::scaled::scaledFrobeniusNorm ( const Plain & matrix,
long long norm_ops )

Definition at line 660 of file ExtendedReal.h.

661{
662 using std::abs;
663 using std::sqrt;
664 bool finite = true;
665 bool nonzero = false;
666 bool safe = true;
667 int top = 0;
668 for (Eigen::Index j = 0; j < matrix.cols(); ++j) {
669 for (Eigen::Index i = 0; i < matrix.rows(); ++i) {
670 const Work value = matrix(i, j);
671 if (!workingFinite(value)) {
672 finite = false;
673 continue;
674 }
675 if (value == Work(0)) continue;
676 const int exponent = leadingExponent(value);
677 if (exponent - 1 < kSafeLow || exponent > kSafeHigh) safe = false;
678 top = nonzero ? std::max(top, exponent) : exponent;
679 nonzero = true;
680 }
681 }
682 if (!finite || !nonzero || safe)
683 return ScaledNorm<Work>{Work(matrix.norm()), 0, norm_ops};
684
685 const Work dropped_below(std::ldexp(1.0, kDroppedExp));
686 Work sum(0);
687 for (Eigen::Index j = 0; j < matrix.cols(); ++j) {
688 for (Eigen::Index i = 0; i < matrix.rows(); ++i) {
689 const Work value = matrix(i, j);
690 if (value == Work(0)) continue;
691 bool exact = true;
692 const Work y = abs(scaleByPowerOfTwo(value, -top, exact));
693 if (y < dropped_below) continue;
694 sum += y * y;
695 }
696 }
697 sum += Work(std::ldexp(static_cast<double>(matrix.size()), -917));
698 return ScaledNorm<Work>{sqrt(sum), top, norm_ops + 1};
699}
@ y
Definition Enums.h:28
constexpr int kDroppedExp
constexpr int kSafeLow
constexpr int kSafeHigh
long double scaleByPowerOfTwo(long double value, int shift, bool &exact)
int leadingExponent(long double value)
DoubleDouble sqrt(DoubleDouble a)
DoubleDouble abs(DoubleDouble a)
bool workingFinite(Work value)

Variable Documentation

◆ kDroppedExp

int gpr::extended::scaled::kDroppedExp = -460
inlineconstexpr

Definition at line 627 of file ExtendedReal.h.

◆ kSafeHigh

int gpr::extended::scaled::kSafeHigh = 450
inlineconstexpr

Definition at line 625 of file ExtendedReal.h.

◆ kSafeLow

int gpr::extended::scaled::kSafeLow = -450
inlineconstexpr

Definition at line 624 of file ExtendedReal.h.