// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// The following includes of STL headers have to be done _before_ the // definition of macros min() and max(). The reason is that many STL // implementations will not work properly as the min and max symbols collide // with the STL functions std:min() and std::max(). The STL headers may check // for the macro definition of min/max and issue a warning or undefine the // macros. // // Still, Windows defines min() and max() in windef.h as part of the regular // Windows system interfaces and many other Windows APIs depend on these // macros being available. To prevent the macro expansion of min/max and to // make Eigen compatible with the Windows environment all function calls of // std::min() and std::max() have to be written with parenthesis around the // function name. // // All STL headers used by Eigen should be included here. Because main.h is // included before any Eigen header and because the STL headers are guarded // against multiple inclusions, no STL header will see our own min/max macro // definitions. #include <limits> #include <algorithm> // Disable ICC's std::complex operator specializations so we can use our own. #define _OVERRIDE_COMPLEX_SPECIALIZATION_ 1 #include <complex> #include <deque> #include <queue> #include <cassert> #include <list> #if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L) #include <random> #include <chrono> #ifdef EIGEN_USE_THREADS #include <future> #endif #endif
// Same for cuda_fp16.h #ifdefined(__CUDACC__) && !defined(EIGEN_NO_CUDA) // Means the compiler is either nvcc or clang with CUDA enabled #define EIGEN_CUDACC __CUDACC__ #endif #ifdefined(EIGEN_CUDACC) #include <cuda.h> #define EIGEN_CUDA_SDK_VER (CUDA_VERSION * 10) #else #define EIGEN_CUDA_SDK_VER 0 #endif #if EIGEN_CUDA_SDK_VER >= 70500 #include <cuda_fp16.h> #endif
// To test that all calls from Eigen code to std::min() and std::max() are // protected by parenthesis against macro expansion, the min()/max() macros // are defined here and any not-parenthesized min/max call will cause a // compiler error. #if !defined(__HIPCC__) && !defined(EIGEN_USE_SYCL) // // HIP header files include the following files // <thread> // <regex> // <unordered_map> // which seem to contain not-parenthesized calls to "max"/"min", triggering the following check and causing the compile to fail // // Including those header files before the following macro definition for "min" / "max", only partially resolves the issue // This is because other HIP header files also define "isnan" / "isinf" / "isfinite" functions, which are needed in other // headers. // // So instead choosing to simply disable this check for HIP // #define min(A,B) please_protect_your_min_with_parentheses #define max(A,B) please_protect_your_max_with_parentheses #define isnan(X) please_protect_your_isnan_with_parentheses #define isinf(X) please_protect_your_isinf_with_parentheses #define isfinite(X) please_protect_your_isfinite_with_parentheses #endif
// test possible conflicts struct real {}; struct imag {};
#define FORBIDDEN_IDENTIFIER (this_identifier_is_forbidden_to_avoid_clashes) this_identifier_is_forbidden_to_avoid_clashes // B0 is defined in POSIX header termios.h #define B0 FORBIDDEN_IDENTIFIER // `I` may be defined by complex.h: #define I FORBIDDEN_IDENTIFIER
// Unit tests calling Eigen's blas library must preserve the default blocking size // to avoid troubles. #ifndef EIGEN_NO_DEBUG_SMALL_PRODUCT_BLOCKS #define EIGEN_DEBUG_SMALL_PRODUCT_BLOCKS #endif
// shuts down ICC's remark #593: variable "XXX" was set but never used #define TEST_SET_BUT_UNUSED_VARIABLE(X) EIGEN_UNUSED_VARIABLE(X)
inlinevoid on_temporary_creation(longint size) { // here's a great place to set a breakpoint when debugging failures in this test! if(size!=0) nb_temporaries++; if(nb_temporaries_on_assert>0) assert(nb_temporaries<nb_temporaries_on_assert);
}
namespace Eigen
{ staticconstbool should_raise_an_assert = false;
// Used to avoid to raise two exceptions at a time in which // case the exception is not properly caught. // This may happen when a second exceptions is triggered in a destructor. staticbool no_more_assert = false; staticbool report_on_cerr_on_assert_failure = true;
struct eigen_static_assert_exception
{
eigen_static_assert_exception(void) {}
~eigen_static_assert_exception() { Eigen::no_more_assert = false; }
};
} // If EIGEN_DEBUG_ASSERTS is defined and if no assertion is triggered while // one should have been, then the list of executed assertions is printed out. // // EIGEN_DEBUG_ASSERTS is not enabled by default as it // significantly increases the compilation time // and might even introduce side effects that would hide // some memory errors. #ifdef EIGEN_DEBUG_ASSERTS
#define EIGEN_TEST_SCALAR_TEST_OVERLOAD(TYPE) \ inlinebool test_isApprox(TYPE a, TYPE b) \
{ return internal::isApprox(a, b, test_precision<TYPE>()); } \ inlinebool test_isMuchSmallerThan(TYPE a, TYPE b) \
{ return internal::isMuchSmallerThan(a, b, test_precision<TYPE>()); } \ inlinebool test_isApproxOrLessThan(TYPE a, TYPE b) \
{ return internal::isApproxOrLessThan(a, b, test_precision<TYPE>()); }
#ifndef EIGEN_TEST_NO_COMPLEX inlinebool test_isApprox(const std::complex<float>& a, const std::complex<float>& b)
{ return internal::isApprox(a, b, test_precision<std::complex<float> >()); } inlinebool test_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b)
{ return internal::isMuchSmallerThan(a, b, test_precision<std::complex<float> >()); }
inlinebool test_isApprox(const std::complex<double>& a, const std::complex<double>& b)
{ return internal::isApprox(a, b, test_precision<std::complex<double> >()); } inlinebool test_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b)
{ return internal::isMuchSmallerThan(a, b, test_precision<std::complex<double> >()); }
#ifndef EIGEN_TEST_NO_LONGDOUBLE inlinebool test_isApprox(const std::complex<longdouble>& a, const std::complex<longdouble>& b)
{ return internal::isApprox(a, b, test_precision<std::complex<longdouble> >()); } inlinebool test_isMuchSmallerThan(const std::complex<longdouble>& a, const std::complex<longdouble>& b)
{ return internal::isMuchSmallerThan(a, b, test_precision<std::complex<longdouble> >()); } #endif #endif
#ifndef EIGEN_TEST_NO_LONGDOUBLE inlinebool test_isApprox(constlongdouble& a, constlongdouble& b)
{ bool ret = internal::isApprox(a, b, test_precision<longdouble>()); if (!ret) std::cerr
<< std::endl << " actual = " << a
<< std::endl << " expected = " << b << std::endl << std::endl; return ret;
}
inlinebool test_isMuchSmallerThan(constlongdouble& a, constlongdouble& b)
{ return internal::isMuchSmallerThan(a, b, test_precision<longdouble>()); } inlinebool test_isApproxOrLessThan(constlongdouble& a, constlongdouble& b)
{ return internal::isApproxOrLessThan(a, b, test_precision<longdouble>()); } #endif// EIGEN_TEST_NO_LONGDOUBLE
// test_relative_error returns the relative difference between a and b as a real scalar as used in isApprox. template<typename T1,typename T2> typename NumTraits<typename T1::RealScalar>::NonInteger test_relative_error(const EigenBase<T1> &a, const EigenBase<T2> &b)
{ using std::sqrt; typedeftypename NumTraits<typename T1::RealScalar>::NonInteger RealScalar; typename internal::nested_eval<T1,2>::type ea(a.derived()); typename internal::nested_eval<T2,2>::type eb(b.derived()); return sqrt(RealScalar((ea-eb).cwiseAbs2().sum()) / RealScalar((std::min)(eb.cwiseAbs2().sum(),ea.cwiseAbs2().sum())));
}
template<typename Type1, typename Type2> inlinebool test_isApprox(const Type1& a, const Type2& b, typename Type1::Scalar* = 0) // Enabled for Eigen's type only
{ return a.isApprox(b, test_precision<typename Type1::Scalar>());
}
// get_test_precision is a small wrapper to test_precision allowing to return the scalar precision for either scalars or expressions template<typename T> typename NumTraits<typename T::Scalar>::Real get_test_precision(const T&, consttypename T::Scalar* = 0)
{ return test_precision<typename NumTraits<typename T::Scalar>::Real>();
}
// verifyIsApprox is a wrapper to test_isApprox that outputs the relative difference magnitude if the test fails. template<typename Type1, typename Type2> inlinebool verifyIsApprox(const Type1& a, const Type2& b)
{ bool ret = test_isApprox(a,b); if(!ret)
{
std::cerr << "Difference too large wrt tolerance " << get_test_precision(a) << ", relative error is: " << test_relative_error(a,b) << std::endl;
} return ret;
}
// The idea behind this function is to compare the two scalars a and b where // the scalar ref is a hint about the expected order of magnitude of a and b. // WARNING: the scalar a and b must be positive // Therefore, if for some reason a and b are very small compared to ref, // we won't issue a false negative. // This test could be: abs(a-b) <= eps * ref // However, it seems that simply comparing a+ref and b+ref is more sensitive to true error. template<typename Scalar,typename ScalarRef> inlinebool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref)
{ return test_isApprox(a+ref, b+ref);
}
/** Creates a random Partial Isometry matrix of given rank. * * A partial isometry is a matrix all of whose singular values are either 0 or 1. * This is very useful to test rank-revealing algorithms.
*/ // Forward declaration to avoid ICC warning template<typename MatrixType> void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m); template<typename MatrixType> void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m)
{ typedeftypename internal::traits<MatrixType>::Scalar Scalar; enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
if(desired_rank == 1)
{ // here we normalize the vectors to get a partial isometry
m = VectorType::Random(rows).normalized() * VectorType::Random(cols).normalized().transpose(); return;
}
MatrixAType a = MatrixAType::Random(rows,rows);
MatrixType d = MatrixType::Identity(rows,cols);
MatrixBType b = MatrixBType::Random(cols,cols);
// set the diagonal such that only desired_rank non-zero entries reamain const Index diag_size = (std::min)(d.rows(),d.cols()); if(diag_size != desired_rank)
d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank);
HouseholderQR<MatrixAType> qra(a);
HouseholderQR<MatrixBType> qrb(b);
m = qra.householderQ() * d * qrb.householderQ();
}
// Forward declaration to avoid ICC warning template<typename PermutationVectorType> void randomPermutationVector(PermutationVectorType& v, Index size); template<typename PermutationVectorType> void randomPermutationVector(PermutationVectorType& v, Index size)
{ typedeftypename PermutationVectorType::Scalar Scalar;
v.resize(size); for(Index i = 0; i < size; ++i) v(i) = Scalar(i); if(size == 1) return; for(Index n = 0; n < 3 * size; ++n)
{
Index i = internal::random<Index>(0, size-1);
Index j; do j = internal::random<Index>(0, size-1); while(j==i);
std::swap(v(i), v(j));
}
}
for(int i = 1; i < argc; i++)
{ if(argv[i][0] == 'r')
{ if(g_has_set_repeat)
{
std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl; return 1;
}
set_repeat_from_string(argv[i]+1);
} elseif(argv[i][0] == 's')
{ if(g_has_set_seed)
{
std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl; return 1;
}
set_seed_from_string(argv[i]+1);
} else
{
need_help = true;
}
}
if(need_help)
{
std::cout << "This test application takes the following optional arguments:" << std::endl;
std::cout << " rN Repeat each test N times (default: " << DEFAULT_REPEAT << ")" << std::endl;
std::cout << " sN Use N as seed for random numbers (default: based on current time)" << std::endl;
std::cout << std::endl;
std::cout << "If defined, the environment variables EIGEN_REPEAT and EIGEN_SEED" << std::endl;
std::cout << "will be used as default values for these parameters." << std::endl; return 1;
}
// These warning are disabled here such that they are still ON when parsing Eigen's header files. #ifdefined __INTEL_COMPILER // remark #383: value copied to temporary, reference to temporary used // -> this warning is raised even for legal usage as: g_test_stack.push_back("foo"); where g_test_stack is a std::vector<std::string> // remark #1418: external function definition with no prior declaration // -> this warning is raised for all our test functions. Declaring them static would fix the issue. // warning #279: controlling expression is constant // remark #1572: floating-point equality and inequality comparisons are unreliable #pragma warning disable 279 383 1418 1572 #endif
#ifdef _MSC_VER // 4503 - decorated name length exceeded, name was truncated #pragma warning( disable : 4503) #endif
¤ Dauer der Verarbeitung: 0.4 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.