MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlMatrixTemplate.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2012, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_MATRIX_TEMPLATE_H
14#define ML_MATRIX_TEMPLATE_H
15
16
22
23#include <mlInitSystemML.h>
25
26#ifdef UNIX
27#include "mlDebug.h"
28#endif
29
30#include <ThirdPartyWarningsDisable.h>
31#include <iostream>
32#include <valarray>
33#include <numeric>
34#include <iterator>
35#include <ThirdPartyWarningsRestore.h>
36
37//======================================================================
38// namespaces
39//======================================================================
41//======================================================================
42// declaration of class *Slice_iter*
43//======================================================================
48template <class T> class Slice_iter
49{
50 std::valarray<T>* v;
51 std::slice s;
52 size_t curr;
53
55 T& ref(size_t i) const
56 {
57 return (*v)[s.start() + i * s.stride()];
58 }
59
60public:
62 Slice_iter(std::valarray<T>* vv, std::slice ss)
63 : v(vv)
64 , s(ss)
65 , curr(0)
66 {}
67
70 {
71 Slice_iter<T> t = *this;
72 t.curr = s.size(); // index of one plus last element
73 return t;
74 }
75
79 {
80 curr++;
81 return *this;
82 }
84 {
85 Slice_iter<T> t = *this;
86 curr++;
87 return t;
88 }
90
93 T& operator[](size_t i)
94 {
95 return ref(i);
96 }
97 T& operator()(size_t i)
98 {
99 return ref(i);
100 }
102 {
103 return ref(curr);
104 }
106
109 // caution: friend function has to be implemented here inplace to support
110 // explicit template specification according to ansi standart in gcc, i.e to
111 // let the compiler know, that the friend functions are realy templated overloads
112 // (note: the gcc conform syntax (friend bool operator== <> (...)) is not supported
113 // on other compiler)
115 friend bool operator==(const Slice_iter<T>& p, const Slice_iter<T>& q)
116 {
117 return p.curr == q.curr && p.s.stride() == q.s.stride() && p.s.start() == q.s.start();
118 }
119
121 friend bool operator!=(const Slice_iter<T>& p, const Slice_iter<T>& q)
122 {
123 return !(p == q);
124 }
125
127 friend bool operator<(const Slice_iter<T>& p, const Slice_iter<T>& q)
128 {
129 return p.curr < q.curr && p.s.stride() == q.s.stride() && p.s.start() == q.s.start();
130 }
132};
133
134
135//======================================================================
136// declaration of class *Cslice_iter*
137//======================================================================
142template <class T> class Cslice_iter
143{
144 std::valarray<T>* v;
145 std::slice s;
146 size_t curr;
147
149 const T& ref(size_t i) const
150 {
151 return (*v)[s.start() + i * s.stride()];
152 }
153
154public:
156 Cslice_iter(std::valarray<T>* vv, std::slice ss)
157 : v(vv)
158 , s(ss)
159 , curr(0)
160 {}
161
164 {
165 Cslice_iter<T> t = *this;
166 t.curr = s.size(); // index of one plus last element
167 return t;
168 }
169
173 {
174 curr++;
175 return *this;
176 }
178 {
179 Cslice_iter<T> t = *this;
180 curr++;
181 return t;
182 }
184
187 const T& operator[](size_t i) const
188 {
189 return ref(i);
190 }
191 const T& operator()(size_t i) const
192 {
193 return ref(i);
194 }
195 const T& operator*() const
196 {
197 return ref(curr);
198 }
200
203 // caution: friend function has to be implemented here inplace to support
204 // explicit template specification according to ansi standart in gcc, i.e to
205 // let the compiler know, that the friend functions are realy templated overloads
206 // (note: the gcc conform syntax (friend bool operator== <> (...)) is not supported
207 // on other compiler)
209 friend bool operator==(const Cslice_iter<T>& p, const Cslice_iter<T>& q)
210 {
211 return p.curr == q.curr && p.s.stride() == q.s.stride() && p.s.start() == q.s.start();
212 }
213
215 friend bool operator!=(const Cslice_iter<T>& p, const Cslice_iter<T>& q)
216 {
217 return !(p == q);
218 }
219
221 friend bool operator<(const Cslice_iter<T>& p, const Cslice_iter<T>& q)
222 {
223 return p.curr < q.curr && p.s.stride() == q.s.stride() && p.s.start() == q.s.start();
224 }
226};
227
228//======================================================================
229// declaration of class *MatrixTemplate*
230//======================================================================
234template <class T> class MatrixTemplate
235{
236 std::valarray<T>* v;
237 size_t size_x;
238 size_t size_y;
239public:
240 MatrixTemplate(size_t x, size_t y);
244
245 void freeMatrix();
246 void resizeMatrix(size_t x, size_t y, const T& c = T());
247 void overrideMatrix(size_t x, size_t y,
248 const T& c = T());
249
250 size_t size() const
251 {
252 return size_x * size_y;
253 }
254 size_t sizeX() const
255 {
256 return size_x;
257 }
258 size_t sizeY() const
259 {
260 return size_y;
261 }
262
264 Cslice_iter<T> row(size_t i) const;
265
267 Cslice_iter<T> column(size_t i) const;
268
271 T& operator()(size_t x, size_t y);
272 T operator()(size_t x, size_t y) const;
273
275 {
276 return column(i);
277 }
279 {
280 return column(i);
281 }
283
287 {
288 return column(i);
289 }
291 {
292 return column(i);
293 }
295
297
298 std::valarray<T>& array()
299 {
300 return *v;
301 }
302
303 operator T*()
304 {
305 return reinterpret_cast<T*>(&v);
306 }
307};
308
309
310//======================================================================
311// declaration of class *MatrixSizedTemplate*
312//======================================================================
318template <class T, size_t SIZE_X, size_t SIZE_Y> class MatrixSizedTemplate : public MatrixTemplate<T>
319{
320public:
324 {
325 ;
326 }
327};
328
329
330//=======================================================================
331// Implementation of MatrixTemplate - functions
332//=======================================================================
333
335template <class T> inline Slice_iter<T> MatrixTemplate<T>::row(size_t i)
336{
337 return Slice_iter<T>(v, std::slice(i, size_x, size_y));
338}
339
341template <class T> inline Cslice_iter<T> MatrixTemplate<T>::row(size_t i) const
342{
343 return Cslice_iter<T>(v, std::slice(i, size_x, size_y));
344}
345
347template <class T> inline Slice_iter<T> MatrixTemplate<T>::column(size_t i)
348{
349 return Slice_iter<T>(v, std::slice(i * size_y, size_y, 1));
350}
351
353template <class T> inline Cslice_iter<T> MatrixTemplate<T>::column(size_t i) const
354{
355 return Cslice_iter<T>(v, std::slice(i * size_y, size_y, 1));
356}
357
359template <class T> MatrixTemplate<T>::MatrixTemplate(size_t x, size_t y)
360{
361 size_x = x;
362 size_y = y;
363 v = new std::valarray<T>(size_x * size_y);
364}
365
368{
369 // set size according to transfered object
370 size_x = mt.size_x;
371 size_y = mt.size_y;
372 // assign new valarray of appropriate size
373 v = new std::valarray<T>(size_x * size_y);
374
375 // copy memory content
376 *v = *(mt.v);
377}
378
381{
382 if (this != &mt) {
383 // resize matrixTemplate
384 this->resizeMatrix(mt.size_x, mt.size_y);
385 // copy memory content
386 *v = *(mt.v);
387 }
388 return *this;
389}
390
393{
394 delete v;
395}
396
400template <class T> void MatrixTemplate<T>::resizeMatrix(size_t x, size_t y, const T& c)
401{
402 size_x = x;
403 size_y = y;
404 v->resize(size_x * size_y, c);
405}
406
410template <class T> void MatrixTemplate<T>::overrideMatrix(size_t x, size_t y, const T& c)
411{
412 size_x = x;
413 size_y = y;
414 *v = c;
415 v->resize(size_x * size_y, c);
416}
417
418
420template <class T> void MatrixTemplate<T>::freeMatrix()
421{
422 size_x = 0;
423 size_y = 0;
424 v->free();
425}
426
428template <class T> T& MatrixTemplate<T>::operator()(size_t x, size_t y)
429{
430 return column(x)[y];
431}
432
433
434//---------------------------------------------------------------------------------
436//---------------------------------------------------------------------------------
439template <class T> T mul(const Cslice_iter<T>& v1, const std::valarray<T>& v2)
440{
441 T res = 0;
442 for (size_t i = 0; i < v2.size(); i++)
443 res += v1[i] * v2[i];
444 return res;
445}
446
447
449template <class T> std::valarray<T> operator*(const MatrixTemplate<T>& m, const std::valarray<T>& v)
450{
451 if (m.sizeX() != v.size()) {
452 printTemplateError("mlMatrixTemplate.h, operator*() # 1", ML_BAD_PARAMETER,
453 "Wrong number of elements in m*v\n, ignoring problem");
454 }
455
456 std::valarray<T> res(m.sizeY());
457 for (size_t i = 0; i < m.sizeY(); i++)
458 res[i] = mul(m.row(i), v);
459 return res;
460}
461
462
464// std::valarray<T> operator*(const MatrixTemplate<T>& m, std::valarray<T>& v)
465template <class T> std::valarray<T> mul_mv(const MatrixTemplate<T>& m, std::valarray<T>& v)
466{
467 if (m.sizeX() != v.size()) {
468 printTemplateError("mlMatrixTemplate.h, mul_mv()", ML_BAD_PARAMETER,
469 "Wrong number of elements in m*v\n, ignoring problem");
470 }
471
472 std::valarray<T> res(m.sizeY());
473
474 for (size_t i = 0; i < m.sizeY(); i++) {
475 const Cslice_iter<T>& ri = m.row(i);
476 res[i] = inner_product(ri, ri.end(), &v[0], T(0));
477 }
478 return res;
479}
480
481
483template <class T> std::valarray<T> operator*(std::valarray<T>& v, const MatrixTemplate<T>& m)
484{
485 if (v.size() != m.sizeY()) {
486 printTemplateError("mlMatrixTemplate.h, operator*() # 2", ML_BAD_PARAMETER,
487 "Wrong number of elements in v*m\n, ignoring problem");
488 }
489
490 std::valarray<T> res(m.sizeX());
491
492 for (size_t i = 0; i < m.sizeX(); i++) {
493 const Cslice_iter<T>& ci = m.column(i);
494 res[i] = inner_product(ci, ci.end(), &v[0], T(0));
495 }
496 return res;
497}
498
501{
502 (*v) *= d;
503 return *this;
504}
505
507template <class T> std::ostream& operator<<(std::ostream& os, MatrixTemplate<T>& m)
508{
509 for (int y = 0; y < m.sizeY(); y++) {
510 for (int x = 0; x < m.sizeX(); x++)
511 os << m[x][y] << "\t";
512 os << "\n";
513 }
514 return os;
515}
516
518
519
520} // namespace ml
521
522#endif //__MatrixTemplate_h__
@ T
Templated support class for class MatrixTemplate to allow fast element access organisation by slicing...
friend bool operator==(const Cslice_iter< T > &p, const Cslice_iter< T > &q)
overload operator==(.,.) for Cslice_iter objects
const T & operator()(size_t i) const
Fortran-style subscript.
Cslice_iter< T > & operator++()
standard itterator incrementation
const T & operator*() const
current element
const T & operator[](size_t i) const
C style subscript.
friend bool operator!=(const Cslice_iter< T > &p, const Cslice_iter< T > &q)
overload operator!=(.,.) for Cslice_iter objects
Cslice_iter< T > end() const
returns itterator positioned on last-plus-one element
friend bool operator<(const Cslice_iter< T > &p, const Cslice_iter< T > &q)
overload operator<(.,.) for Cslice_iter objects
Cslice_iter(std::valarray< T > *vv, std::slice ss)
constructor: initialising slice-itterator for val-array
Cslice_iter< T > operator++(int)
standard itterator incrementation
general Matrix class based on MatrixTemplate<T> to allow fixed size setting with object declaration.
MatrixSizedTemplate()
ctor: initialise size at object creation with desired value
MatricTemplate implements general Matrix class based on valarray of objects with Template class type ...
Slice_iter< T > operator[](size_t i)
slice of column(i)
Cslice_iter< T > operator[](size_t i) const
const slice of column(i)
size_t sizeX() const
return number of columns
void overrideMatrix(size_t x, size_t y, const T &c=T())
resize matrix if necessary, set all elements on value c
Cslice_iter< T > row(size_t i) const
const itterator access to i-te row slice
Cslice_iter< T > operator()(size_t i) const
const slice of first argument (i.e column(i))
T & operator()(size_t x, size_t y)
reference of element x,y
void freeMatrix()
free allocated memory
size_t sizeY() const
return number of rows
void resizeMatrix(size_t x, size_t y, const T &c=T())
resize matrix, set new allocated elements to value c
MatrixTemplate(size_t x, size_t y)
constructor initialising memory block for requested matrix size
Cslice_iter< T > column(size_t i) const
const itterator access to i-te column slice
Slice_iter< T > column(size_t i)
itterator access to i-te column slice
MatrixTemplate< T > & operator=(const MatrixTemplate< T > &)
assignment operator ATTENTION: implies resizing of matrix
size_t size() const
return total number of matrix elements
~MatrixTemplate()
destructor: free allocated memory
T operator()(size_t x, size_t y) const
element x,y
Slice_iter< T > operator()(size_t i)
slice of first argument (i.e column(i))
Slice_iter< T > row(size_t i)
itterator access to i-te row slice
MatrixTemplate(const MatrixTemplate< T > &)
copy constructor
std::valarray< T > & array()
reference to internal valarray
MatrixTemplate< T > & operator*=(T)
multiplication of all matrix elements with element-type multiplicator
Templated support class for class MatrixTemplate to allow fast element access organisation by slicing...
friend bool operator!=(const Slice_iter< T > &p, const Slice_iter< T > &q)
overload operator!=(.,.) for Slice_iter objects
Slice_iter< T > & operator++()
standard itterator incrementation
Slice_iter(std::valarray< T > *vv, std::slice ss)
constructor: initialising slice-itterator for val-array
friend bool operator<(const Slice_iter< T > &p, const Slice_iter< T > &q)
overload operator< (.,.) for Slice_iter objects
friend bool operator==(const Slice_iter< T > &p, const Slice_iter< T > &q)
overload operator==(.,.) for Slice_iter objects
T & operator[](size_t i)
C style subscript.
Slice_iter< T > end() const
returns itterator positioned on last-plus-one element
T & operator()(size_t i)
Fortran-style subscript.
Slice_iter< T > operator++(int)
standard itterator incrementation
T & operator*()
current element
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm,...
Definition mlTypeDefs.h:879
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
std::ostream & operator<<(std::ostream &out, const ml::Variant &variant)
Definition mlVariant.h:231
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
T mul(const Cslice_iter< T > &v1, const std::valarray< T > &v2)
implementation of (global) operator functions to assist MatrixTemplate class
std::valarray< T > mul_mv(const MatrixTemplate< T > &m, std::valarray< T > &v)
alternative definition of m*v