aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/YAGE/Math/matrix.hpp22
-rw-r--r--test/matrixtest.cpp66
2 files changed, 77 insertions, 11 deletions
diff --git a/include/YAGE/Math/matrix.hpp b/include/YAGE/Math/matrix.hpp
index d4566c98..d1c5fd74 100644
--- a/include/YAGE/Math/matrix.hpp
+++ b/include/YAGE/Math/matrix.hpp
@@ -236,6 +236,22 @@ Matrix<M, N, T> operator-(const T &lhs, Matrix<M, N, T> rhs)
}
template<int M, int N, class T>
+bool operator==(const Matrix<M, N, T> &lhs, const Matrix<M, N, T> &rhs)
+{
+ for(int i=0; i<M; ++i)
+ {
+ for(int j=0; j<N; ++j)
+ {
+ if(lhs[i][j]!=rhs[i][j])
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+template<int M, int N, class T>
std::ostream& operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
{
return os<<mat.toString();
@@ -304,12 +320,12 @@ template<int M, int N, class T> Matrix<N, M, T> transpose(const Matrix<M, N, T>
return trans;
}
-template<int R, class T> T dot(const Vector<R, T> &v1, const Vector<R, T> &v2)
+template<int R, class T> T dot(const Matrix<R, 1, T> &m1, const Matrix<R, 1, T> &m2)
{
T sum=0;
for(int i=0; i<R; ++i)
{
- sum += v1[i]*v2[i];
+ sum += m1[i][0]*m2[i][0];
}
return sum;
}
@@ -328,7 +344,7 @@ Matrix<M, Q, T> multiply(const Matrix<M, N, T> &m1, const Matrix<P, Q, T> &m2)
{
for(int j=0; j<Q; ++j)
{
- // int sum=0;
+ res[i][j] = dot(transpose(m1.getRow(i)), m2.getCol(j));
}
}
diff --git a/test/matrixtest.cpp b/test/matrixtest.cpp
index fd9a0bb7..ce4e9187 100644
--- a/test/matrixtest.cpp
+++ b/test/matrixtest.cpp
@@ -14,6 +14,10 @@ void test(TestBench &tb, const std::string &test_name, bool result)
{
tb.startTest(test_name);
tb.endTest(result);
+ if(!result)
+ {
+ throw std::runtime_error(test_name+" failed...");
+ }
}
bool matrixAssign()
@@ -21,27 +25,73 @@ bool matrixAssign()
yage::Matrix<4, 5, int> m;
m[2][3]=5;
- return m[2][3]==5;
+ return m[2][3]==4;
}
bool matrixAddition()
{
yage::Matrix<4, 4, int> m1, m2;
- m1[1][1] = 293;
- m2[1][1] = 583;
+ m1[1][1]=293;
+ m2[1][1]=583;
- yage::Matrix<4, 4, int> m3 = m1 + m2;
+ yage::Matrix<4, 4, int> m3=m1+m2;
- return m3[1][1] == 876;
+ return m3[1][1]==876;
+}
+
+bool vectorDotProduct()
+{
+ yage::Vector<3, int> v1, v2;
+ v1[0] = 2;
+ v1[1] = 3;
+ v1[2] = 5;
+ v2[0] = 9;
+ v2[1] = 6;
+ v2[2] = 8;
+
+ int x = yage::matrix::dot(v1, v2);
+
+ return x==76;
+}
+
+bool matrixMultiplication()
+{
+
}
int main()
{
TestBench tb;
- test(tb, "Matrix Assign", matrixAssign());
- test(tb, "Matrix Addition", matrixAddition());
+ bool all_passed=true;
+
+ try
+ {
+ test(tb, "Matrix Assign", matrixAssign());
+ }
+ catch(std::exception e)
+ {
+ std::cout<<e.what()<<'\n';
+ }
+
+ try
+ {
+ test(tb, "Matrix Addition", matrixAddition());
+ }
+ catch(std::exception e)
+ {
+ std::cout<<e.what()<<'\n';
+ }
+
+ try
+ {
+ test(tb, "Vector Dot Product", vectorDotProduct());
+ }
+ catch(std::string e)
+ {
+ std::cout<<e<<'\n';
+ }
tb.printResults();
- return 0;
+ return all_passed;
}