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
@@ -14,23 +14,20 @@
|
||||
// relative error.
|
||||
#define VERIFY_IS_APPROX_ABS(a, b) VERIFY(test_isApprox_abs(a, b))
|
||||
|
||||
template<typename Type1, typename Type2>
|
||||
inline bool test_isApprox_abs(const Type1& a, const Type2& b)
|
||||
{
|
||||
return ((a-b).array().abs() < test_precision<typename Type1::RealScalar>()).all();
|
||||
template <typename Type1, typename Type2>
|
||||
inline bool test_isApprox_abs(const Type1& a, const Type2& b) {
|
||||
return ((a - b).array().abs() < test_precision<typename Type1::RealScalar>()).all();
|
||||
}
|
||||
|
||||
|
||||
// Returns a matrix with eigenvalues clustered around 0, 1 and 2.
|
||||
template<typename MatrixType>
|
||||
MatrixType randomMatrixWithRealEivals(const Index size)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
MatrixType randomMatrixWithRealEivals(const Index size) {
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef typename MatrixType::RealScalar RealScalar;
|
||||
MatrixType diag = MatrixType::Zero(size, size);
|
||||
for (Index i = 0; i < size; ++i) {
|
||||
diag(i, i) = Scalar(RealScalar(internal::random<int>(0,2)))
|
||||
+ internal::random<Scalar>() * Scalar(RealScalar(0.01));
|
||||
diag(i, i) =
|
||||
Scalar(RealScalar(internal::random<int>(0, 2))) + internal::random<Scalar>() * Scalar(RealScalar(0.01));
|
||||
}
|
||||
MatrixType A = MatrixType::Random(size, size);
|
||||
HouseholderQR<MatrixType> QRofA(A);
|
||||
@@ -38,30 +35,27 @@ MatrixType randomMatrixWithRealEivals(const Index size)
|
||||
}
|
||||
|
||||
template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
|
||||
struct randomMatrixWithImagEivals
|
||||
{
|
||||
struct randomMatrixWithImagEivals {
|
||||
// Returns a matrix with eigenvalues clustered around 0 and +/- i.
|
||||
static MatrixType run(const Index size);
|
||||
};
|
||||
|
||||
// Partial specialization for real matrices
|
||||
template<typename MatrixType>
|
||||
struct randomMatrixWithImagEivals<MatrixType, 0>
|
||||
{
|
||||
static MatrixType run(const Index size)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
struct randomMatrixWithImagEivals<MatrixType, 0> {
|
||||
static MatrixType run(const Index size) {
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
MatrixType diag = MatrixType::Zero(size, size);
|
||||
Index i = 0;
|
||||
while (i < size) {
|
||||
Index randomInt = internal::random<Index>(-1, 1);
|
||||
if (randomInt == 0 || i == size-1) {
|
||||
if (randomInt == 0 || i == size - 1) {
|
||||
diag(i, i) = internal::random<Scalar>() * Scalar(0.01);
|
||||
++i;
|
||||
} else {
|
||||
Scalar alpha = Scalar(randomInt) + internal::random<Scalar>() * Scalar(0.01);
|
||||
diag(i, i+1) = alpha;
|
||||
diag(i+1, i) = -alpha;
|
||||
diag(i, i + 1) = alpha;
|
||||
diag(i + 1, i) = -alpha;
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
@@ -72,18 +66,16 @@ struct randomMatrixWithImagEivals<MatrixType, 0>
|
||||
};
|
||||
|
||||
// Partial specialization for complex matrices
|
||||
template<typename MatrixType>
|
||||
struct randomMatrixWithImagEivals<MatrixType, 1>
|
||||
{
|
||||
static MatrixType run(const Index size)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
struct randomMatrixWithImagEivals<MatrixType, 1> {
|
||||
static MatrixType run(const Index size) {
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef typename MatrixType::RealScalar RealScalar;
|
||||
const Scalar imagUnit(0, 1);
|
||||
MatrixType diag = MatrixType::Zero(size, size);
|
||||
for (Index i = 0; i < size; ++i) {
|
||||
diag(i, i) = Scalar(RealScalar(internal::random<Index>(-1, 1))) * imagUnit
|
||||
+ internal::random<Scalar>() * Scalar(RealScalar(0.01));
|
||||
diag(i, i) = Scalar(RealScalar(internal::random<Index>(-1, 1))) * imagUnit +
|
||||
internal::random<Scalar>() * Scalar(RealScalar(0.01));
|
||||
}
|
||||
MatrixType A = MatrixType::Random(size, size);
|
||||
HouseholderQR<MatrixType> QRofA(A);
|
||||
@@ -91,10 +83,8 @@ struct randomMatrixWithImagEivals<MatrixType, 1>
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename MatrixType>
|
||||
void testMatrixExponential(const MatrixType& A)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void testMatrixExponential(const MatrixType& A) {
|
||||
typedef typename internal::traits<MatrixType>::Scalar Scalar;
|
||||
typedef typename NumTraits<Scalar>::Real RealScalar;
|
||||
typedef std::complex<RealScalar> ComplexScalar;
|
||||
@@ -102,9 +92,8 @@ void testMatrixExponential(const MatrixType& A)
|
||||
VERIFY_IS_APPROX(A.exp(), A.matrixFunction(internal::stem_function_exp<ComplexScalar>));
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void testMatrixLogarithm(const MatrixType& A)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void testMatrixLogarithm(const MatrixType& A) {
|
||||
typedef typename internal::traits<MatrixType>::Scalar Scalar;
|
||||
typedef typename NumTraits<Scalar>::Real RealScalar;
|
||||
|
||||
@@ -121,51 +110,47 @@ void testMatrixLogarithm(const MatrixType& A)
|
||||
VERIFY_IS_APPROX(logExpA, scaledA);
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void testHyperbolicFunctions(const MatrixType& A)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void testHyperbolicFunctions(const MatrixType& A) {
|
||||
// Need to use absolute error because of possible cancellation when
|
||||
// adding/subtracting expA and expmA.
|
||||
VERIFY_IS_APPROX_ABS(A.sinh(), (A.exp() - (-A).exp()) / 2);
|
||||
VERIFY_IS_APPROX_ABS(A.cosh(), (A.exp() + (-A).exp()) / 2);
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void testGonioFunctions(const MatrixType& A)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void testGonioFunctions(const MatrixType& A) {
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef typename NumTraits<Scalar>::Real RealScalar;
|
||||
typedef std::complex<RealScalar> ComplexScalar;
|
||||
typedef Matrix<ComplexScalar, MatrixType::RowsAtCompileTime,
|
||||
MatrixType::ColsAtCompileTime, MatrixType::Options> ComplexMatrix;
|
||||
typedef Matrix<ComplexScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime, MatrixType::Options>
|
||||
ComplexMatrix;
|
||||
|
||||
ComplexScalar imagUnit(0,1);
|
||||
ComplexScalar two(2,0);
|
||||
ComplexScalar imagUnit(0, 1);
|
||||
ComplexScalar two(2, 0);
|
||||
|
||||
ComplexMatrix Ac = A.template cast<ComplexScalar>();
|
||||
|
||||
|
||||
ComplexMatrix exp_iA = (imagUnit * Ac).exp();
|
||||
ComplexMatrix exp_miA = (-imagUnit * Ac).exp();
|
||||
|
||||
|
||||
ComplexMatrix sinAc = A.sin().template cast<ComplexScalar>();
|
||||
VERIFY_IS_APPROX_ABS(sinAc, (exp_iA - exp_miA) / (two*imagUnit));
|
||||
|
||||
VERIFY_IS_APPROX_ABS(sinAc, (exp_iA - exp_miA) / (two * imagUnit));
|
||||
|
||||
ComplexMatrix cosAc = A.cos().template cast<ComplexScalar>();
|
||||
VERIFY_IS_APPROX_ABS(cosAc, (exp_iA + exp_miA) / 2);
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void testMatrix(const MatrixType& A)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void testMatrix(const MatrixType& A) {
|
||||
testMatrixExponential(A);
|
||||
testMatrixLogarithm(A);
|
||||
testHyperbolicFunctions(A);
|
||||
testGonioFunctions(A);
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void testMatrixType(const MatrixType& m)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void testMatrixType(const MatrixType& m) {
|
||||
// Matrices with clustered eigenvalue lead to different code paths
|
||||
// in MatrixFunction.h and are thus useful for testing.
|
||||
|
||||
@@ -177,27 +162,27 @@ void testMatrixType(const MatrixType& m)
|
||||
}
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void testMapRef(const MatrixType& A)
|
||||
{
|
||||
template <typename MatrixType>
|
||||
void testMapRef(const MatrixType& A) {
|
||||
// Test if passing Ref and Map objects is possible
|
||||
// (Regression test for Bug #1796)
|
||||
Index size = A.rows();
|
||||
MatrixType X; X.setRandom(size, size);
|
||||
MatrixType Y(size,size);
|
||||
Ref< MatrixType> R(Y);
|
||||
MatrixType X;
|
||||
X.setRandom(size, size);
|
||||
MatrixType Y(size, size);
|
||||
Ref<MatrixType> R(Y);
|
||||
Ref<const MatrixType> Rc(X);
|
||||
Map< MatrixType> M(Y.data(), size, size);
|
||||
Map<MatrixType> M(Y.data(), size, size);
|
||||
Map<const MatrixType> Mc(X.data(), size, size);
|
||||
|
||||
X = X*X; // make sure sqrt is possible
|
||||
X = X * X; // make sure sqrt is possible
|
||||
Y = X.sqrt();
|
||||
R = Rc.sqrt();
|
||||
M = Mc.sqrt();
|
||||
Y = X.exp();
|
||||
R = Rc.exp();
|
||||
M = Mc.exp();
|
||||
X = Y; // make sure log is possible
|
||||
X = Y; // make sure log is possible
|
||||
Y = X.log();
|
||||
R = Rc.log();
|
||||
M = Mc.log();
|
||||
@@ -209,19 +194,17 @@ void testMapRef(const MatrixType& A)
|
||||
Y = X.sinh() + Rc.sinh() + Mc.sinh();
|
||||
}
|
||||
|
||||
|
||||
EIGEN_DECLARE_TEST(matrix_function)
|
||||
{
|
||||
CALL_SUBTEST_1(testMatrixType(Matrix<float,1,1>()));
|
||||
EIGEN_DECLARE_TEST(matrix_function) {
|
||||
CALL_SUBTEST_1(testMatrixType(Matrix<float, 1, 1>()));
|
||||
CALL_SUBTEST_2(testMatrixType(Matrix3cf()));
|
||||
CALL_SUBTEST_3(testMatrixType(MatrixXf(8,8)));
|
||||
CALL_SUBTEST_3(testMatrixType(MatrixXf(8, 8)));
|
||||
CALL_SUBTEST_4(testMatrixType(Matrix2d()));
|
||||
CALL_SUBTEST_5(testMatrixType(Matrix<double,5,5,RowMajor>()));
|
||||
CALL_SUBTEST_5(testMatrixType(Matrix<double, 5, 5, RowMajor>()));
|
||||
CALL_SUBTEST_6(testMatrixType(Matrix4cd()));
|
||||
CALL_SUBTEST_7(testMatrixType(MatrixXd(13,13)));
|
||||
CALL_SUBTEST_7(testMatrixType(MatrixXd(13, 13)));
|
||||
|
||||
CALL_SUBTEST_1(testMapRef(Matrix<float,1,1>()));
|
||||
CALL_SUBTEST_1(testMapRef(Matrix<float, 1, 1>()));
|
||||
CALL_SUBTEST_2(testMapRef(Matrix3cf()));
|
||||
CALL_SUBTEST_3(testMapRef(MatrixXf(8,8)));
|
||||
CALL_SUBTEST_7(testMapRef(MatrixXd(13,13)));
|
||||
CALL_SUBTEST_3(testMapRef(MatrixXf(8, 8)));
|
||||
CALL_SUBTEST_7(testMapRef(MatrixXd(13, 13)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user