aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/YAGE/Math/matrix.hpp26
-rw-r--r--test/matrix_test.cpp37
2 files changed, 33 insertions, 30 deletions
diff --git a/include/YAGE/Math/matrix.hpp b/include/YAGE/Math/matrix.hpp
index 5a753616..d4566c98 100644
--- a/include/YAGE/Math/matrix.hpp
+++ b/include/YAGE/Math/matrix.hpp
@@ -1,3 +1,11 @@
+/*
+ * created 22-06-17 by Yann Herklotz
+ *
+ * Matrix class that contains definitions for matrices, vectors and operations
+ * on vectors and matrices.
+ *
+ */
+
#ifndef YAGE_MATH_MATRIX_HPP
#define YAGE_MATH_MATRIX_HPP
@@ -12,11 +20,15 @@ namespace yage
{
template<int Rows, int Cols, class Type> class Matrix;
-template<int Rows, class Type> class Vector;
+// includes implementation details that should not be accessible to the user
namespace detail
{
+// Row class
+//
+// Used to implement the double square bracket operator and be able
+// to return the value by reference of the array.
template<int Rows, int Cols, class Type> class Row
{
private:
@@ -30,6 +42,7 @@ public:
Type& operator[](int col)
{
+ // the index is the y-position of the element in the matrix
return parent_->data_[index_*Cols+col];
}
@@ -41,8 +54,13 @@ public:
} // detail
+// Matrix class
+//
+// Implements the base Matrix class that is inherited by other classes to make them more
+// specific.
template<int Rows=4, int Cols=4, class Type=double> class Matrix
{
+ // friended with the row class so that it can access protected member data
friend class detail::Row<Rows, Cols, Type>;
protected:
std::vector<Type> data_;
@@ -60,6 +78,7 @@ public:
return Cols;
}
+ // returns the row in a row matrix
Matrix<1, Cols, Type> getRow(int row) const
{
Matrix<1, Cols, Type> rowMatrix;
@@ -70,6 +89,7 @@ public:
return rowMatrix;
}
+ // returns the column in a column matrix
Matrix<Rows, 1, Type> getCol(int col) const
{
Matrix<Rows, 1, Type> colMatrix;
@@ -80,16 +100,20 @@ public:
return colMatrix;
}
+ // iterator support for begin
typename std::vector<Type>::iterator begin()
{
return data_.begin();
}
+ // iterator support for end
typename std::vector<Type>::iterator end()
{
return data_.end();
}
+ // prints out the matrix, but can also be implemented by other classes to print data
+ // differently
virtual std::string toString() const
{
std::stringstream ss;
diff --git a/test/matrix_test.cpp b/test/matrix_test.cpp
index aa8bb71d..16548164 100644
--- a/test/matrix_test.cpp
+++ b/test/matrix_test.cpp
@@ -1,35 +1,14 @@
-#include "Math/math.hpp"
+/*
+ * created 22-06-17 by Yann Herklotz
+ *
+ * Tests the math class using different tests to see if matrix operations work
+ * correctly.
+ *
+ */
-#include <iostream>
+#include "Math/math.hpp"
int main()
{
- yage::Matrix<4, 4, int> m1, m2;
-
- yage::Vector<2, int> v1, v2;
-
- yage::Vector2d v3;
-
- m1[0][1]=1;
- m2[1][1]=2;
-
- v1[0]=2;
- v1[1]=3;
- v2[0]=5;
- v2[1]=2;
-
- v2 += v1+v1;
-
- v3.x() = 2;
- v3.y() = 1;
-
- auto m3=yage::matrix::transpose(v3);
- auto vec4=yage::Vector<2, double>(yage::matrix::transpose(m3));
-
- std::cout<<m2<<'\n';
- std::cout<<v3<<'\n';
- std::cout<<m3<<'\n';
- std::cout<<vec4<<'\n';
-
return 0;
}