]> 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 1c831be6b11cf6f4181366017c739c9ba5e4fd30..2711d40a637489c1bfa3cc9676c2fb8043f978d4 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_LINAL_MATRIX_H_
 
 #include <algorithm>
+#include <ostream>
 #include "vector.h"
 
 namespace Msp {
@@ -36,6 +37,9 @@ public:
        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;
 
@@ -257,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