Library of Assembled Shared Sources
matrix.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::num::Matrix
46 * @brief a dynamic sized n-dimensional matrix with expression templates
47 * @author Bram de Greve [BdG]
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_MATRIX_H
51#define LASS_GUARDIAN_OF_INCLUSION_NUM_MATRIX_H
52
53#include "num_common.h"
54#include "num_traits.h"
56#include "../util/call_traits.h"
58
59namespace lass
60{
61namespace num
62{
63
64template
65<
66 typename T,
67 typename S = impl::MStorage<T>
68>
69class Matrix
70{
71public:
72
73 typedef Matrix<T, S> TSelf;
74 typedef S TStorage;
75 typedef typename TStorage::TSize TSize;
76 typedef typename num::NumTraits<TSize>::signedType TSigned;
77
78 typedef typename util::CallTraits<T>::TValue TValue;
79 typedef typename util::CallTraits<T>::TParam TParam;
80 typedef typename util::CallTraits<T>::TReference TReference;
81 typedef typename util::CallTraits<T>::TConstReference TConstReference;
82 typedef num::NumTraits<T> TNumTraits;
83 typedef TValue* TPointer;
84
85 template <typename U> struct Rebind
86 {
87 typedef Matrix<U, S> Type;
88 };
89
90 class Row
91 {
92 public:
94 T& operator[](TSize column) { return matrix_(row_, column); }
95 const T& operator[](TSize column) const { return matrix_(row_, column); }
96 T& at(TSigned column) { LASS_ASSERT(static_cast<TSigned>(row_) >= 0); return matrix_.at(static_cast<TSigned>(row_), column); }
97 const T& at(TSigned column) const { LASS_ASSERT(static_cast<TSigned>(row_) >= 0); return matrix_.at(static_cast<TSigned>(row_), column); }
98 const TSize size() const { return matrix_.columns(); }
99 iterator begin() { return iterator(*this, 0); }
100 iterator end() { return iterator(*this, size()); }
101 private:
102 friend class Matrix<T, S>;
103 Row(Matrix<T, S>& matrix, TSize row): matrix_(matrix), row_(row) {}
104 Matrix<T, S>& matrix_;
105 TSize row_;
106 };
107
108 class ConstRow
109 {
110 public:
111 ConstRow(const Row& other): matrix_(other.matrix_), row_(other.row_) {}
112 const T& operator[](TSize column) const { return matrix_(row_, column); }
113 const T& at(TSigned column) const { LASS_ASSERT(static_cast<TSigned>(row_) >= 0); return matrix_.at(static_cast<TSigned>(row_), column); }
114 const TSize size() const { return matrix_.columns(); }
115 private:
116 friend class Matrix<T, S>;
117 ConstRow(const Matrix<T, S>& matrix, TSize row): matrix_(matrix), row_(row) {}
118 const Matrix<T, S>& matrix_;
119 TSize row_;
120 };
121
122 class Column
123 {
124 public:
126 T& operator[](TSize row) { return matrix_(row, column_); }
127 const T& operator[](TSize row) const { return matrix_(row, column_); }
128 T& at(TSigned row) { LASS_ASSERT(static_cast<TSigned>(column_) >= 0); return matrix_.at(row, static_cast<TSigned>(column_)); }
129 const T& at(TSigned row) const { LASS_ASSERT(static_cast<TSigned>(column_) >= 0); return matrix_.at(row, static_cast<TSigned>(column_)); }
130 const TSize size() const { return matrix_.rows(); }
131 iterator begin() { return iterator(*this, 0); }
132 iterator end() { return iterator(*this, matrix_.columns()); }
133 private:
134 friend class Matrix<T, S>;
135 Column(Matrix<T, S>& matrix, TSize column): matrix_(matrix), column_(column) {}
136 Matrix<T, S>& matrix_;
137 TSize column_;
138 };
139
140 class ConstColumn
141 {
142 public:
143 ConstColumn(const Column& other): matrix_(other.matrix_), column_(other.column_) {}
144 const T& operator[](TSize row) const { return matrix_(row, column_); }
145 const T& at(TSigned row) const { LASS_ASSERT(static_cast<TSigned>(column_) >= 0); return matrix_.at(row, static_cast<TSigned>(column_)); }
146 const TSize size() const { return matrix_.rows(); }
147 private:
148 friend class Matrix<T, S>;
149 ConstColumn(const Matrix<T, S>& matrix, TSize column): matrix_(matrix), column_(column) {}
150 const Matrix<T, S>& matrix_;
151 TSize column_;
152 };
153
155 explicit Matrix(TSize rows, TSize columns);
156 explicit Matrix(const TStorage& storage);
157 template <typename T2, typename S2> Matrix(const Matrix<T2, S2>& other);
158
159 template <typename T2, typename S2> Matrix<T, S>& operator=(const Matrix<T2, S2>& other);
160
161 const TSize rows() const;
162 const TSize columns() const;
163
164 const TValue operator()(TSize row, TSize column) const;
165 TReference operator()(TSize row, TSize column);
166 const TValue at(TSigned row, TSigned column) const;
167 TReference at(TSigned row, TSigned column);
168
169 ConstRow row(TSigned index) const;
170 Row row(TSigned index);
171 ConstColumn column(TSigned index) const;
172 Column column(TSigned index);
173
174 const Matrix<T, S>& operator+() const;
176
177 template <typename S2> Matrix<T, S>& operator+=(const Matrix<T, S2>& other);
178 template <typename S2> Matrix<T, S>& operator-=(const Matrix<T, S2>& other);
179 Matrix<T, S>& operator*=(TParam scalar);
180 Matrix<T, S>& operator/=(TParam scalar);
181
182 void setZero(TSize rows, TSize columns);
183 void setIdentity(TSize size);
184
185 bool isEmpty() const;
186 bool isZero() const;
187 bool isIdentity() const;
188 bool isDiagonal() const;
189 bool isSquare() const;
190
192 void invert();
193
194 const TStorage& storage() const;
195 TStorage& storage();
196 void swap(Matrix<T, S>& other);
197
198private:
199
200 TStorage storage_;
201};
202
203template <typename T, typename S1, typename S2>
204bool operator==(const Matrix<T, S1>& a, const Matrix<T, S2>& b);
205template <typename T, typename S1, typename S2>
206inline bool operator!=(const Matrix<T, S1>& a, const Matrix<T, S2>& b);
207
208template <typename T, typename S1, typename S2>
209const Matrix<T, impl::MAdd<T, S1, S2> > operator+(const Matrix<T, S1>& a, const Matrix<T, S2>& b);
210template <typename T, typename S1, typename S2>
211const Matrix<T, impl::MSub<T, S1, S2> > operator-(const Matrix<T, S1>& a, const Matrix<T, S2>& b);
212template <typename T, typename S1, typename S2>
213const Matrix<T, impl::MProd<T, S1, S2> > operator*(const Matrix<T, S1>& a, const Matrix<T, S2>& b);
214
215template <typename T, typename S>
216const Matrix<T, impl::MAdd<T, impl::MScalar<T>, S> > operator+(const T& a, const Matrix<T, S>& b);
217template <typename T, typename S>
218const Matrix<T, impl::MSub<T, impl::MScalar<T>, S> > operator-(const T& a, const Matrix<T, S>& b);
219template <typename T, typename S>
220const Matrix<T, impl::MMul<T, impl::MScalar<T>, S> > operator*(const T& a, const Matrix<T, S>& b);
221
222template <typename T, typename S>
223const Matrix<T, impl::MAdd<T, S, impl::MScalar<T> > > operator+(const Matrix<T, S>& a, typename Matrix<T, S>::TParam b);
224template <typename T, typename S>
225const Matrix<T, impl::MAdd<T, S, impl::MScalar<T> > > operator-(const Matrix<T, S>& a, typename Matrix<T, S>::TParam b);
226template <typename T, typename S>
227const Matrix<T, impl::MMul<T, S, impl::MScalar<T> > > operator*(const Matrix<T, S>& a, typename Matrix<T, S>::TParam b);
228template <typename T, typename S>
229const Matrix<T, impl::MMul<T, S, impl::MScalar<T> > > operator/(const Matrix<T, S>& a, typename Matrix<T, S>::TParam b);
230
231template <typename T, typename S, typename S2>
232void solve(const Matrix<T, S>& a, Matrix<T, S2>& bx, bool improve = true);
233
234
235template <typename T, typename S, typename Char, typename Traits>
236std::basic_ostream<Char, Traits>&
237operator<<(std::basic_ostream<Char, Traits>& stream, const Matrix<T, S>& matrix);
238
239}
240
241}
242
243#include "matrix.inl"
244
245#ifdef LASS_GUARDIAN_OF_INCLUSION_NUM_VECTOR_H
246#include "matrix_vector.h"
247#endif
248
249#endif
250
251// EOF
a dynamic sized n-dimensional matrix with expression templates
Definition matrix.h:70
bool isDiagonal() const
test if this matrix is an diagonal matrix
Definition matrix.inl:634
Column column(TSigned index)
return a column of matrix.
Definition matrix.inl:327
Matrix< T, S > & operator=(const Matrix< T2, S2 > &other)
assign storage/expression matrix to this (this should be a storage matrix).
Definition matrix.inl:164
Matrix< T, S > & operator/=(TParam scalar)
scalar multiplication assignment of matrix
Definition matrix.inl:470
void setIdentity(TSize size)
set matrix to an identity matrix.
Definition matrix.inl:520
bool isIdentity() const
test if this matrix equals a identity matrix
Definition matrix.inl:598
Matrix< T, S > & operator*=(TParam scalar)
scalar multiplication assignment of matrix
Definition matrix.inl:448
Matrix< T, S > & operator-=(const Matrix< T, S2 > &other)
componentswise subtraction assignment of two matrices This method is equivalent to this+=(-iB) .
Definition matrix.inl:421
bool isSquare() const
return true if matrix has as many rows as columns
Definition matrix.inl:663
TReference at(TSigned row, TSigned column)
access the component value at position (row, column) (row, column) will be wrapped to range [0,...
Definition matrix.inl:276
Matrix(const Matrix< T2, S2 > &other)
assign storage/expression matrix to this (this should be a storage matrix).
Definition matrix.inl:133
void invert()
replace matrix by its inverse.
Definition matrix.inl:696
const TValue at(TSigned row, TSigned column) const
return the component value at position (row, column) (row, column) will be wrapped to range [0,...
Definition matrix.inl:261
Row row(TSigned index)
return a row of matrix.
Definition matrix.inl:302
const Matrix< T, impl::MNeg< T, S > > operator-() const
return a vector with all components negated (-a)(i, j) == -(a(i, j)).
Definition matrix.inl:361
Matrix()
constructs an empty matrix
Definition matrix.inl:84
const TSize columns() const
return number of columns in matrix.
Definition matrix.inl:215
void setZero(TSize rows, TSize columns)
set this to a zero matrix of rows rows and columns columns.
Definition matrix.inl:496
const Matrix< T, S > & operator+() const
A weird way to get back the same object.
Definition matrix.inl:343
TReference operator()(TSize row, TSize column)
access the component value at position (row, column)
Definition matrix.inl:246
const TSize rows() const
return number of rows in matrix.
Definition matrix.inl:196
ConstRow row(TSigned index) const
return a row of matrix.
Definition matrix.inl:289
const Matrix< T, impl::MTrans< T, S > > transposed() const
return transposed matrix
Definition matrix.inl:677
void swap(Matrix< T, S > &other)
swap two matrices
Definition matrix.inl:750
bool isEmpty() const
return true if this is a (0�0) matrix
Definition matrix.inl:544
ConstColumn column(TSigned index) const
return a column of matrix.
Definition matrix.inl:314
Matrix(TSize rows, TSize columns)
Construct a matrix of dimension rows x columns.
Definition matrix.inl:106
Matrix< T, S > & operator+=(const Matrix< T, S2 > &other)
componentswise addition assignment of two matrices
Definition matrix.inl:388
const TValue operator()(TSize row, TSize column) const
return the component value at position (row, column)
Definition matrix.inl:229
bool isZero() const
test if this matrix equals a zero matrix.
Definition matrix.inl:563
iterator adaptor to access members.
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53