Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
matrix.h
1 
2 /***************************************************************************
3  * matrix.h - A matrix class
4  *
5  * Created: Wed Sep 26 14:28:01 2007
6  * Copyright 2007-2009 Daniel Beck <beck@kbsg.rwth-aachen.de>
7  * 2009 Masrur Doostdar <doostdar@kbsg.rwth-aachen.de>
8  * 2009 Christof Rath <c.rath@student.tugraz.at>
9  *
10  ****************************************************************************/
11 
12 /* This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version. A runtime exception applies to
16  * this software (see LICENSE.GPL_WRE file mentioned below for details).
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Library General Public License for more details.
22  *
23  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
24  */
25 
26 #ifndef __GEOMETRY_MATRIX_H_
27 #define __GEOMETRY_MATRIX_H_
28 
29 namespace fawkes {
30 
31 class Vector;
32 
33 class Matrix
34 {
35 public:
36  Matrix(unsigned int num_rows = 0, unsigned int num_cols = 0,
37  float *data = 0, bool manage_own_memory = true);
38  Matrix(const Matrix &tbc);
39  ~Matrix();
40 
41  void size(unsigned int &num_rows, unsigned int &num_cols) const;
42  inline unsigned int num_rows() const { return m_num_rows; }
43  inline unsigned int num_cols() const { return m_num_cols; }
44 
45  Matrix &id();
46  static Matrix get_id(unsigned int size, float *data_buffer = 0);
47  static Matrix get_diag(unsigned int size, float value, float *data_buffer = 0);
48 
49  Matrix &transpose();
50  Matrix get_transpose() const;
51 
52  Matrix &invert();
53  Matrix get_inverse() const;
54 
55  float det() const;
56 
57  const float* get_data() const { return m_data; }
58  float* get_data() { return m_data; }
59 
60  Matrix get_submatrix(unsigned int row, unsigned int col,
61  unsigned int num_rows, unsigned int num_cols) const;
62 
63  void overlay(unsigned int row, unsigned int col, const Matrix &m);
64 
65  float operator()(unsigned int row, unsigned int col) const;
66  float &operator()(unsigned int row, unsigned int col);
67 
68  Matrix& operator=(const Matrix &rhs);
69 
70  Matrix operator*(const Matrix &rhs) const;
71  Matrix& operator*=(const Matrix &rhs);
72 
73  Vector operator*(const Vector &cv) const;
74 
75  Matrix operator*(const float &f) const;
76  Matrix& operator*=(const float &f);
77 
78  Matrix operator/(const float &f) const;
79  Matrix& operator/=(const float &f);
80 
81  Matrix operator+(const Matrix &rhs) const;
82  Matrix& operator+=(const Matrix &rhs);
83 
84  Matrix operator-(const Matrix &rhs) const;
85  Matrix& operator-=(const Matrix &rhs);
86 
87  bool operator==(const Matrix &rhs) const;
88 
89  void print_info(const char *name = 0, const char *col_sep = 0,
90  const char *row_sep = 0) const;
91 
92 private:
93  void mult_row(unsigned int row, float factor);
94  void sub_row(unsigned int row_a, unsigned int row_b, float factor);
95  inline float data(unsigned int row, unsigned int col) const
96  {
97  return m_data[row * m_num_cols + col];
98  }
99  inline float& data(unsigned int row, unsigned int col)
100  {
101  return m_data[row * m_num_cols + col];
102  }
103 
104 private:
105  float *m_data;
106 
107  unsigned int m_num_rows;
108  unsigned int m_num_cols;
109  unsigned int m_num_elements;
110 
111  bool m_own_memory;
112 };
113 
114 } // end namespace fawkes
115 
116 #endif /* __GEOMETRY_MATRIX_H_ */
Matrix & id()
Sets the diagonal elements to 1.0 and all other to 0.0.
Definition: matrix.cpp:172
unsigned int num_cols() const
Return the number of columns in the Matrix.
Definition: matrix.h:43
Matrix operator*(const Matrix &rhs) const
Matrix multiplication operator.
Definition: matrix.cpp:535
Matrix & operator-=(const Matrix &rhs)
Subtract-assign operator.
Definition: matrix.cpp:801
Matrix & operator*=(const Matrix &rhs)
Combined matrix-multipliation and assignement operator.
Definition: matrix.cpp:572
A simple column vector.
Definition: vector.h:31
A general matrix class.
Definition: matrix.h:33
float det() const
Computes the determinant of the matrix.
Definition: matrix.cpp:374
Matrix operator/(const float &f) const
Divide every element of the matrix with the given scalar.
Definition: matrix.cpp:692
Matrix & operator/=(const float &f)
Combined scalar division and assignment operator.
Definition: matrix.cpp:710
unsigned int num_rows() const
Return the number of rows in the Matrix.
Definition: matrix.h:42
Matrix operator+(const Matrix &rhs) const
Addition operator.
Definition: matrix.cpp:726
void print_info(const char *name=0, const char *col_sep=0, const char *row_sep=0) const
Print matrix to standard out.
Definition: matrix.cpp:890
Matrix & operator+=(const Matrix &rhs)
Add-assign operator.
Definition: matrix.cpp:751
void overlay(unsigned int row, unsigned int col, const Matrix &m)
Overlays another matrix over this matrix.
Definition: matrix.cpp:445
const float * get_data() const
Returns the const data pointer.
Definition: matrix.h:57
void size(unsigned int &num_rows, unsigned int &num_cols) const
Determines the dimensions of the matrix.
Definition: matrix.cpp:161
static Matrix get_id(unsigned int size, float *data_buffer=0)
Creates a quadratic matrix with dimension size and sets the diagonal elements to 1.0.
Definition: matrix.cpp:193
float * get_data()
Returns the data pointer.
Definition: matrix.h:58
bool operator==(const Matrix &rhs) const
Comparison operator.
Definition: matrix.cpp:825
Matrix & transpose()
Transposes the matrix.
Definition: matrix.cpp:233
Matrix get_inverse() const
Computes a matrix that is the inverse of this matrix.
Definition: matrix.cpp:362
Matrix & invert()
Inverts the matrix.
Definition: matrix.cpp:317
Matrix get_submatrix(unsigned int row, unsigned int col, unsigned int num_rows, unsigned int num_cols) const
Returns a submatrix of the matrix.
Definition: matrix.cpp:415
float operator()(unsigned int row, unsigned int col) const
(Read-only) Access-operator.
Definition: matrix.cpp:472
Matrix operator-(const Matrix &rhs) const
Subtraction operator.
Definition: matrix.cpp:775
Matrix get_transpose() const
Computes a matrix that is the transposed of this matrix.
Definition: matrix.cpp:294
Matrix & operator=(const Matrix &rhs)
Assignment operator.
Definition: matrix.cpp:506
~Matrix()
Destructor.
Definition: matrix.cpp:151
Matrix(unsigned int num_rows=0, unsigned int num_cols=0, float *data=0, bool manage_own_memory=true)
Constructor.
Definition: matrix.cpp:102
static Matrix get_diag(unsigned int size, float value, float *data_buffer=0)
Creates a quadratic matrix with dimension size and sets the diagonal elements to value.
Definition: matrix.cpp:207