mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Clang-format tests, examples, libraries, benchmarks, etc.
This commit is contained in:
committed by
Rasmus Munk Larsen
parent
3252ecc7a4
commit
46e9cdb7fe
@@ -15,16 +15,13 @@
|
||||
|
||||
template <typename MatrixType>
|
||||
void cod() {
|
||||
|
||||
Index rows = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
|
||||
Index cols = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
|
||||
Index cols2 = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
|
||||
Index rank = internal::random<Index>(1, (std::min)(rows, cols) - 1);
|
||||
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime,
|
||||
MatrixType::RowsAtCompileTime>
|
||||
MatrixQType;
|
||||
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> MatrixQType;
|
||||
MatrixType matrix;
|
||||
createRandomPIMatrixOfRank(rank, rows, cols, matrix);
|
||||
CompleteOrthogonalDecomposition<MatrixType> cod(matrix);
|
||||
@@ -42,8 +39,7 @@ void cod() {
|
||||
|
||||
MatrixType t;
|
||||
t.setZero(rows, cols);
|
||||
t.topLeftCorner(rank, rank) =
|
||||
cod.matrixT().topLeftCorner(rank, rank).template triangularView<Upper>();
|
||||
t.topLeftCorner(rank, rank) = cod.matrixT().topLeftCorner(rank, rank).template triangularView<Upper>();
|
||||
|
||||
MatrixType c = q * t * z * cod.colsPermutation().inverse();
|
||||
VERIFY_IS_APPROX(matrix, c);
|
||||
@@ -64,10 +60,7 @@ void cod() {
|
||||
|
||||
template <typename MatrixType, int Cols2>
|
||||
void cod_fixedsize() {
|
||||
enum {
|
||||
Rows = MatrixType::RowsAtCompileTime,
|
||||
Cols = MatrixType::ColsAtCompileTime
|
||||
};
|
||||
enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef CompleteOrthogonalDecomposition<Matrix<Scalar, Rows, Cols> > COD;
|
||||
int rank = internal::random<int>(1, (std::min)(int(Rows), int(Cols)) - 1);
|
||||
@@ -95,18 +88,19 @@ void cod_fixedsize() {
|
||||
VERIFY_IS_APPROX(cod_solution, pinv * rhs);
|
||||
}
|
||||
|
||||
template<typename MatrixType> void qr()
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void qr() {
|
||||
using std::sqrt;
|
||||
|
||||
Index rows = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols2 = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE);
|
||||
Index rank = internal::random<Index>(1, (std::min)(rows, cols)-1);
|
||||
Index rows = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE), cols = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE),
|
||||
cols2 = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
|
||||
Index rank = internal::random<Index>(1, (std::min)(rows, cols) - 1);
|
||||
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef typename MatrixType::RealScalar RealScalar;
|
||||
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> MatrixQType;
|
||||
MatrixType m1;
|
||||
createRandomPIMatrixOfRank(rank,rows,cols,m1);
|
||||
createRandomPIMatrixOfRank(rank, rows, cols, m1);
|
||||
ColPivHouseholderQR<MatrixType> qr(m1);
|
||||
VERIFY_IS_EQUAL(rank, qr.rank());
|
||||
VERIFY_IS_EQUAL(cols - qr.rank(), qr.dimensionOfKernel());
|
||||
@@ -123,8 +117,7 @@ template<typename MatrixType> void qr()
|
||||
|
||||
// Verify that the absolute value of the diagonal elements in R are
|
||||
// non-increasing until they reach the singularity threshold.
|
||||
RealScalar threshold =
|
||||
sqrt(RealScalar(rows)) * numext::abs(r(0, 0)) * NumTraits<Scalar>::epsilon();
|
||||
RealScalar threshold = sqrt(RealScalar(rows)) * numext::abs(r(0, 0)) * NumTraits<Scalar>::epsilon();
|
||||
for (Index i = 0; i < (std::min)(rows, cols) - 1; ++i) {
|
||||
RealScalar x = numext::abs(r(i, i));
|
||||
RealScalar y = numext::abs(r(i + 1, i + 1));
|
||||
@@ -133,8 +126,7 @@ template<typename MatrixType> void qr()
|
||||
for (Index j = 0; j < (std::min)(rows, cols); ++j) {
|
||||
std::cout << "i = " << j << ", |r_ii| = " << numext::abs(r(j, j)) << std::endl;
|
||||
}
|
||||
std::cout << "Failure at i=" << i << ", rank=" << rank
|
||||
<< ", threshold=" << threshold << std::endl;
|
||||
std::cout << "Failure at i=" << i << ", rank=" << rank << ", threshold=" << threshold << std::endl;
|
||||
}
|
||||
VERIFY_IS_APPROX_OR_LESS_THAN(y, x);
|
||||
}
|
||||
@@ -145,43 +137,42 @@ template<typename MatrixType> void qr()
|
||||
MatrixType m2, m3;
|
||||
Index size = rows;
|
||||
do {
|
||||
m1 = MatrixType::Random(size,size);
|
||||
m1 = MatrixType::Random(size, size);
|
||||
qr.compute(m1);
|
||||
} while(!qr.isInvertible());
|
||||
} while (!qr.isInvertible());
|
||||
MatrixType m1_inv = qr.inverse();
|
||||
m3 = m1 * MatrixType::Random(size,cols2);
|
||||
m3 = m1 * MatrixType::Random(size, cols2);
|
||||
m2 = qr.solve(m3);
|
||||
VERIFY_IS_APPROX(m2, m1_inv*m3);
|
||||
VERIFY_IS_APPROX(m2, m1_inv * m3);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename MatrixType, int Cols2> void qr_fixedsize()
|
||||
{
|
||||
using std::sqrt;
|
||||
template <typename MatrixType, int Cols2>
|
||||
void qr_fixedsize() {
|
||||
using std::abs;
|
||||
using std::sqrt;
|
||||
enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef typename MatrixType::RealScalar RealScalar;
|
||||
int rank = internal::random<int>(1, (std::min)(int(Rows), int(Cols))-1);
|
||||
Matrix<Scalar,Rows,Cols> m1;
|
||||
createRandomPIMatrixOfRank(rank,Rows,Cols,m1);
|
||||
ColPivHouseholderQR<Matrix<Scalar,Rows,Cols> > qr(m1);
|
||||
int rank = internal::random<int>(1, (std::min)(int(Rows), int(Cols)) - 1);
|
||||
Matrix<Scalar, Rows, Cols> m1;
|
||||
createRandomPIMatrixOfRank(rank, Rows, Cols, m1);
|
||||
ColPivHouseholderQR<Matrix<Scalar, Rows, Cols> > qr(m1);
|
||||
VERIFY_IS_EQUAL(rank, qr.rank());
|
||||
VERIFY_IS_EQUAL(Cols - qr.rank(), qr.dimensionOfKernel());
|
||||
VERIFY_IS_EQUAL(qr.isInjective(), (rank == Rows));
|
||||
VERIFY_IS_EQUAL(qr.isSurjective(), (rank == Cols));
|
||||
VERIFY_IS_EQUAL(qr.isInvertible(), (qr.isInjective() && qr.isSurjective()));
|
||||
|
||||
Matrix<Scalar,Rows,Cols> r = qr.matrixQR().template triangularView<Upper>();
|
||||
Matrix<Scalar,Rows,Cols> c = qr.householderQ() * r * qr.colsPermutation().inverse();
|
||||
Matrix<Scalar, Rows, Cols> r = qr.matrixQR().template triangularView<Upper>();
|
||||
Matrix<Scalar, Rows, Cols> c = qr.householderQ() * r * qr.colsPermutation().inverse();
|
||||
VERIFY_IS_APPROX(m1, c);
|
||||
|
||||
check_solverbase<Matrix<Scalar,Cols,Cols2>, Matrix<Scalar,Rows,Cols2> >(m1, qr, Rows, Cols, Cols2);
|
||||
check_solverbase<Matrix<Scalar, Cols, Cols2>, Matrix<Scalar, Rows, Cols2> >(m1, qr, Rows, Cols, Cols2);
|
||||
|
||||
// Verify that the absolute value of the diagonal elements in R are
|
||||
// non-increasing until they reache the singularity threshold.
|
||||
RealScalar threshold =
|
||||
sqrt(RealScalar(Rows)) * (std::abs)(r(0, 0)) * NumTraits<Scalar>::epsilon();
|
||||
RealScalar threshold = sqrt(RealScalar(Rows)) * (std::abs)(r(0, 0)) * NumTraits<Scalar>::epsilon();
|
||||
for (Index i = 0; i < (std::min)(int(Rows), int(Cols)) - 1; ++i) {
|
||||
RealScalar x = numext::abs(r(i, i));
|
||||
RealScalar y = numext::abs(r(i + 1, i + 1));
|
||||
@@ -190,8 +181,7 @@ template<typename MatrixType, int Cols2> void qr_fixedsize()
|
||||
for (Index j = 0; j < (std::min)(int(Rows), int(Cols)); ++j) {
|
||||
std::cout << "i = " << j << ", |r_ii| = " << numext::abs(r(j, j)) << std::endl;
|
||||
}
|
||||
std::cout << "Failure at i=" << i << ", rank=" << rank
|
||||
<< ", threshold=" << threshold << std::endl;
|
||||
std::cout << "Failure at i=" << i << ", rank=" << rank << ", threshold=" << threshold << std::endl;
|
||||
}
|
||||
VERIFY_IS_APPROX_OR_LESS_THAN(y, x);
|
||||
}
|
||||
@@ -204,20 +194,20 @@ template<typename MatrixType, int Cols2> void qr_fixedsize()
|
||||
// for rank-revealing QR. See
|
||||
// http://www.netlib.org/lapack/lawnspdf/lawn176.pdf
|
||||
// page 3 for more detail.
|
||||
template<typename MatrixType> void qr_kahan_matrix()
|
||||
{
|
||||
using std::sqrt;
|
||||
template <typename MatrixType>
|
||||
void qr_kahan_matrix() {
|
||||
using std::abs;
|
||||
using std::sqrt;
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef typename MatrixType::RealScalar RealScalar;
|
||||
|
||||
Index rows = 300, cols = rows;
|
||||
|
||||
MatrixType m1;
|
||||
m1.setZero(rows,cols);
|
||||
m1.setZero(rows, cols);
|
||||
RealScalar s = std::pow(NumTraits<RealScalar>::epsilon(), 1.0 / rows);
|
||||
RealScalar c = std::sqrt(1 - s*s);
|
||||
RealScalar pow_s_i(1.0); // pow(s,i)
|
||||
RealScalar c = std::sqrt(1 - s * s);
|
||||
RealScalar pow_s_i(1.0); // pow(s,i)
|
||||
for (Index i = 0; i < rows; ++i) {
|
||||
m1(i, i) = pow_s_i;
|
||||
m1.row(i).tail(rows - i - 1) = -pow_s_i * c * MatrixType::Ones(1, rows - i - 1);
|
||||
@@ -227,8 +217,7 @@ template<typename MatrixType> void qr_kahan_matrix()
|
||||
ColPivHouseholderQR<MatrixType> qr(m1);
|
||||
MatrixType r = qr.matrixQR().template triangularView<Upper>();
|
||||
|
||||
RealScalar threshold =
|
||||
std::sqrt(RealScalar(rows)) * numext::abs(r(0, 0)) * NumTraits<Scalar>::epsilon();
|
||||
RealScalar threshold = std::sqrt(RealScalar(rows)) * numext::abs(r(0, 0)) * NumTraits<Scalar>::epsilon();
|
||||
for (Index i = 0; i < (std::min)(rows, cols) - 1; ++i) {
|
||||
RealScalar x = numext::abs(r(i, i));
|
||||
RealScalar y = numext::abs(r(i + 1, i + 1));
|
||||
@@ -237,29 +226,27 @@ template<typename MatrixType> void qr_kahan_matrix()
|
||||
for (Index j = 0; j < (std::min)(rows, cols); ++j) {
|
||||
std::cout << "i = " << j << ", |r_ii| = " << numext::abs(r(j, j)) << std::endl;
|
||||
}
|
||||
std::cout << "Failure at i=" << i << ", rank=" << qr.rank()
|
||||
<< ", threshold=" << threshold << std::endl;
|
||||
std::cout << "Failure at i=" << i << ", rank=" << qr.rank() << ", threshold=" << threshold << std::endl;
|
||||
}
|
||||
VERIFY_IS_APPROX_OR_LESS_THAN(y, x);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename MatrixType> void qr_invertible()
|
||||
{
|
||||
using std::log;
|
||||
template <typename MatrixType>
|
||||
void qr_invertible() {
|
||||
using std::abs;
|
||||
using std::log;
|
||||
typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
|
||||
int size = internal::random<int>(10,50);
|
||||
int size = internal::random<int>(10, 50);
|
||||
|
||||
MatrixType m1(size, size), m2(size, size), m3(size, size);
|
||||
m1 = MatrixType::Random(size,size);
|
||||
m1 = MatrixType::Random(size, size);
|
||||
|
||||
if (internal::is_same<RealScalar,float>::value)
|
||||
{
|
||||
if (internal::is_same<RealScalar, float>::value) {
|
||||
// let's build a matrix more stable to inverse
|
||||
MatrixType a = MatrixType::Random(size,size*2);
|
||||
MatrixType a = MatrixType::Random(size, size * 2);
|
||||
m1 += a * a.adjoint();
|
||||
}
|
||||
|
||||
@@ -269,10 +256,10 @@ template<typename MatrixType> void qr_invertible()
|
||||
|
||||
// now construct a matrix with prescribed determinant
|
||||
m1.setZero();
|
||||
for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>();
|
||||
for (int i = 0; i < size; i++) m1(i, i) = internal::random<Scalar>();
|
||||
Scalar det = m1.diagonal().prod();
|
||||
RealScalar absdet = abs(det);
|
||||
m3 = qr.householderQ(); // get a unitary
|
||||
m3 = qr.householderQ(); // get a unitary
|
||||
m1 = m3 * m1 * m3.adjoint();
|
||||
qr.compute(m1);
|
||||
VERIFY_IS_APPROX(det, qr.determinant());
|
||||
@@ -280,8 +267,8 @@ template<typename MatrixType> void qr_invertible()
|
||||
VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
|
||||
}
|
||||
|
||||
template<typename MatrixType> void qr_verify_assert()
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void qr_verify_assert() {
|
||||
MatrixType tmp;
|
||||
|
||||
ColPivHouseholderQR<MatrixType> qr;
|
||||
@@ -300,8 +287,8 @@ template<typename MatrixType> void qr_verify_assert()
|
||||
VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
|
||||
}
|
||||
|
||||
template<typename MatrixType> void cod_verify_assert()
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void cod_verify_assert() {
|
||||
MatrixType tmp;
|
||||
|
||||
CompleteOrthogonalDecomposition<MatrixType> cod;
|
||||
@@ -320,31 +307,30 @@ template<typename MatrixType> void cod_verify_assert()
|
||||
VERIFY_RAISES_ASSERT(cod.logAbsDeterminant())
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(qr_colpivoting)
|
||||
{
|
||||
for(int i = 0; i < g_repeat; i++) {
|
||||
CALL_SUBTEST_1( qr<MatrixXf>() );
|
||||
CALL_SUBTEST_2( qr<MatrixXd>() );
|
||||
CALL_SUBTEST_3( qr<MatrixXcd>() );
|
||||
CALL_SUBTEST_4(( qr_fixedsize<Matrix<float,3,5>, 4 >() ));
|
||||
CALL_SUBTEST_5(( qr_fixedsize<Matrix<double,6,2>, 3 >() ));
|
||||
CALL_SUBTEST_5(( qr_fixedsize<Matrix<double,1,1>, 1 >() ));
|
||||
EIGEN_DECLARE_TEST(qr_colpivoting) {
|
||||
for (int i = 0; i < g_repeat; i++) {
|
||||
CALL_SUBTEST_1(qr<MatrixXf>());
|
||||
CALL_SUBTEST_2(qr<MatrixXd>());
|
||||
CALL_SUBTEST_3(qr<MatrixXcd>());
|
||||
CALL_SUBTEST_4((qr_fixedsize<Matrix<float, 3, 5>, 4>()));
|
||||
CALL_SUBTEST_5((qr_fixedsize<Matrix<double, 6, 2>, 3>()));
|
||||
CALL_SUBTEST_5((qr_fixedsize<Matrix<double, 1, 1>, 1>()));
|
||||
}
|
||||
|
||||
for(int i = 0; i < g_repeat; i++) {
|
||||
CALL_SUBTEST_1( cod<MatrixXf>() );
|
||||
CALL_SUBTEST_2( cod<MatrixXd>() );
|
||||
CALL_SUBTEST_3( cod<MatrixXcd>() );
|
||||
CALL_SUBTEST_4(( cod_fixedsize<Matrix<float,3,5>, 4 >() ));
|
||||
CALL_SUBTEST_5(( cod_fixedsize<Matrix<double,6,2>, 3 >() ));
|
||||
CALL_SUBTEST_5(( cod_fixedsize<Matrix<double,1,1>, 1 >() ));
|
||||
for (int i = 0; i < g_repeat; i++) {
|
||||
CALL_SUBTEST_1(cod<MatrixXf>());
|
||||
CALL_SUBTEST_2(cod<MatrixXd>());
|
||||
CALL_SUBTEST_3(cod<MatrixXcd>());
|
||||
CALL_SUBTEST_4((cod_fixedsize<Matrix<float, 3, 5>, 4>()));
|
||||
CALL_SUBTEST_5((cod_fixedsize<Matrix<double, 6, 2>, 3>()));
|
||||
CALL_SUBTEST_5((cod_fixedsize<Matrix<double, 1, 1>, 1>()));
|
||||
}
|
||||
|
||||
for(int i = 0; i < g_repeat; i++) {
|
||||
CALL_SUBTEST_1( qr_invertible<MatrixXf>() );
|
||||
CALL_SUBTEST_2( qr_invertible<MatrixXd>() );
|
||||
CALL_SUBTEST_6( qr_invertible<MatrixXcf>() );
|
||||
CALL_SUBTEST_3( qr_invertible<MatrixXcd>() );
|
||||
for (int i = 0; i < g_repeat; i++) {
|
||||
CALL_SUBTEST_1(qr_invertible<MatrixXf>());
|
||||
CALL_SUBTEST_2(qr_invertible<MatrixXd>());
|
||||
CALL_SUBTEST_6(qr_invertible<MatrixXcf>());
|
||||
CALL_SUBTEST_3(qr_invertible<MatrixXcd>());
|
||||
}
|
||||
|
||||
CALL_SUBTEST_7(qr_verify_assert<Matrix3f>());
|
||||
@@ -364,6 +350,6 @@ EIGEN_DECLARE_TEST(qr_colpivoting)
|
||||
// Test problem size constructors
|
||||
CALL_SUBTEST_9(ColPivHouseholderQR<MatrixXf>(10, 20));
|
||||
|
||||
CALL_SUBTEST_1( qr_kahan_matrix<MatrixXf>() );
|
||||
CALL_SUBTEST_2( qr_kahan_matrix<MatrixXd>() );
|
||||
CALL_SUBTEST_1(qr_kahan_matrix<MatrixXf>());
|
||||
CALL_SUBTEST_2(qr_kahan_matrix<MatrixXd>());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user