Openholo  v1.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>
61  class _declspec(dllexport) matrix
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;
71  ivec2 size;
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 
127  matrix<T>& identity(void) {
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 
231  matrix<T>& add(matrix<T>& p) {
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 
243  matrix<T>& sub(matrix<T>& p) {
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 
255  matrix<T>& mul(matrix<T>& p) {
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 
274  matrix<T>& div(matrix<T>& p) {
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  if (p[x][y] == 0) continue;
280  mat[x][y] /= p[x][y];
281  }
282  }
283 
284  return *this;
285  }
286 
287  matrix<T>& mulElem(matrix<T>& p) {
288  if (size != p.size) return *this;
289 
290  matrix<T> res(size);
291 
292  for (int x = 0; x < res.size[_X]; x++) {
293  for (int y = 0; y < res.size[_Y]; y++) {
294  res[x][y] = 0;
295  res[x][y] = mat[x][y] * p.mat[x][y];
296  }
297  }
298 
299  *this = res;
300 
301  return *this;
302  }
303 
304 
305  std::vector<T>& operator[](const int index) {
306  return mat[index];
307  }
308 
309  T& operator ()(int x, int y) {
310  return mat[x][y];
311  }
312 
313  inline void operator =(matrix<T>& p) {
314  if (size != p.size)
315  return;
316 
317  for (int x = 0; x < size[_X]; x++) {
318  for (int y = 0; y < size[_Y]; y++) {
319  mat[x][y] = p[x][y];
320  }
321  }
322  }
323 
324  inline void operator =(T* p) {
325  for (int x = 0; x < size[_X]; x++) {
326  for (int y = 0; y < size[_y]; y++) {
327  mat[x][y] = *p;
328  p++;
329  }
330  }
331  }
332 
333  //matrix<T>& operator ()(T args...) {
334  // va_list ap;
335 
336  // __va_start(&ap, args);
337 
338  // for (int x = 0; x < size[_X]; x++) {
339  // for (int y = 0; y < size[_Y]; y++) {
340  // if (x == 0 && y == 0) {
341  // mat[x][y] = args;
342  // continue;
343  // }
344  // T n = __crt_va_arg(ap, T);
345  // mat[x][y] = n;
346  // }
347  // }
348  // __crt_va_end(ap);
349 
350  // return *this;
351  //}
352 
353  matrix<T>& operator +(matrix<T>& p) {
354  return add(p);
355  }
356 
357  matrix<T>& operator -(matrix<T>& p) {
358  return sub(p);
359  }
360 
361  matrix<T>& operator *(matrix<T>& p) {
362  return mul(p);
363  }
364 
365  matrix<T>& operator /(matrix<T>& p) {
366  return div(p);
367  }
368 
369  matrix<T>& operator +(const T& p) {
370  for (int x = 0; x < size[_X]; x++) {
371  for (int y = 0; y < size[_Y]; y++) {
372  mat[x][y] += p;
373  }
374  }
375  return *this;
376  }
377 
378  const matrix<T>& operator -(const T& p) {
379  for (int x = 0; x < size[_X]; x++) {
380  for (int y = 0; y < size[_Y]; y++) {
381  mat[x][y] -= p;
382  }
383  }
384  return *this;
385  }
386 
387  const matrix<T>& operator *(const T& p) {
388  for (int x = 0; x < size[_X]; x++) {
389  for (int y = 0; y < size[_Y]; y++) {
390  mat[x][y] *= p;
391  }
392  }
393  return *this;
394  }
395 
396  const matrix<T>& operator /(const T& p) {
397  for (int x = 0; x < size[_X]; x++) {
398  for (int y = 0; y < size[_Y]; y++) {
399  mat[x][y] /= p;
400  }
401  }
402  return *this;
403  }
404 
405  //print test
406  //void Print(const char* _context) {
407  // for (int x = 0; x < size[_X]; x++) {
408  // for (int y = 0; y < size[_Y]; y++) {
409  // printf(_context, mat[x][y]);
410  // }
411  // cout << endl;
412  // }
413  // cout << endl;
414  //}
415  };
416 
417  typedef oph::matrix<int> OphIntField;
418  typedef oph::matrix<uchar> OphByteField;
419  typedef oph::matrix<Real> OphRealField;
420  typedef oph::matrix<Real_t> OphRealTField;
421  typedef oph::matrix<Complex<Real>> OphComplexField;
422  typedef oph::matrix<Complex<Real_t>> OphComplexTField;
423 
426 }
427 
428 #endif // !__mat_h
oph::matrix< Real > OphRealField
Definition: mat.h:419
OphComplexTField MatF
Definition: mat.h:425
oph::matrix< Real_t > OphRealTField
Definition: mat.h:420
ivec2 operator-(const ivec2 &a, const ivec2 &b)
Definition: ivec.h:122
#define _Y
Definition: define.h:84
oph::matrix< Complex< Real_t > > OphComplexTField
Definition: mat.h:422
ivec2 operator+(const ivec2 &a, const ivec2 &b)
Definition: ivec.h:99
#define _X
Definition: define.h:80
ivec2 operator*(const ivec2 &a, const ivec2 &b)
Definition: ivec.h:145
int num
Definition: ophTriMesh.cpp:603
OphComplexField Mat
Definition: mat.h:424
oph::matrix< Complex< Real > > OphComplexField
Definition: mat.h:421
#define _ROW
Definition: define.h:112
vec2 operator/(const vec2 &a, const vec2 &b)
Definition: vec.h:200
oph::matrix< int > OphIntField
Definition: mat.h:415
oph::matrix< uchar > OphByteField
Definition: mat.h:418
#define _COL
Definition: define.h:108
Definition: Bitmap.h:49