2010-07-15 22:29:04 +08:00
|
|
|
#include "main.h"
|
|
|
|
#include <Eigen/MPRealSupport>
|
2011-01-27 20:51:39 +08:00
|
|
|
#include <Eigen/LU>
|
|
|
|
#include <Eigen/Eigenvalues>
|
2012-06-21 16:02:32 +08:00
|
|
|
#include <sstream>
|
2010-07-15 22:29:04 +08:00
|
|
|
|
|
|
|
using namespace mpfr;
|
|
|
|
using namespace Eigen;
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(mpreal_support)
|
2010-07-15 22:29:04 +08:00
|
|
|
{
|
|
|
|
// set precision to 256 bits (double has only 53 bits)
|
|
|
|
mpreal::set_default_prec(256);
|
|
|
|
typedef Matrix<mpreal,Eigen::Dynamic,Eigen::Dynamic> MatrixXmp;
|
2016-11-05 18:28:53 +08:00
|
|
|
typedef Matrix<std::complex<mpreal>,Eigen::Dynamic,Eigen::Dynamic> MatrixXcmp;
|
2010-07-15 22:29:04 +08:00
|
|
|
|
|
|
|
std::cerr << "epsilon = " << NumTraits<mpreal>::epsilon() << "\n";
|
|
|
|
std::cerr << "dummy_precision = " << NumTraits<mpreal>::dummy_precision() << "\n";
|
|
|
|
std::cerr << "highest = " << NumTraits<mpreal>::highest() << "\n";
|
|
|
|
std::cerr << "lowest = " << NumTraits<mpreal>::lowest() << "\n";
|
2016-07-25 20:38:55 +08:00
|
|
|
std::cerr << "digits10 = " << NumTraits<mpreal>::digits10() << "\n";
|
2010-07-15 22:29:04 +08:00
|
|
|
|
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
2010-10-25 22:15:22 +08:00
|
|
|
int s = Eigen::internal::random<int>(1,100);
|
2010-07-15 22:29:04 +08:00
|
|
|
MatrixXmp A = MatrixXmp::Random(s,s);
|
|
|
|
MatrixXmp B = MatrixXmp::Random(s,s);
|
|
|
|
MatrixXmp S = A.adjoint() * A;
|
|
|
|
MatrixXmp X;
|
2016-11-05 18:28:53 +08:00
|
|
|
MatrixXcmp Ac = MatrixXcmp::Random(s,s);
|
|
|
|
MatrixXcmp Bc = MatrixXcmp::Random(s,s);
|
|
|
|
MatrixXcmp Sc = Ac.adjoint() * Ac;
|
|
|
|
MatrixXcmp Xc;
|
2012-06-21 16:02:32 +08:00
|
|
|
|
|
|
|
// Basic stuffs
|
|
|
|
VERIFY_IS_APPROX(A.real(), A);
|
|
|
|
VERIFY(Eigen::internal::isApprox(A.array().abs2().sum(), A.squaredNorm()));
|
|
|
|
VERIFY_IS_APPROX(A.array().exp(), exp(A.array()));
|
|
|
|
VERIFY_IS_APPROX(A.array().abs2().sqrt(), A.array().abs());
|
|
|
|
VERIFY_IS_APPROX(A.array().sin(), sin(A.array()));
|
|
|
|
VERIFY_IS_APPROX(A.array().cos(), cos(A.array()));
|
2010-07-15 22:29:04 +08:00
|
|
|
|
|
|
|
// Cholesky
|
|
|
|
X = S.selfadjointView<Lower>().llt().solve(B);
|
|
|
|
VERIFY_IS_APPROX((S.selfadjointView<Lower>()*X).eval(),B);
|
2016-11-05 18:28:53 +08:00
|
|
|
|
|
|
|
Xc = Sc.selfadjointView<Lower>().llt().solve(Bc);
|
|
|
|
VERIFY_IS_APPROX((Sc.selfadjointView<Lower>()*Xc).eval(),Bc);
|
2014-07-17 15:41:33 +08:00
|
|
|
|
2010-07-15 22:29:04 +08:00
|
|
|
// partial LU
|
|
|
|
X = A.lu().solve(B);
|
|
|
|
VERIFY_IS_APPROX((A*X).eval(),B);
|
|
|
|
|
|
|
|
// symmetric eigenvalues
|
|
|
|
SelfAdjointEigenSolver<MatrixXmp> eig(S);
|
|
|
|
VERIFY_IS_EQUAL(eig.info(), Success);
|
2013-01-24 06:57:28 +08:00
|
|
|
VERIFY( (S.selfadjointView<Lower>() * eig.eigenvectors()).isApprox(eig.eigenvectors() * eig.eigenvalues().asDiagonal(), NumTraits<mpreal>::dummy_precision()*1e3) );
|
2010-07-15 22:29:04 +08:00
|
|
|
}
|
2012-06-21 16:02:32 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
MatrixXmp A(8,3); A.setRandom();
|
|
|
|
// test output (interesting things happen in this code)
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << A;
|
|
|
|
}
|
2010-07-15 22:29:04 +08:00
|
|
|
}
|