29 #ifndef TESTING_UTIL_MATRIXTESTUTILITIES
30 #define TESTING_UTIL_MATRIXTESTUTILITIES 1
32 #include <gtest/gtest.h>
33 #include <boost/test/floating_point_comparison.hpp>
34 #include <boost/tuple/tuple.hpp>
35 #include <boost/timer.hpp>
36 #include <boost/assign.hpp>
37 #include <boost/filesystem.hpp>
59 #define DEFAULT_MATRIX_PERCENT_TOLERANCE 1e-5
64 using namespace boost::test_tools;
66 std::equal(m1.data(), m1.data() + m1.size(), m2.data(),
67 close_at_tolerance<double>(percent_tolerance(percentTolerance)));
72 using namespace boost::test_tools;
74 std::equal(m1.valuePtr(), m1.valuePtr() + m1.nonZeros(), m2.valuePtr(),
75 close_at_tolerance<double>(percent_tolerance(percentTolerance)));
81 ::testing::AssertionSuccess() :
82 ::testing::AssertionFailure() <<
"Matrix 1: \n"<<
matrix_to_string(m1).substr(0, printSize) <<
"\nMatrix 2: \n" <<
matrix_to_string(m2).substr(0, printSize);
88 double num = diff.lpNorm<Eigen::Infinity>();
89 double denom = xhat.lpNorm<Eigen::Infinity>();
97 ::testing::AssertionSuccess() :
98 ::testing::AssertionFailure() <<
"ColumnMatrix 1: \n"<<
matrix_to_string(x).substr(0, printSize) <<
"ColumnMatrix 2: \n" <<
matrix_to_string(xhat).substr(0, printSize);
104 double delta = (x - xhat).
norm();
105 return delta < error ?
106 ::testing::AssertionSuccess() :
107 ::testing::AssertionFailure() <<
108 "Vectors are " << delta <<
" apart, expect less than " << error <<
" distance apart.\n" <<
115 for (
int i = 0; i < from.rows(); ++i)
116 for (
int j = 0; j < from.cols(); ++j)
117 if (fabs(from(i,j)) > 1e-10)
118 to.insert(i,j) = from(i,j);
131 for (
int k=0; k < sparse.outerSize(); ++k)
133 for (Core::Datatypes::SparseRowMatrix::InnerIterator it(sparse,k); it; ++it)
135 (*dense)(it.row(), it.col()) = it.value();
143 #define EXPECT_MATRIX_EQ_TOLERANCE(actual, expected, tolerance) EXPECT_TRUE(compare_with_tolerance_readable((actual), (expected), (tolerance)))
144 #define EXPECT_MATRIX_EQ(actual, expected) EXPECT_MATRIX_EQ_TOLERANCE((actual), (expected), DEFAULT_MATRIX_PERCENT_TOLERANCE)
152 #define EXPECT_COLUMN_MATRIX_EQ_BY_TWO_NORM(actual, expected, error) EXPECT_TRUE(compare_with_two_norm((actual), (expected), (error)))
160 template <
class Tuple>
163 typedef typename boost::tuples::element<0,Tuple>::type element0type;
164 typedef boost::is_convertible<element0type, double> CanConvertToDouble;
165 BOOST_STATIC_ASSERT(CanConvertToDouble::value);
167 const int columns = boost::tuples::length<Tuple>::value;
168 const bool tupleShouldBeAllOneType = columns *
sizeof(element0type) ==
sizeof(Tuple);
169 BOOST_STATIC_ASSERT(tupleShouldBeAllOneType);
171 const size_t rows = matrixData.size();
174 for (
size_t i = 0; i < rows; ++i)
176 const element0type* tupleData =
reinterpret_cast<const element0type*
>(&matrixData[i]);
177 std::copy(tupleData, tupleData + columns, matrix.data() + i*columns);
183 template <
class Cont>
184 inline std::vector<typename Cont::value_type>
to_vector(
const Cont& cont)
187 throw std::invalid_argument(
"Matrix<double> construction will not work without any data");
189 std::vector<typename Cont::value_type>
v;
190 return cont.to_container(v);
193 #define MAKE_DENSE_MATRIX(x) (convertDataToMatrix(to_vector(boost::assign::tuple_list_of x)))
195 #define MAKE_DENSE_MATRIX_HANDLE(x) (boost::make_shared<SCIRun::Core::Datatypes::DenseMatrix>(MAKE_DENSE_MATRIX(x)))
197 #define MAKE_SPARSE_MATRIX_HANDLE(x) (toSparseHandle(MAKE_DENSE_MATRIX(x)))
203 std::cout <<
"Starting timer " << name_ << std::endl;
209 double elapsed = t_.elapsed();
210 std::cout <<
"Timer " << name_ <<
" stopped at " << elapsed <<
" seconds." << std::endl;
Definition: DenseColumnMatrix.h:43
Definition: DenseMatrix.h:44
~ScopedTimer()
Definition: MatrixTestUtilities.h:207
virtual size_t ncols() const =0
bool compare_with_tolerance(const Core::Datatypes::DenseMatrix &m1, const Core::Datatypes::DenseMatrix &m2, double percentTolerance=DEFAULT_MATRIX_PERCENT_TOLERANCE)
Definition: MatrixTestUtilities.h:62
ScopedTimer(const std::string &name)
Definition: MatrixTestUtilities.h:201
std::string to_string(const T &t)
Definition: StringUtil.h:55
std::string name_
Definition: MatrixTestUtilities.h:213
boost::timer t_
Definition: MatrixTestUtilities.h:214
#define SCISHARE
Definition: share.h:39
boost::shared_ptr< SparseRowMatrix > SparseRowMatrixHandle
Definition: MatrixFwd.h:68
std::vector< typename Cont::value_type > to_vector(const Cont &cont)
Definition: MatrixTestUtilities.h:184
Core::Datatypes::SparseRowMatrixHandle toSparseHandle(const Core::Datatypes::DenseMatrix &dense)
Definition: MatrixTestUtilities.h:121
virtual size_t nrows() const =0
SCISHARE FieldHandle loadFieldFromFile(const boost::filesystem::path &filename)
Definition: MatrixTestUtilities.cc:34
#define DEFAULT_MATRIX_PERCENT_TOLERANCE
Definition: MatrixTestUtilities.h:59
Core::Datatypes::DenseMatrixHandle makeDense(const Core::Datatypes::SparseRowMatrix &sparse)
Definition: MatrixTestUtilities.h:128
const char * name[]
Definition: BoostGraphExampleTests.cc:87
bool equal_size(const Core::Datatypes::Matrix &m1, const Core::Datatypes::Matrix &m2)
Definition: MatrixTestUtilities.h:53
void copyDenseToSparse(const Core::Datatypes::DenseMatrix &from, Core::Datatypes::SparseRowMatrix &to)
Definition: MatrixTestUtilities.h:112
inline::testing::AssertionResult compare_with_two_norm(const Core::Datatypes::DenseColumnMatrix &x, const Core::Datatypes::DenseColumnMatrix &xhat, double error=1e-15, int printSize=50)
Definition: MatrixTestUtilities.h:102
Core::Datatypes::DenseMatrix convertDataToMatrix(const std::vector< Tuple > &matrixData)
Definition: MatrixTestUtilities.h:161
v
Definition: readAllFields.py:42
double relative_infinity_norm(const Core::Datatypes::DenseColumnMatrix &x, const Core::Datatypes::DenseColumnMatrix &xhat)
Definition: MatrixTestUtilities.h:85
inline::testing::AssertionResult compare_with_tolerance_readable(const Core::Datatypes::DenseMatrix &m1, const Core::Datatypes::DenseMatrix &m2, double percentTolerance, int printSize=50)
Definition: MatrixTestUtilities.h:78
boost::shared_ptr< DenseMatrix > DenseMatrixHandle
Definition: MatrixFwd.h:52
Definition: ParallelLinearAlgebraTests.cc:751
boost::shared_ptr< Field > FieldHandle
Definition: DatatypeFwd.h:65
Definition: MatrixFwd.h:64
Definition: MatrixTestUtilities.h:199
std::string matrix_to_string(const MatrixBase &mat)
Definition: Matrix.h:206
inline::testing::AssertionResult compare_with_relative_infinity_norm(const Core::Datatypes::DenseColumnMatrix &x, const Core::Datatypes::DenseColumnMatrix &xhat, double relativeError=DEFAULT_MATRIX_PERCENT_TOLERANCE, int printSize=50)
Definition: MatrixTestUtilities.h:94