]> 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 00e960d9ce8111de4d88bd0bf664d470183f442c..2711d40a637489c1bfa3cc9676c2fb8043f978d4 100644 (file)
@@ -1,17 +1,22 @@
-#ifndef MSP_LIEAL_MATMIX_H_
-#define MSP_LIEAL_MATMIX_H_
+#ifndef MSP_LINAL_MATRIX_H_
+#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];
 
@@ -20,30 +25,98 @@ public:
        Matrix(const T *);
        template<typename U>
        Matrix(const Matrix<U, M, N> &);
+
        static Matrix from_columns(const Vector<T, M> *);
        static Matrix from_rows(const Vector<T, N> *);
 
-       T &operator()(unsigned, unsigned);
-       const T &operator()(unsigned, unsigned) const;
+       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 &);
        Matrix &operator-=(const Matrix &);
 
-       Matrix<T, N, M> transpose() const;
+       Matrix &exchange_rows(unsigned, unsigned);
+       Matrix &multiply_row(unsigned, T);
+       Matrix &add_row(unsigned, unsigned, T);
 };
 
 template<typename T, unsigned M, unsigned N>
-inline T &Matrix<T, M, N>::operator()(unsigned i, unsigned j)
+inline Matrix<T, M, N>::Matrix()
+{
+       std::fill(data, data+M*N, T());
+}
+
+template<typename T, unsigned M, unsigned N>
+inline Matrix<T, M, N>::Matrix(const T *d)
+{
+       std::copy(d, d+M*N, data);
+}
+
+template<typename T, unsigned M, unsigned N>
+template<typename U>
+inline Matrix<T, M, N>::Matrix(const Matrix<U, M, N> &other)
+{
+       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>
+inline Matrix<T, M, N> Matrix<T, M, N>::from_columns(const Vector<T, M> *v)
+{
+       Matrix<T, M, N> m;
+       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>
+inline Matrix<T, M, N> Matrix<T, M, N>::from_rows(const Vector<T, N> *v)
+{
+       Matrix<T, M, N> m;
+       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
 {
-       return data[i+M*j];
+       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>
-inline const T &Matrix<T, M, N>::operator()(unsigned i, unsigned j) const
+template<unsigned P, unsigned Q>
+inline Matrix<T, P, Q> Matrix<T, M, N>::block(unsigned y, unsigned x) const
 {
-       return data[i+M*j];
+       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>
@@ -67,6 +140,37 @@ inline Matrix<T, M, N> operator*(T s, const Matrix<T, M, N> &m)
        return m*s;
 }
 
+template<typename T, unsigned M, unsigned P, unsigned N>
+inline Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
+{
+       Matrix<T, M, N> r;
+       for(unsigned i=0; i<M; ++i)
+               for(unsigned j=0; j<N; ++j)
+                       for(unsigned k=0; k<P; ++k)
+                               r(i, j) += m1(i, k)*m2(k, j);
+       return r;
+}
+
+template<typename T, unsigned M, unsigned N>
+inline Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
+{
+       Vector<T, M> r;
+       for(unsigned i=0; i<M; ++i)
+               for(unsigned j=0; j<N; ++j)
+                       r[i] += m(i, j)*v[j];
+       return r;
+}
+
+template<typename T, unsigned M, unsigned N>
+inline Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
+{
+       Vector<T, N> r;
+       for(unsigned j=0; j<N; ++j)
+               for(unsigned i=0; i<M; ++i)
+                       r[j] += v[i]*m(i, j);
+       return r;
+}
+
 template<typename T, unsigned M, unsigned N>
 inline Matrix<T, M, N> &Matrix<T, M, N>::operator/=(T s)
 {
@@ -112,37 +216,72 @@ inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N
        return r -= m2;
 }
 
-template<typename T, unsigned M, unsigned P, unsigned N>
-Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
+template<typename T, unsigned M, unsigned N>
+inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
 {
-       Matrix<T, M, N> r;
-       for(unsigned i=0; i<M; ++i)
-               for(unsigned j=0; j<N; ++j)
-                       for(unsigned k=0; k<P; ++k)
-                               r.at(i, j) += m1(i, k)*m2(k, j);
-       return r;
+       for(unsigned j=0; j<N; ++j)
+               for(unsigned i=0; i<M; ++i)
+                       if(a(i, j)!=b(i, j))
+                               return false;
+       return true;
 }
 
 template<typename T, unsigned M, unsigned N>
-Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
+inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
 {
-       Vector<T, M> r;
-       for(unsigned i=0; i<M; ++i)
-               for(unsigned j=0; j<N; ++j)
-                       r[i] += m(i, j)*v[j];
-       return r;
+       using std::swap;
+       for(unsigned k=0; k<N; ++k)
+               swap(element(i, k), element(j, k));
+       return *this;
 }
 
 template<typename T, unsigned M, unsigned N>
-Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
+inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_row(unsigned i, T s)
 {
-       Vector<T, N> r;
+       for(unsigned k=0; k<N; ++k)
+               element(i, k) *= s;
+       return *this;
+}
+
+template<typename T, unsigned M, unsigned N>
+inline Matrix<T, M, N> &Matrix<T, M, N>::add_row(unsigned i, unsigned j, T s)
+{
+       for(unsigned k=0; k<N; ++k)
+               element(j, k) += element(i, k)*s;
+       return *this;
+}
+
+template<typename T, unsigned M, unsigned N>
+inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
+{
+       Matrix<T, N, M> r;
        for(unsigned j=0; j<N; ++j)
                for(unsigned i=0; i<M; ++i)
-                       r[j] += v[i]*m.at(i, j);
+                       r(j, i) = m(i, j);
        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