typename std::common_type<typename M1::value_type, typename M2::value_type>::type inner_product(const M1& A, const M2& B) { #ifdef UNROLL auto s1 = A(0, 0) * B(0, 0); auto s2 = A(1, 0) * B(1, 0); auto s3 = A(2, 0) * B(2, 0); auto s4 = A(3, 0) * B(3, 0); for(std::size_t j=0; j < A.cols(); ++j) for(std::size_t i=0; i < A.rows(); i+=4) { s1 += A(i,j) * B(i,j); s2 += A(i+1,j) * B(i+1,j); s3 += A(i+2,j) * B(i+2,j); s4 += A(i+3,j) * B(i+3,j); } return s1 + s2 + s3 + s4; #else auto s = A(0, 0) * B(0, 0); for(std::size_t j=0; j < A.cols(); ++j) for(std::size_t i=0; i < A.rows(); ++i) s += A(i,j) * B(i,j); return s; #endif }
void expectNear(const M1 & A, const M2 & B, T tolerance, std::string const & message = "") { EXPECT_EQ((size_t)A.rows(),(size_t)B.rows()) << message << "\nMatrix A:\n" << A << "\nand matrix B\n" << B << "\nare not the same\n"; EXPECT_EQ((size_t)A.cols(),(size_t)B.cols()) << message << "\nMatrix A:\n" << A << "\nand matrix B\n" << B << "\nare not the same\n"; for(int r = 0; r < A.rows(); r++) { for(int c = 0; c < A.cols(); c++) { EXPECT_NEAR(A(r,c),B(r,c),tolerance) << message << "\nTolerance comparison failed at (" << r << "," << c << ")\n" << "\nMatrix A:\n" << A << "\nand matrix B\n" << B; } } }
void assertEqual(const M1 & A, const M2 & B, std::string const & message = "") { ASSERT_EQ((size_t)A.rows(),(size_t)B.rows()) << message << "\nMatrix A:\n" << A << "\nand matrix B\n" << B << "\nare not the same\n"; ASSERT_EQ((size_t)A.cols(),(size_t)B.cols()) << message << "\nMatrix A:\n" << A << "\nand matrix B\n" << B << "\nare not the same\n"; for(int r = 0; r < A.rows(); r++) { for(int c = 0; c < A.cols(); c++) { ASSERT_EQ(A(r,c),B(r,c)) << message << "\nEquality comparison failed at (" << r << "," << c << ")\n" << "\nMatrix A:\n" << A << "\nand matrix B\n" << B; } } }
void assertNear(const M1 & A, const M2 & B, T tolerance, std::string const & message = "") { // Note: If these assertions fail, they only abort this subroutine. // see: http://code.google.com/p/googletest/wiki/AdvancedGuide#Using_Assertions_in_Sub-routines // \todo better handling of this ASSERT_EQ((size_t)A.rows(),(size_t)B.rows()) << message << "\nMatrix A:\n" << A << "\nand matrix B\n" << B << "\nare not the same\n"; ASSERT_EQ((size_t)A.cols(),(size_t)B.cols()) << message << "\nMatrix A:\n" << A << "\nand matrix B\n" << B << "\nare not the same\n"; for(int r = 0; r < A.rows(); r++) { for(int c = 0; c < A.cols(); c++) { ASSERT_NEAR(A(r,c),B(r,c),tolerance) << message << "\nTolerance comparison failed at (" << r << "," << c << ")\n" << "\nMatrix A:\n" << A << "\nand matrix B\n" << B; } } }
void assertFinite(const M1 & A, std::string const & message = "") { for(int r = 0; r < A.rows(); r++) { for(int c = 0; c < A.cols(); c++) { ASSERT_TRUE(std::isfinite(A(r,c))) << std::endl << "Check for finite values failed at A(" << r << "," << c << "). Matrix A:" << std::endl << A << std::endl; } } }