]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/matrix.h
Add formatted output operators for vector and matrix classes
[libs/math.git] / source / linal / matrix.h
index ff73165d02c703c0efe263b27f23a522c54b7383..2711d40a637489c1bfa3cc9676c2fb8043f978d4 100644 (file)
@@ -2,17 +2,21 @@
 #define MSP_LINAL_MATRIX_H_
 
 #include <algorithm>
+#include <ostream>
 #include "vector.h"
 
 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
 {
+public:
+       typedef T ElementType;
+
 private:
        T data[M*N];
 
@@ -25,11 +29,23 @@ public:
        static Matrix from_columns(const Vector<T, M> *);
        static Matrix from_rows(const Vector<T, N> *);
 
+       unsigned rows() const { return M; }
+       unsigned columns() const { return N; }
+
        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); }
 
+       Vector<T, M> column(unsigned i) const { return Vector<T, M>(data+M*i); }
+       Vector<T, N> row(unsigned i) const { return Vector<T, N>(data+i, M); }
+
+       template<unsigned P, unsigned Q>
+       Matrix<T, P, Q> select(const Vector<unsigned, P> &, const Vector<unsigned, Q> &) const;
+
+       template<unsigned P, unsigned Q>
+       Matrix<T, P, Q> block(unsigned, unsigned) const;
+
        Matrix &operator*=(T);
        Matrix &operator/=(T);
        Matrix &operator+=(const Matrix &);
@@ -54,9 +70,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 +84,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,6 +94,29 @@ 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];
+       return m;
+}
+
+template<typename T, unsigned M, unsigned N>
+template<unsigned P, unsigned Q>
+inline Matrix<T, P, Q> Matrix<T, M, N>::select(const Vector<unsigned, P> &row_indices, const Vector<unsigned, Q> &col_indices) const
+{
+       Matrix<T, P, Q> r;
+       for(unsigned j=0; j<P; ++j)
+               for(unsigned i=0; i<Q; ++i)
+                       r(j, i) = element(row_indices[j], col_indices[i]);
+       return r;
+}
+
+template<typename T, unsigned M, unsigned N>
+template<unsigned P, unsigned Q>
+inline Matrix<T, P, Q> Matrix<T, M, N>::block(unsigned y, unsigned x) const
+{
+       Matrix<T, P, Q> r;
+       for(unsigned j=0; j<P; ++j)
+               for(unsigned i=0; i<Q; ++i)
+                       r(j, i) = element(y+j, x+i);
+       return r;
 }
 
 template<typename T, unsigned M, unsigned N>
@@ -187,8 +229,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;
 }
 
@@ -218,6 +261,27 @@ inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
        return r;
 }
 
+template<typename T, unsigned M, unsigned N>
+inline std::ostream &operator<<(std::ostream &s, const Matrix<T, M, N> &m)
+{
+       s << "Matrix" << M << 'x' << N << '(';
+       for(unsigned i=0; i<N; ++i)
+       {
+               if(i)
+                       s << ", ";
+               s << '[';
+               for(unsigned j=0; j<M; ++j)
+               {
+                       if(j)
+                               s << ", ";
+                       s << m(j, i);
+               }
+               s << ']';
+       }
+       s << ')';
+       return s;
+}
+
 } // namespace LinAl
 } // namespace Msp