diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-08-06 15:57:26 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-08-06 17:07:43 +0100 |
commit | 1d3657c760228be246554ab0e7bfe9c2818e5a5b (patch) | |
tree | 457c398c0f202fd5cc62130b70537c12b4ff2145 | |
parent | c2d74ea23c2270732b2090ef7d9f92c636a00b59 (diff) | |
download | YAGE-1d3657c760228be246554ab0e7bfe9c2818e5a5b.tar.gz YAGE-1d3657c760228be246554ab0e7bfe9c2818e5a5b.zip |
Improving matrix class
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | include/YAGE/Math/matrix.hpp | 8 | ||||
-rw-r--r-- | test/matrixtest.cpp | 49 |
3 files changed, 36 insertions, 23 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 77de0782..d9a69a05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,4 +78,4 @@ enable_testing() add_executable(matrixtest ${YAGE_TEST_DIR}/matrixtest.cpp) target_link_libraries(matrixtest gtest_main ${YAGE_LIB_DEP_L}) -add_test(NAME matrixtest COMMAND matrixtest) +add_test(NAME matrixtest COMMAND matrixtest --gtest_repeat=1000 --gtest_break_on_failure) diff --git a/include/YAGE/Math/matrix.hpp b/include/YAGE/Math/matrix.hpp index 2aca04db..8f490b41 100644 --- a/include/YAGE/Math/matrix.hpp +++ b/include/YAGE/Math/matrix.hpp @@ -96,6 +96,7 @@ protected: public: /// Initializes the size of the data_ vector Matrix<Rows, Cols, Type>() : data_(Rows*Cols) {} + Matrix<Rows, Cols, Type>(const std::vector<Type>& data) : data_(data) {} /// Returns the row size of the Matrix int rowSize() const @@ -273,8 +274,7 @@ Matrix<M, N, T> operator*(Matrix<M, N, T> lhs, const T &rhs) } template<int M, int N, class T> -Matrix<M, N, T> operator*(const T &lhs, Matrix<M, N, T> rhs) -{ +Matrix<M, N, T> operator*(const T &lhs, Matrix<M, N, T> rhs) { for(auto &data : rhs) { data*=lhs; @@ -312,7 +312,8 @@ template<int Rows=2, class Type=double> class Vector : public Matrix<Rows, 1, Ty { public: Vector<Rows, Type>() : Matrix<Rows, 1, Type>() {} - explicit Vector<Rows, Type>(const Matrix<Rows, 1, Type> &other) : Matrix<Rows, 1, Type>(other) {} + Vector<Rows, Type>(const Matrix<Rows, 1, Type>& other) : Matrix<Rows, 1, Type>(other) {} + Vector<Rows, Type>(const std::vector<Type>& data) : Matrix<Rows, 1, Type>(data) {} Type& operator[](int col) { @@ -345,6 +346,7 @@ template<class Type=double> class Vector2 : public Vector<2, Type> { public: Vector2<Type>() : Vector<2, Type>() {} + Vector2<Type>(const std::vector<Type>& data) : Vector<2, Type>(data) {} Vector2<Type>(Type x, Type y) { diff --git a/test/matrixtest.cpp b/test/matrixtest.cpp index aecf4c87..530d3530 100644 --- a/test/matrixtest.cpp +++ b/test/matrixtest.cpp @@ -6,38 +6,38 @@ * ---------------------------------------------------------------------------- */ +#include <algorithm> +#include <cstdlib> +#include <ctime> +#include <vector> + #include "Math/math.hpp" #include "gtest/gtest.h" -int matrixAssign(int number) +template<int Size> int matrixAssign(int number, int i, int j) { - yage::Matrix<4, 5, int> m; - m[2][3]=number; + yage::Matrix<Size, Size, int> m; + m[j][i]=number; - return m[2][3]; + return m[j][i]; } -int matrixAddition(int num1, int num2) +template<int Size> int matrixAddition(int num1, int num2) { - yage::Matrix<4, 4, int> m1, m2; + yage::Matrix<Size, Size, int> m1, m2; m1[1][1]=num1; m2[1][1]=num2; - yage::Matrix<4, 4, int> m3=m1+m2; + yage::Matrix<Size, Size, int> m3 = m1+m2; return m3[1][1]; } -bool vectorDotProduct() +int vectorDotProduct(const std::vector<int> &vec_contents_f, const std::vector<int> &vec_contents_s) { - yage::Vector<3, int> v1, v2; - v1[0] = 2; - v1[1] = 3; - v1[2] = 5; - v2[0] = 9; - v2[1] = 6; - v2[2] = 8; + yage::Vector<3, int> v1(vec_contents_f); + yage::Vector<3, int> v2(vec_contents_s); int x = yage::matrix::dot(v1, v2); @@ -53,21 +53,32 @@ bool matrixMultiplication() TEST(Matrix, Assign) { - ASSERT_EQ(29348, matrixAssign(29348)); + int rand_num = rand(); + ASSERT_EQ(rand_num, matrixAssign<10>(rand_num, 4, 2)); } TEST(Matrix, Addition) { - ASSERT_EQ(5793+2389, matrixAddition(5793, 2389)); + int rand_x = rand(); + int rand_y = rand(); + ASSERT_EQ(rand_x+rand_y, matrixAddition<10>(rand_x, rand_y)); } -TEST(Matrix, DotProduct) +TEST(Vector, DotProduct) { - ASSERT_TRUE(vectorDotProduct()); + std::vector<int> contents_i = { rand()%100, rand()%100, rand()%100 }; + std::vector<int> contents_j = { rand()%100, rand()%100, rand()%100 }; + int sum = 0; + for(std::size_t i = 0; i < contents_i.size(); ++i) + { + sum += contents_i[i]*contents_j[i]; + } + ASSERT_EQ(sum, vectorDotProduct(contents_i, contents_j)); } int main(int argc, char **argv) { + srand(static_cast<unsigned>(time(nullptr))); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } |