]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/matrix.h
Add missing return statements
[libs/math.git] / source / linal / matrix.h
index 94a6e0991b2d13502e2f998107d5e26f5831ad04..f18eb2f4cfb52504214b84015688e46d0d7be121 100644 (file)
@@ -8,7 +8,7 @@ namespace Msp {
 namespace LinAl {
 
 /**
-A general mathematical matrix.
+A general mathematical matrix with M rows and N columns.
 */
 template<typename T, unsigned M, unsigned N>
 class Matrix
@@ -25,10 +25,10 @@ public:
        static Matrix from_columns(const Vector<T, M> *);
        static Matrix from_rows(const Vector<T, N> *);
 
-       T &element(unsigned, unsigned);
-       const T &element(unsigned, unsigned) const;
-       T &operator()(unsigned, unsigned);
-       const T &operator()(unsigned, unsigned) const;
+       T &element(unsigned i, unsigned j) { return data[i+M*j]; }
+       const T &element(unsigned i, unsigned j) const { return data[i+M*j]; }
+       T &operator()(unsigned i, unsigned j) { return element(i, j); }
+       const T &operator()(unsigned i, unsigned j) const { return element(i, j); }
 
        Matrix &operator*=(T);
        Matrix &operator/=(T);
@@ -54,9 +54,11 @@ inline Matrix<T, M, N>::Matrix(const T *d)
 
 template<typename T, unsigned M, unsigned N>
 template<typename U>
-inline Matrix<T, M, N>::Matrix(const Matrix<U, M, N> &m)
+inline Matrix<T, M, N>::Matrix(const Matrix<U, M, N> &other)
 {
-       std::copy(m.data, m.data+M*N, data);
+       for(unsigned i=0; i<M; ++i)
+               for(unsigned j=0; j<N; ++j)
+                       element(i, j) = other(i, j);
 }
 
 template<typename T, unsigned M, unsigned N>
@@ -66,6 +68,7 @@ inline Matrix<T, M, N> Matrix<T, M, N>::from_columns(const Vector<T, M> *v)
        for(unsigned i=0; i<M; ++i)
                for(unsigned j=0; j<N; ++j)
                        m(i, j) = v[j][i];
+       return m;
 }
 
 template<typename T, unsigned M, unsigned N>
@@ -75,30 +78,7 @@ inline Matrix<T, M, N> Matrix<T, M, N>::from_rows(const Vector<T, N> *v)
        for(unsigned i=0; i<M; ++i)
                for(unsigned j=0; j<N; ++j)
                        m(i, j) = v[i][j];
-}
-
-template<typename T, unsigned M, unsigned N>
-inline T &Matrix<T, M, N>::element(unsigned i, unsigned j)
-{
-       return data[i+M*j];
-}
-
-template<typename T, unsigned M, unsigned N>
-inline const T &Matrix<T, M, N>::element(unsigned i, unsigned j) const
-{
-       return data[i+M*j];
-}
-
-template<typename T, unsigned M, unsigned N>
-inline T &Matrix<T, M, N>::operator()(unsigned i, unsigned j)
-{
-       return element(i, j);
-}
-
-template<typename T, unsigned M, unsigned N>
-inline const T &Matrix<T, M, N>::operator()(unsigned i, unsigned j) const
-{
-       return element(i, j);
+       return m;
 }
 
 template<typename T, unsigned M, unsigned N>
@@ -211,8 +191,9 @@ inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
 template<typename T, unsigned M, unsigned N>
 inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
 {
+       using std::swap;
        for(unsigned k=0; k<N; ++k)
-               std::swap(element(i, k), element(j, k));
+               swap(element(i, k), element(j, k));
        return *this;
 }