]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/matrix.h
Clearer API for slicing and dicing vectors, and also matrices
[libs/math.git] / source / linal / matrix.h
index 6c04454296e51ae2162e0534f1def87fa379afac..929c48247e517aa47d837df1438c94465e25064a 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
@@ -30,6 +30,9 @@ public:
        T &operator()(unsigned i, unsigned j) { return element(i, j); }
        const T &operator()(unsigned i, unsigned j) const { return element(i, j); }
 
+       template<unsigned P, unsigned Q>
+       Matrix<T, P, Q> block(unsigned, unsigned) const;
+
        Matrix &operator*=(T);
        Matrix &operator/=(T);
        Matrix &operator+=(const Matrix &);
@@ -68,6 +71,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>
@@ -77,6 +81,18 @@ 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>::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>