|
| 1 | +/* |
| 2 | + * This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad). |
| 3 | + * Copyright (c) 2022-2026 Jochen Meidow, Fraunhofer IOSB |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef ISCOV_H |
| 20 | +#define ISCOV_H |
| 21 | + |
| 22 | + |
| 23 | +#include <Eigen/Core> |
| 24 | +#include <Eigen/Eigenvalues> |
| 25 | + |
| 26 | + |
| 27 | +namespace Stats { |
| 28 | + |
| 29 | +using Eigen::MatrixXd; |
| 30 | + |
| 31 | + |
| 32 | +//! Check if matrix MM is a proper covariance matrix |
| 33 | +[[maybe_unused, nodiscard]] static bool isCovMat( const MatrixXd & MM ) |
| 34 | +{ |
| 35 | + // qDebug() << Q_FUNC_INFO; |
| 36 | + const Eigen::SelfAdjointEigenSolver<MatrixXd> eig( MM, Eigen::ComputeEigenvectors); |
| 37 | + Eigen::VectorXcd ev = eig.eigenvalues(); |
| 38 | + |
| 39 | + constexpr double threshold = 1e-6; |
| 40 | +#ifdef QT_DEBUG |
| 41 | + if ( (ev.real().array() < -threshold ).any() ) { |
| 42 | + for ( Eigen::Index i=0; i< ev.size(); i++) { |
| 43 | + qDebug().noquote() << QStringLiteral( "(%1,%2)") |
| 44 | + .arg( ev(i).real() ) |
| 45 | + .arg( ev(i).imag() ); |
| 46 | + } |
| 47 | + } |
| 48 | +#endif |
| 49 | + |
| 50 | + return ( ev.real().array() >= -threshold ).all(); |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace Stats |
| 54 | + |
| 55 | +#endif // ISCOV_H |
0 commit comments