40 const unsigned int n =
p.Nrow();
43 for (
unsigned int i = 0; i <
n; i++) {
45 for (
unsigned int j = 0; j <
n; j++)
46 rowSum += std::abs(
p(i, j));
47 bound = std::max(bound, rowSum);
49 const double shift = (epspdf + 1.e-5 + (
n + 1.) *
n * std::numeric_limits<double>::epsilon()) * bound;
52 std::vector<double>
l(
p.Data(),
p.Data() +
p.size());
53 for (
unsigned int i = 0; i <
n; i++) {
54 double *li =
l.data() + i * (i + 1) / 2;
55 for (
unsigned int j = 0; j <= i; j++) {
56 const double *lj =
l.data() + j * (j + 1) / 2;
60 for (
unsigned int k = 0; k < j; k++)
65 li[i] = std::sqrt(
sum);
90 if (err.
size() == 1 && err(0, 0) < prec.
Eps()) {
94 if (err.
size() == 1 && err(0, 0) > prec.
Eps()) {
99 double epspdf = std::max(1.e-12, prec.
Eps2());
100 double dgmin = err(0, 0);
102 for (
unsigned int i = 0; i < err.
Nrow(); i++) {
104 print.
Warn(
"non-positive diagonal element in covariance matrix[", i,
"] =", err(i, i));
106 if (err(i, i) < dgmin)
112 dg = 0.5 + epspdf - dgmin;
115 print.
Warn(
"Added to diagonal of Error matrix a value", dg);
122 for (
unsigned int i = 0; i < err.
Nrow(); i++) {
126 s(i) = 1. / std::sqrt(err(i, i));
127 for (
unsigned int j = 0; j <= i; j++) {
128 p(i, j) = err(i, j) * s(i) * s(j);
133 if (IsCertainlyPosDef(
p, epspdf))
137 double pmin = eval(0);
138 double pmax = eval(eval.
size() - 1);
140 pmax = std::max(std::fabs(pmax), 1.);
141 if (pmin > epspdf * pmax)
144 double pAdd = 0.001 * pmax - pmin;
146 for (
unsigned int i = 0; i < err.
Nrow(); i++)
147 err(i, i) *= (1. + pAdd);
149 print.
Debug([&](std::ostream &os) {
150 os <<
"Eigenvalues:";
151 for (
unsigned i = 0; i < err.
Nrow(); ++i)
152 os <<
"\n " << eval(i);
157 print.
Warn(
"Matrix forced pos-def by adding to diagonal", pAdd);
winID h TVirtualViewer3D TVirtualGLPainter p
Class describing a symmetric matrix of size n.
unsigned int Nrow() const
unsigned int size() const
unsigned int size() const
MinimumError keeps the inv.
MinimumState keeps the information (position, Gradient, 2nd deriv, etc) after one minimization step (...
const MinimumError & Error() const
const MinimumParameters & Parameters() const
const FunctionGradient & Gradient() const
Sets the relative floating point (double) arithmetic precision.
double Eps() const
eps returns the smallest possible number so that 1.+eps > 1.
double Eps2() const
eps2 returns 2*sqrt(eps)
MinimumState operator()(const MinimumState &, const MnMachinePrecision &) const
void Debug(const Ts &... args)
void Warn(const Ts &... args)
LAVector eigenvalues(const LASymMatrix &)
LASymMatrix MnAlgebraicSymMatrix
static uint64_t sum(uint64_t i)