Openholo  v5.0
Open Source Digital Holographic Library
mat.h
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install, copy or use the software.
7 //
8 //
9 // License Agreement
10 // For Open Source Digital Holographic Library
11 //
12 // Openholo library is free software;
13 // you can redistribute it and/or modify it under the terms of the BSD 2-Clause license.
14 //
15 // Copyright (C) 2017-2024, Korea Electronics Technology Institute. All rights reserved.
16 // E-mail : contact.openholo@gmail.com
17 // Web : http://www.openholo.org
18 //
19 // Redistribution and use in source and binary forms, with or without modification,
20 // are permitted provided that the following conditions are met:
21 //
22 // 1. Redistribution's of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
24 //
25 // 2. Redistribution's in binary form must reproduce the above copyright notice,
26 // this list of conditions and the following disclaimer in the documentation
27 // and/or other materials provided with the distribution.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the copyright holder or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 // This software contains opensource software released under GNU Generic Public License,
41 // NVDIA Software License Agreement, or CUDA supplement to Software License Agreement.
42 // Check whether software you use contains licensed software.
43 //
44 //M*/
45 
46 #ifndef __mat_h
47 #define __mat_h
48 
49 #include "ivec.h"
50 #include "typedef.h"
51 #include "complex.h"
52 #include "define.h"
53 
54 #include <vector>
55 
56 using namespace oph;
57 
58 namespace oph
59 {
60  template<typename T>
62  {
63  public:
64  using typeT = typename std::enable_if<
65  std::is_same<Real, T>::value || std::is_same<Real_t, T>::value ||
66  std::is_same<int, T>::value ||
67  std::is_same<uchar, T>::value ||
68  std::is_same<Complex<Real>, T>::value || std::is_same<Complex<Real_t>, T>::value, T>::type;
69 
70  std::vector<T>* mat;
72 
73  matrix(void) : size(1, 1) {
74  init();
75  }
76 
77  matrix(int x, int y) : size(x, y) {
78  init();
79  }
80 
81  matrix(ivec2 _size) : size(_size) {
82  init();
83  }
84 
85  matrix(const matrix<T>& ref) : size(ref.size) {
86  init();
87  for (int x = 0; x < size[_X]; x++)
88  for (int y = 0; y < size[_Y]; y++) {
89  mat[x][y] = ref.mat[x][y];
90  }
91  }
92 
93  ~matrix() {
94  release();
95  }
96 
97  void init(void) {
98  mat = new std::vector<T>[size[0]];
99  for (int x = 0; x < size[_X]; x++) {
100  for (int y = 0; y < size[_Y]; y++) {
101  if (x == y && size[_X] == size[_Y])
102  mat[x].push_back(1);
103  else
104  mat[x].push_back(0);
105  }
106  }
107  }
108 
109  void release(void) {
110  if (!mat) return;
111  delete[] mat;
112  mat = nullptr;
113  }
114 
115  oph::ivec2& getSize(void) { return size; }
116 
117  matrix<T>& resize(int x, int y) {
118  release();
119 
120  size[0] = x; size[1] = y;
121 
122  init();
123 
124  return *this;
125  }
126 
128  if (size[_X] != size[_Y]) return *this;
129  for (int x = 0; x < size[_X]; x++) {
130  for (int y = 0; y < size[_Y]; y++) {
131  if (x == y)
132  mat[x][y] = 1;
133  else
134  mat[x][y] = 0;
135  }
136  }
137  return *this;
138  }
139 
140  matrix<T>& zeros(void) {
141  for (int col = 0; col < size[_COL]; col++) {
142  for (int row = 0; row < size[_ROW]; row++) {
143  mat[col][row] = 0;
144  }
145  }
146  return *this;
147  }
148 
149  //T determinant(void) {
150  // if (size[_X] != size[_Y]) return 0;
151 
152  // return determinant(*this, size[_X]);
153  //}
154 
155  //T determinant(matrix<T>& _mat, int _size) {
156  // int p = 0, q = 0;
157  // T det = 0;
158 
159  // if (_size == 1) return _mat[0][0];
160  // else if (_size == 2) return _mat[0][0] * _mat[1][1] - _mat[0][1] * _mat[1][0];
161  // else {
162  // for (q = 0, det = 0; q<_size; q++) {
163  // det = det + _mat[0][q] * cofactor(_mat, 0, q, _size);
164  // }
165  // return det;
166  // }
167  // return 0;
168  //}
169 
170  //T cofactor(matrix<T>& _mat, int p, int q, int _size) {
171  // int i = 0, j = 0;
172  // int x = 0, y = 0;
173  // matrix<T> cmat(_size - 1, _size - 1);
174  // T cofactor = 0;
175 
176  // for (i = 0, x = 0; i<_size; i++) {
177  // if (i != p) {
178  // for (j = 0, y = 0; j<_size; j++) {
179  // if (j != q) {
180  // cmat[x][y] = _mat[i][j];
181  // y++;
182  // }
183  // }
184  // x++;
185  // }
186  // }
187 
188  // cofactor = pow(-1, p)*pow(-1, q)*determinant(cmat, _size - 1);
189  // return cofactor;
190  //}
191 
192  //void swapRow(int i, int j) {
193  // if (i == j) return;
194  // for (int k = 0; k < size[0]; k++) swap(mat[i][k], mat[j][k]);
195  //}
196 
197  //matrix<T>& inverse(void) {
198  // if (size[_X] != size[_Y]) return *this;
199  // if (determinant() == 0) return *this;
200 
201  // matrix<T> inv(size);
202  // inv.identity();
203 
204  // for (int k = 0; k < size[0]; k++) {
205  // int t = k - 1;
206 
207  // while (t + 1 < size[0] && !mat[++t][k]);
208  // if (t == size[0] - 1 && !mat[t][k]) return *this;
209  // swapRow(k, t), inv.swapRow(k, t);
210 
211  // T d = mat[k][k];
212  // for (int j = 0; j < size[0]; j++)
213  // mat[k][j] /= d, inv[k][j] /= d;
214 
215 
216  // for (int i = 0; i < size[0]; i++)
217  // if (i != k) {
218  // T m = mat[i][k];
219  // for (int j = 0; j < size[0]; j++) {
220  // if (j >= k) mat[i][j] -= mat[k][j] * m;
221  // inv[i][j] -= inv[k][j] * m;
222  // }
223  // }
224  // }
225 
226  // *this = inv;
227 
228  // return *this;
229  //}
230 
232  if (size != p.size) return *this;
233 
234  for (int x = 0; x < size[_X]; x++) {
235  for (int y = 0; y < size[_Y]; y++) {
236  mat[x][y] += p[x][y];
237  }
238  }
239 
240  return *this;
241  }
242 
244  if (size != p.size) return *this;
245 
246  for (int x = 0; x < size[_X]; x++) {
247  for (int y = 0; y < size[_Y]; y++) {
248  mat[x][y] -= p[x][y];
249  }
250  }
251 
252  return *this;
253  }
254 
256  if (size[_X] != p.size[_Y]) return *this;
257 
258  matrix<T> res(p.size[_Y], size[_X]);
259 
260  for (int x = 0; x < res.size[_X]; x++) {
261  for (int y = 0; y < res.size[_Y]; y++) {
262  res[x][y] = 0;
263  for (int num = 0; num < p.size[_X]; num++) {
264  res[x][y] += mat[x][num] * p[num][y];
265  }
266  }
267  }
268  this->resize(res.size[_X], res.size[_Y]);
269  *this = res;
270 
271  return *this;
272  }
273 
275  if (size != p.size) return *this;
276 
277  for (int x = 0; x < size[_X]; x++) {
278  for (int y = 0; y < size[_Y]; y++) {
279  mat[x][y] /= p[x][y];
280  }
281  }
282 
283  return *this;
284  }
285 
287  if (size != p.size) return *this;
288 
289  matrix<T> res(size);
290 
291  for (int x = 0; x < res.size[_X]; x++) {
292  for (int y = 0; y < res.size[_Y]; y++) {
293  res[x][y] = 0;
294  res[x][y] = mat[x][y] * p.mat[x][y];
295  }
296  }
297 
298  *this = res;
299 
300  return *this;
301  }
302 
303 
304  std::vector<T>& operator[](const int index) {
305  return mat[index];
306  }
307 
308  T& operator ()(int x, int y) {
309  return mat[x][y];
310  }
311 
312  inline void operator =(const matrix<T>& p) {
313  if (size != p.size)
314  return;
315 
316  for (int x = 0; x < size[_X]; x++) {
317  for (int y = 0; y < size[_Y]; y++) {
318  mat[x][y] = p.mat[x][y];
319  }
320  }
321  }
322 
323  inline void operator =(T* p) {
324  for (int x = 0; x < size[_X]; x++) {
325  for (int y = 0; y < size[_Y]; y++) {
326  mat[x][y] = *p;
327  p++;
328  }
329  }
330  }
331 
332  //matrix<T>& operator ()(T args...) {
333  // va_list ap;
334 
335  // __va_start(&ap, args);
336 
337  // for (int x = 0; x < size[_X]; x++) {
338  // for (int y = 0; y < size[_Y]; y++) {
339  // if (x == 0 && y == 0) {
340  // mat[x][y] = args;
341  // continue;
342  // }
343  // T n = __crt_va_arg(ap, T);
344  // mat[x][y] = n;
345  // }
346  // }
347  // __crt_va_end(ap);
348 
349  // return *this;
350  //}
351 
353  return add(p);
354  }
355 
357  return sub(p);
358  }
359 
361  return mul(p);
362  }
363 
365  return div(p);
366  }
367 
368  matrix<T>& operator +(const T& p) {
369  for (int x = 0; x < size[_X]; x++) {
370  for (int y = 0; y < size[_Y]; y++) {
371  mat[x][y] += p;
372  }
373  }
374  return *this;
375  }
376 
377  const matrix<T>& operator -(const T& p) {
378  for (int x = 0; x < size[_X]; x++) {
379  for (int y = 0; y < size[_Y]; y++) {
380  mat[x][y] -= p;
381  }
382  }
383  return *this;
384  }
385 
386  const matrix<T>& operator *(const T& p) {
387  for (int x = 0; x < size[_X]; x++) {
388  for (int y = 0; y < size[_Y]; y++) {
389  mat[x][y] *= p;
390  }
391  }
392  return *this;
393  }
394 
395  const matrix<T>& operator /(const T& p) {
396  for (int x = 0; x < size[_X]; x++) {
397  for (int y = 0; y < size[_Y]; y++) {
398  mat[x][y] /= p;
399  }
400  }
401  return *this;
402  }
403 
404  //print test
405  //void Print(const char* _context) {
406  // for (int x = 0; x < size[_X]; x++) {
407  // for (int y = 0; y < size[_Y]; y++) {
408  // printf(_context, mat[x][y]);
409  // }
410  // cout << endl;
411  // }
412  // cout << endl;
413  //}
414  };
415 
422 
425 }
426 
427 #endif // !__mat_h
oph::matrix< Real > OphRealField
Definition: mat.h:418
oph::ivec2 & getSize(void)
Definition: mat.h:115
OphComplexTField MatF
Definition: mat.h:424
matrix< T > & sub(matrix< T > &p)
Definition: mat.h:243
matrix< T > & add(matrix< T > &p)
Definition: mat.h:231
void init(void)
Definition: mat.h:97
matrix< T > & identity(void)
Definition: mat.h:127
matrix< T > & mulElem(matrix< T > &p)
Definition: mat.h:286
oph::matrix< Real_t > OphRealTField
Definition: mat.h:419
ivec2 operator-(const ivec2 &a, const ivec2 &b)
Definition: ivec.h:132
structure for 2-dimensional integer vector and its arithmetic.
Definition: ivec.h:66
matrix< T > & resize(int x, int y)
Definition: mat.h:117
#define _Y
Definition: define.h:96
oph::matrix< Complex< Real_t > > OphComplexTField
Definition: mat.h:421
ivec2 operator+(const ivec2 &a, const ivec2 &b)
Definition: ivec.h:109
void release(void)
Definition: mat.h:109
#define _X
Definition: define.h:92
ivec2 operator*(const ivec2 &a, const ivec2 &b)
Definition: ivec.h:155
matrix(void)
Definition: mat.h:73
#define OPH_DLL
Definition: Base.h:59
ivec2 size
Definition: mat.h:71
matrix(const matrix< T > &ref)
Definition: mat.h:85
OphComplexField Mat
Definition: mat.h:423
oph::matrix< Complex< Real > > OphComplexField
Definition: mat.h:420
~matrix()
Definition: mat.h:93
matrix< T > & zeros(void)
Definition: mat.h:140
#define _ROW
Definition: define.h:128
std::vector< T > * mat
Definition: mat.h:70
matrix(int x, int y)
Definition: mat.h:77
vec2 operator/(const vec2 &a, const vec2 &b)
Definition: vec.h:200
std::vector< T > & operator[](const int index)
Definition: mat.h:304
matrix< T > & mul(matrix< T > &p)
Definition: mat.h:255
oph::matrix< int > OphIntField
Definition: mat.h:416
typename std::enable_if< std::is_same< Real, T >::value||std::is_same< Real_t, T >::value||std::is_same< int, T >::value||std::is_same< uchar, T >::value||std::is_same< Complex< Real >, T >::value||std::is_same< Complex< Real_t >, T >::value, T >::type typeT
Definition: mat.h:68
oph::matrix< uchar > OphByteField
Definition: mat.h:417
#define _COL
Definition: define.h:124
matrix(ivec2 _size)
Definition: mat.h:81
Definition: Bitmap.h:49
matrix< T > & div(matrix< T > &p)
Definition: mat.h:274