wfmath  1.0.3
A math library for the Worldforge system.
oldmatrix.h
1 // -*-C++-*-
2 // matrix.h (Matrix<> class definition)
3 //
4 // The WorldForge Project
5 // Copyright (C) 2001 The WorldForge Project
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 //
21 // For information about WorldForge and its authors, please contact
22 // the Worldforge Web Site at http://www.worldforge.org.
23 
24 // Author: Ron Steinke
25 // Created: 2001-12-7
26 
27 #ifndef WFMATH_MATRIX_H
28 #define WFMATH_MATRIX_H
29 
30 #include <wfmath/vector.h>
31 
32 namespace WF { namespace Math {
33 
34 template<const int rows, const int columns> class RowVector;
35 
36 template<const int rows, const int columns>
37 Matrix<rows,columns> operator*(const double& d, const Matrix<rows,columns>& m);
38 template<const int rows, const int columns>
39 Matrix<rows,columns> OuterProduct(const Vector<rows>& v1, const Vector<columns>& v2);
40 
41 template<const int size>
42 Matrix<size,size> DiagonalMatrix(const Vector<size>& v);
43 template<const int size>
44 double Trace(const Matrix<size,size>& m);
45 template<const int size>
46 double Determinant(const Matrix<size,size>& m);
47 template<const int size>
48 Matrix<size,size> Inverse(const Matrix<size,size>& m);
49 
50 template<const int rows, const int columns = rows>
51 class Matrix {
52  public:
53  Matrix() {}
54  Matrix(const Matrix<rows,columns>& m);
55 
56  bool operator==(const Matrix<rows,columns>& m) const;
57  bool operator!=(const Matrix<rows,columns>& m) const {return !(*this == m);}
58 
59  // WARNING! This operator is for sorting only. It does not
60  // reflect any property of the matrix.
61  bool operator< (const Matrix<rows,columns>& m) const;
62 
63  // Division only makes sense for square matrices, make people use
64  // inverse() explicitly
65 
66  Matrix<rows,columns> operator+(const Matrix<rows,columns>& m) const;
67  Matrix<rows,columns> operator-(const Matrix<rows,columns>& m) const;
68  template<const int i>
69  Matrix<rows,i> operator*(const Matrix<columns,i>& m) const;
70  Matrix<rows,columns> operator*(const double& d) const;
71  Matrix<rows,columns> operator/(const double& d) const;
72 
73  Matrix<rows,columns> operator-() const; // Unary minus
74 
75  Matrix<rows,columns>& operator+=(const Matrix<rows,columns>& m);
76  Matrix<rows,columns>& operator-=(const Matrix<rows,columns>& m);
77  // Since we need to construct a matrix to hold the temporary values
78  // anyway, just use operator* to implement this one
79  Matrix<rows,columns>& operator*=(const Matrix<columns,columns>& m)
80  {*this = *this * m; return *this;}
81  Matrix<rows,columns>& operator*=(const double& d);
82  Matrix<rows,columns>& operator/=(const double& d);
83 
84  Vector<rows> operator*(const Vector<columns>& v) const;
85 
86 // FIXME it doesn't like this
87 // friend Matrix<rows,columns> operator*<rows,columns>(const double& d,
88 // const Matrix<rows,columns>& m);
89 
90  friend Matrix<rows,columns> OuterProduct<rows,columns>(const Vector<rows>& v1,
91  const Vector<columns>& v2);
92 
93  const double& elem(const int i, const int j) const {return m_elem[i][j];}
94  double& elem(const int i, const int j) {return m_elem[i][j];}
95 
96  Vector<columns> row(const int i) const;
97  void setRow(const int i, const Vector<columns>& v);
98  Vector<rows> column(const int i) const;
99  void setColumn(const int i, const Vector<rows>& v);
100 
101  Matrix<rows,columns>& zero();
102 
103  Matrix<columns,rows> transpose() const;
104 
105  // The following are only defined for square matrices. Trying
106  // to use them with non-square matrices will result in various
107  // compiler/linker errors.
108 
109  // FIXME is there some way to do this with partial specialization,
110  // without having to rewrite all the operator functions?
111 
112  Matrix<rows,columns>& identity();
113  Matrix<rows,columns>& diagonal(const Vector<rows>& v)
114  {return *this = DiagonalMatrix(v);}
115 
116  double trace() const {return Trace(*this);}
117  double determinant() const {return Determinant(*this);}
118  Matrix<columns,rows> inverse() const {return Inverse(*this);}
119 
120  friend Matrix<rows> DiagonalMatrix<rows>(const Vector<rows>& v);
121 
122  friend double Trace<rows>(const Matrix<rows>& m);
123  friend double Determinant<rows>(const Matrix<rows>& m);
124  friend Matrix<rows> Inverse<rows>(const Matrix<rows>& m);
125 
126  //TODO rotate() and string functions, Atlas and Tcross functions from stage/math
127 
128  private:
129  double m_elem[rows][columns];
130 };
131 
132 Matrix<3> SkewSymmetric(const Vector<3>& v);
133 
134 }} // namespace WF::Math
135 
136 #endif // WFMATH_MATRIX_H
WF::Math::Matrix
Definition: oldmatrix.h:51
WF::Math::RowVector
Definition: oldmatrix.h:34