iRoCS Toolbox  1.1.0
TinyMatrixOperators.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2015 Thorsten Falk
4  *
5  * Image Analysis Lab, University of Freiburg, Germany
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 3 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 Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  **************************************************************************/
22 
23 /*======================================================================*/
29 /*======================================================================*/
30 
31 #ifndef ATBTINYMATRIXOPERATORS_HH
32 #define ATBTINYMATRIXOPERATORS_HH
33 
34 #ifdef HAVE_CONFIG_H
35 #include <config.hh>
36 #endif
37 
38 namespace blitz
39 {
40 
41 /*======================================================================*/
49 /*======================================================================*/
50  template<typename DataT, int NRows, int NColumns>
51  TinyMatrix<DataT,NRows,NColumns> operator-(
52  TinyMatrix<DataT,NRows,NColumns> const &m)
53  {
54  TinyMatrix<DataT,NRows,NColumns> res;
55  for (int i = 0; i < NRows * NColumns; ++i) res.data()[i] = -m.data()[i];
56  return res;
57  }
58 
59 /*======================================================================*/
68 /*======================================================================*/
69  template<typename DataT, int Dim1, int Dim2>
70  TinyVector<DataT,Dim2> operator*(
71  TinyMatrix<DataT,Dim2,Dim1> const &m,
72  TinyVector<DataT,Dim1> const &v)
73  {
74  TinyVector<DataT,Dim2> res(DataT(0));
75  for (int r = 0; r < Dim2; ++r)
76  for (int c = 0; c < Dim1; ++c) res(r) += m(r,c) * v(c);
77  return res;
78  }
79 
80 /*======================================================================*/
90 /*======================================================================*/
91  template<typename DataT, int Dim1, int Dim2>
92  TinyVector<DataT,Dim1> operator*(
93  TinyVector<DataT,Dim2> const &v,
94  TinyMatrix<DataT,Dim2,Dim1> const &m)
95  {
96  TinyVector<DataT,Dim1> res(DataT(0));
97  for (int r = 0; r < Dim2; ++r)
98  for (int c = 0; c < Dim1; ++c) res(c) += m(r,c) * v(r);
99  return res;
100  }
101 
102 /*======================================================================*/
111 /*======================================================================*/
112  template<typename DataT, int NRows, int NColumns>
113  TinyMatrix<DataT,NRows,NColumns> operator*(
114  DataT const &alpha, TinyMatrix<DataT,NRows,NColumns> const &m)
115  {
116  TinyMatrix<DataT,NRows,NColumns> res;
117  for (int i = 0; i < NRows * NColumns; ++i)
118  {
119  res.data()[i] = m.data()[i] * alpha;
120  }
121  return res;
122  }
123 
124 /*======================================================================*/
133 /*======================================================================*/
134  template<typename DataT, int NRows, int NColumns>
135  TinyMatrix<DataT,NRows,NColumns> operator*(
136  TinyMatrix<DataT,NRows,NColumns> const &m, DataT const &alpha)
137  {
138  TinyMatrix<DataT,NRows,NColumns> res;
139  for (int i = 0; i < NRows * NColumns; ++i)
140  {
141  res.data()[i] = m.data()[i] * alpha;
142  }
143  return res;
144  }
145 
146 /*======================================================================*/
155 /*======================================================================*/
156  template<typename DataT, int NRows, int NColumns>
157  TinyMatrix<DataT,NRows,NColumns> operator/(
158  TinyMatrix<DataT,NRows,NColumns> const &m, DataT const &alpha)
159  {
160  TinyMatrix<DataT,NRows,NColumns> res;
161  for (int i = 0; i < NRows * NColumns; ++i)
162  {
163  res.data()[i] = m.data()[i] / alpha;
164  }
165  return res;
166  }
167 
168 /*======================================================================*/
177 /*======================================================================*/
178  template<typename DataT, int NRows, int NColumns>
179  TinyMatrix<DataT,NRows,NColumns> operator-(
180  TinyMatrix<DataT,NRows,NColumns> const &lhs,
181  TinyMatrix<DataT,NRows,NColumns> const &rhs)
182  {
183  TinyMatrix<DataT,NRows,NColumns> res;
184  for (int i = 0; i < NRows * NColumns; ++i)
185  {
186  res.data()[i] = lhs.data()[i] - rhs.data()[i];
187  }
188  return res;
189  }
190 
191 /*======================================================================*/
200 /*======================================================================*/
201  template<typename DataT, int NRows, int NColumns>
202  TinyMatrix<DataT,NRows,NColumns> operator+(
203  TinyMatrix<DataT,NRows,NColumns> const &lhs,
204  TinyMatrix<DataT,NRows,NColumns> const &rhs)
205  {
206  TinyMatrix<DataT,NRows,NColumns> res;
207  for (int i = 0; i < NRows * NColumns; ++i)
208  {
209  res.data()[i] = lhs.data()[i] + rhs.data()[i];
210  }
211  return res;
212  }
213 
214 /*======================================================================*/
223 /*======================================================================*/
224  template<typename DataT, int NRows, int NColumns>
225  TinyMatrix<bool,NRows,NColumns> operator==(
226  TinyMatrix<DataT,NRows,NColumns> const &lhs,
227  TinyMatrix<DataT,NRows,NColumns> const &rhs)
228  {
229  TinyMatrix<bool,NRows,NColumns> res;
230  for (int i = 0; i < NRows * NColumns; ++i)
231  res.data()[i] = (lhs.data()[i] == rhs.data()[i]);
232  return res;
233  }
234 
235 /*======================================================================*/
244 /*======================================================================*/
245  template<typename DataT, int NRows, int NColumns>
246  TinyMatrix<bool,NRows,NColumns> operator!=(
247  TinyMatrix<DataT,NRows,NColumns> const &lhs,
248  TinyMatrix<DataT,NRows,NColumns> const &rhs)
249  {
250  TinyMatrix<bool,NRows,NColumns> res;
251  for (int i = 0; i < NRows * NColumns; ++i)
252  res.data()[i] = (lhs.data()[i] != rhs.data()[i]);
253  return res;
254  }
255 
256 /*======================================================================*/
266 /*======================================================================*/
267  template<typename DataT, int NRows, int NColumns>
268  TinyMatrix<bool,NRows,NColumns> operator<(
269  TinyMatrix<DataT,NRows,NColumns> const &lhs,
270  TinyMatrix<DataT,NRows,NColumns> const &rhs)
271  {
272  TinyMatrix<bool,NRows,NColumns> res;
273  for (int i = 0; i < NRows * NColumns; ++i)
274  res.data()[i] = (lhs.data()[i] < rhs.data()[i]);
275  return res;
276  }
277 
278 /*======================================================================*/
288 /*======================================================================*/
289  template<typename DataT, int NRows, int NColumns>
290  TinyMatrix<bool,NRows,NColumns> operator<=(
291  TinyMatrix<DataT,NRows,NColumns> const &lhs,
292  TinyMatrix<DataT,NRows,NColumns> const &rhs)
293  {
294  TinyMatrix<bool,NRows,NColumns> res;
295  for (int i = 0; i < NRows * NColumns; ++i)
296  res.data()[i] = (lhs.data()[i] <= rhs.data()[i]);
297  return res;
298  }
299 
300 /*======================================================================*/
310 /*======================================================================*/
311  template<typename DataT, int NRows, int NColumns>
312  TinyMatrix<bool,NRows,NColumns> operator>(
313  TinyMatrix<DataT,NRows,NColumns> const &lhs,
314  TinyMatrix<DataT,NRows,NColumns> const &rhs)
315  {
316  TinyMatrix<bool,NRows,NColumns> res;
317  for (int i = 0; i < NRows * NColumns; ++i)
318  res.data()[i] = (lhs.data()[i] > rhs.data()[i]);
319  return res;
320  }
321 
322 /*======================================================================*/
332 /*======================================================================*/
333  template<typename DataT, int NRows, int NColumns>
334  TinyMatrix<bool,NRows,NColumns> operator>=(
335  TinyMatrix<DataT,NRows,NColumns> const &lhs,
336  TinyMatrix<DataT,NRows,NColumns> const &rhs)
337  {
338  TinyMatrix<bool,NRows,NColumns> res;
339  for (int i = 0; i < NRows * NColumns; ++i)
340  res.data()[i] = (lhs.data()[i] >= rhs.data()[i]);
341  return res;
342  }
343 
344 /*======================================================================*/
354 /*======================================================================*/
355  template<int NRows, int NColumns>
356  bool all(blitz::TinyMatrix<bool,NRows,NColumns> const &matrix)
357  {
358  for (int i = 0; i < NRows * NColumns; ++i)
359  if (!matrix.data()[i]) return false;
360  return true;
361  }
362 
363 /*======================================================================*/
373 /*======================================================================*/
374  template<int NRows, int NColumns>
375  bool any(blitz::TinyMatrix<bool,NRows,NColumns> const &matrix)
376  {
377  for (int i = 0; i < NRows * NColumns; ++i)
378  if (matrix.data()[i]) return true;
379  return false;
380  }
381 
382 }
383 
384 #endif
TinyMatrix< bool, NRows, NColumns > operator==(TinyMatrix< DataT, NRows, NColumns > const &lhs, TinyMatrix< DataT, NRows, NColumns > const &rhs)
Elementwise comparison for equality of two blitz::TinyMatrices.
TinyMatrix< DataT, NRows, NColumns > operator+(TinyMatrix< DataT, NRows, NColumns > const &lhs, TinyMatrix< DataT, NRows, NColumns > const &rhs)
Addition of a blitz::TinyMatrix to another blitz::TinyMatrix.
TinyMatrix< bool, NRows, NColumns > operator!=(TinyMatrix< DataT, NRows, NColumns > const &lhs, TinyMatrix< DataT, NRows, NColumns > const &rhs)
Elementwise comparison for inequality of two blitz::TinyMatrices.
TinyMatrix< bool, NRows, NColumns > operator<(TinyMatrix< DataT, NRows, NColumns > const &lhs, TinyMatrix< DataT, NRows, NColumns > const &rhs)
Elementwise comparison of two blitz::TinyMatrices using the less than operator.
TinyVector< DataT, Dim2 > operator*(TinyMatrix< DataT, Dim2, Dim1 > const &m, TinyVector< DataT, Dim1 > const &v)
Product of a blitz::TinyMatrix and a blitz::TinyVector.
TinyMatrix< bool, NRows, NColumns > operator<=(TinyMatrix< DataT, NRows, NColumns > const &lhs, TinyMatrix< DataT, NRows, NColumns > const &rhs)
Elementwise comparison of two blitz::TinyMatrices using the less than or equals operator.
TinyMatrix< bool, NRows, NColumns > operator>(TinyMatrix< DataT, NRows, NColumns > const &lhs, TinyMatrix< DataT, NRows, NColumns > const &rhs)
Elementwise comparison of two blitz::TinyMatrices using the greater than operator.
TinyMatrix< DataT, NRows, NColumns > operator/(TinyMatrix< DataT, NRows, NColumns > const &m, DataT const &alpha)
Division of a blitz::TinyMatrix by a scalar.
bool any(blitz::TinyMatrix< bool, NRows, NColumns > const &matrix)
any() reduction for boolean blitz::TinyMatrix.
TinyMatrix< bool, NRows, NColumns > operator>=(TinyMatrix< DataT, NRows, NColumns > const &lhs, TinyMatrix< DataT, NRows, NColumns > const &rhs)
Elementwise comparison of two blitz::TinyMatrices using the greater than or equals operator...
TinyMatrix< DataT, NRows, NColumns > operator-(TinyMatrix< DataT, NRows, NColumns > const &m)
Negation of a blitz::TinyMatrix.
bool all(blitz::TinyMatrix< bool, NRows, NColumns > const &matrix)
all() reduction for boolean blitz::TinyMatrix.