Xmipp  v3.23.11-Nereus
matrix2d.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
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 2 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
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef CORE_MATRIX2D_H_
27 #define CORE_MATRIX2D_H_
28 
29 #include <complex>
30 #include "xmipp_random_mode.h"
31 #include "xmipp_macros.h"
32 #include "xmipp_error.h"
33 #include "xmipp_strings.h"
34 
35 class FileName;
36 
37 #ifdef XMIPP_MMAP
38 #include <sys/mman.h>
39 #endif
40 
41 template<typename T>
42 class Matrix1D;
43 
44 // Forward declarations
45 template<typename T>
46 class Matrix2D;
47 
48 template<typename T>
49 void ludcmp(const Matrix2D<T>& A, Matrix2D<T>& LU, Matrix1D< int >& indx, T& d);
50 
51 template<typename T>
52 void lubksb(const Matrix2D<T>& LU, Matrix1D< int >& indx, Matrix1D<T>& b);
53 
54 template<typename T>
55 void svdcmp(const Matrix2D< T >& a,
59 
65 
66 
71 void cholesky(const Matrix2D<double> &M, Matrix2D<double> &L);
72 
77 
84 
89 #define MATRIX2D_ARRAY(m) ((m).mdata)
90 
104 #define FOR_ALL_ELEMENTS_IN_MATRIX2D(m) \
105  for (size_t i=0; i<(m).mdimy; i++) \
106  for (size_t j=0; j<(m).mdimx; j++)
107 
116 #define MAT_ELEM(m,i,j) ((m).mdata[((size_t)i)*(m).mdimx+((size_t)j)])
117 
120 #define MAT_XSIZE(m) ((m).mdimx)
121 
124 #define MAT_YSIZE(m) ((m).mdimy)
125 
128 #define MAT_SIZE(m) ((m).mdim)
129 
139 #define dMij(m, i, j) MAT_ELEM(m, i, j)
140 
146 #define dMn(m, n) ((m).mdata[(n)])
147 
170 #define M3x3_BY_V3x1(a, M, b) { \
171  spduptmp0 = dMn(M, 0) * XX(b) + dMn(M, 1) * YY(b) + dMn(M, 2) * ZZ(b); \
172  spduptmp1 = dMn(M, 3) * XX(b) + dMn(M, 4) * YY(b) + dMn(M, 5) * ZZ(b); \
173  spduptmp2 = dMn(M, 6) * XX(b) + dMn(M, 7) * YY(b) + dMn(M, 8) * ZZ(b); \
174  XX(a) = spduptmp0; YY(a) = spduptmp1; ZZ(a) = spduptmp2; }
175 
182 #define M3x3_BY_M3x3(A, B, C) { \
183  spduptmp0 = dMn(B,0) * dMn(C,0) + dMn(B,1) * dMn(C,3) + dMn(B,2) * dMn(C,6); \
184  spduptmp1 = dMn(B,0) * dMn(C,1) + dMn(B,1) * dMn(C,4) + dMn(B,2) * dMn(C,7); \
185  spduptmp2 = dMn(B,0) * dMn(C,2) + dMn(B,1) * dMn(C,5) + dMn(B,2) * dMn(C,8); \
186  spduptmp3 = dMn(B,3) * dMn(C,0) + dMn(B,4) * dMn(C,3) + dMn(B,5) * dMn(C,6); \
187  spduptmp4 = dMn(B,3) * dMn(C,1) + dMn(B,4) * dMn(C,4) + dMn(B,5) * dMn(C,7); \
188  spduptmp5 = dMn(B,3) * dMn(C,2) + dMn(B,4) * dMn(C,5) + dMn(B,5) * dMn(C,8); \
189  spduptmp6 = dMn(B,6) * dMn(C,0) + dMn(B,7) * dMn(C,3) + dMn(B,8) * dMn(C,6); \
190  spduptmp7 = dMn(B,6) * dMn(C,1) + dMn(B,7) * dMn(C,4) + dMn(B,8) * dMn(C,7); \
191  spduptmp8 = dMn(B,6) * dMn(C,2) + dMn(B,7) * dMn(C,5) + dMn(B,8) * dMn(C,8); \
192  dMn(A, 0) = spduptmp0; \
193  dMn(A, 1) = spduptmp1; \
194  dMn(A, 2) = spduptmp2; \
195  dMn(A, 3) = spduptmp3; \
196  dMn(A, 4) = spduptmp4; \
197  dMn(A, 5) = spduptmp5; \
198  dMn(A, 6) = spduptmp6; \
199  dMn(A, 7) = spduptmp7; \
200  dMn(A, 8) = spduptmp8; }
201 
225 #define M2x2_BY_V2x1(a, M, b) { \
226  spduptmp0 = dMn(M, 0) * XX(b) + dMn(M, 1) * YY(b); \
227  spduptmp1 = dMn(M, 2) * XX(b) + dMn(M, 3) * YY(b); \
228  XX(a) = spduptmp0; \
229  YY(a) = spduptmp1; }
230 
237 #define M2x2_BY_CT(M2, M1, k) { \
238  dMn(M2, 0) = dMn(M1, 0) * k; \
239  dMn(M2, 1) = dMn(M1, 1) * k; \
240  dMn(M2, 2) = dMn(M1, 2) * k; \
241  dMn(M2, 3) = dMn(M1, 3) * k; }
242 
248 #define M3x3_BY_CT(M2, M1, k) { \
249  dMn(M2, 0) = dMn(M1, 0) * k; \
250  dMn(M2, 1) = dMn(M1, 1) * k; \
251  dMn(M2, 2) = dMn(M1, 2) * k; \
252  dMn(M2, 3) = dMn(M1, 3) * k; \
253  dMn(M2, 4) = dMn(M1, 4) * k; \
254  dMn(M2, 5) = dMn(M1, 5) * k; \
255  dMn(M2, 6) = dMn(M1, 6) * k; \
256  dMn(M2, 7) = dMn(M1, 7) * k; \
257  dMn(M2, 8) = dMn(M1, 8) * k; }
258 
263 #define M4x4_BY_CT(M2, M1, k) { \
264  dMn(M2, 0) = dMn(M1, 0) * k; \
265  dMn(M2, 1) = dMn(M1, 1) * k; \
266  dMn(M2, 2) = dMn(M1, 2) * k; \
267  dMn(M2, 3) = dMn(M1, 3) * k; \
268  dMn(M2, 4) = dMn(M1, 4) * k; \
269  dMn(M2, 5) = dMn(M1, 5) * k; \
270  dMn(M2, 6) = dMn(M1, 6) * k; \
271  dMn(M2, 7) = dMn(M1, 7) * k; \
272  dMn(M2, 8) = dMn(M1, 8) * k; \
273  dMn(M2, 9) = dMn(M1, 9) * k; \
274  dMn(M2,10) = dMn(M1,10) * k; \
275  dMn(M2,11) = dMn(M1,11) * k; \
276  dMn(M2,12) = dMn(M1,12) * k; \
277  dMn(M2,13) = dMn(M1,13) * k; \
278  dMn(M2,14) = dMn(M1,14) * k; \
279  dMn(M2,15) = dMn(M1,15) * k;}
280 
286 #define M2x2_INV(Ainv, A) { \
287  spduptmp0 = dMn(A,0) * dMn(A,3) - dMn(A,1) * dMn(A,2);\
288  if (spduptmp0==0.0) \
289  REPORT_ERROR(ERR_NUMERICAL,"2x2 matrix is not invertible"); \
290  spduptmp0 = 1.0 / spduptmp0; \
291  dMn(Ainv, 0) = dMn(A,3); \
292  dMn(Ainv, 1) = -dMn(A,1); \
293  dMn(Ainv, 2) = -dMn(A,2); \
294  dMn(Ainv, 3) = dMn(A,0); \
295  M2x2_BY_CT(Ainv, Ainv, spduptmp0); }
296 
302 #define M3x3_INV(Ainv, A) { \
303  dMn(Ainv, 0) = dMn(A,8)*dMn(A,4)-dMn(A,7)*dMn(A,5); \
304  dMn(Ainv, 1) = -(dMn(A,8)*dMn(A,1)-dMn(A,7)*dMn(A,2)); \
305  dMn(Ainv, 2) = dMn(A,5)*dMn(A,1)-dMn(A,4)*dMn(A,2); \
306  dMn(Ainv, 3) = -(dMn(A,8)*dMn(A,3)-dMn(A,6)*dMn(A,5)); \
307  dMn(Ainv, 4) = dMn(A,8)*dMn(A,0)-dMn(A,6)*dMn(A,2); \
308  dMn(Ainv, 5) = -(dMn(A,5)*dMn(A,0)-dMn(A,3)*dMn(A,2)); \
309  dMn(Ainv, 6) = dMn(A,7)*dMn(A,3)-dMn(A,6)*dMn(A,4); \
310  dMn(Ainv, 7) = -(dMn(A,7)*dMn(A,0)-dMn(A,6)*dMn(A,1)); \
311  dMn(Ainv, 8) = dMn(A,4)*dMn(A,0)-dMn(A,3)*dMn(A,1); \
312  spduptmp0 = dMn(A,0)*dMn(Ainv,0)+dMn(A,3)*dMn(Ainv,1)+dMn(A,6)*dMn(Ainv,2); \
313  if (spduptmp0==0.0) \
314  REPORT_ERROR(ERR_NUMERICAL,"3x3 matrix is not invertible"); \
315  spduptmp0 = 1.0 / spduptmp0; \
316  M3x3_BY_CT(Ainv, Ainv, spduptmp0); }
317 
323 #define M4x4_INV(Ainv, A) { \
324  dMn(Ainv, 0) = dMn(A,5)*(dMn(A,10)*dMn(A,15)-dMn(A,11)*dMn(A,14))+\
325  dMn(A,6)*(dMn(A,11)*dMn(A,13)-dMn(A,9) *dMn(A,15))+\
326  dMn(A,7)*(dMn(A,9) *dMn(A,14)-dMn(A,10)*dMn(A,13));\
327  dMn(Ainv, 1) = dMn(A,1)*(dMn(A,11)*dMn(A,14)-dMn(A,10)*dMn(A,15))+\
328  dMn(A,2)*(dMn(A,9) *dMn(A,15)-dMn(A,11)*dMn(A,13))+\
329  dMn(A,3)*(dMn(A,10)*dMn(A,13)-dMn(A,9) *dMn(A,14));\
330  dMn(Ainv, 2) = dMn(A,1)*(dMn(A,6) *dMn(A,15)-dMn(A,7) *dMn(A,14))+\
331  dMn(A,2)*(dMn(A,7) *dMn(A,13)-dMn(A,5) *dMn(A,15))+\
332  dMn(A,3)*(dMn(A,5) *dMn(A,14)-dMn(A,6) *dMn(A,13));\
333  dMn(Ainv, 3) = dMn(A,1)*(dMn(A,7) *dMn(A,10)-dMn(A,6) *dMn(A,11))+\
334  dMn(A,2)*(dMn(A,5) *dMn(A,11)-dMn(A,7) *dMn(A,9))+\
335  dMn(A,3)*(dMn(A,6) *dMn(A,9) -dMn(A,5) *dMn(A,10));\
336  dMn(Ainv, 4) = dMn(A,4)*(dMn(A,11)*dMn(A,14)-dMn(A,10)*dMn(A,15))+\
337  dMn(A,6)*(dMn(A,8) *dMn(A,15)-dMn(A,11)*dMn(A,12))+\
338  dMn(A,7)*(dMn(A,10)*dMn(A,12)-dMn(A,8) *dMn(A,14));\
339  dMn(Ainv, 5) = dMn(A,0)*(dMn(A,10)*dMn(A,15)-dMn(A,11)*dMn(A,14))+\
340  dMn(A,2)*(dMn(A,11)*dMn(A,12)-dMn(A,8) *dMn(A,15))+\
341  dMn(A,3)*(dMn(A,8) *dMn(A,14)-dMn(A,10)*dMn(A,12));\
342  dMn(Ainv, 6) = dMn(A,0)*(dMn(A,7) *dMn(A,14)-dMn(A,6) *dMn(A,15))+\
343  dMn(A,2)*(dMn(A,4) *dMn(A,15)-dMn(A,7) *dMn(A,12))+\
344  dMn(A,3)*(dMn(A,6) *dMn(A,12)-dMn(A,4) *dMn(A,14));\
345  dMn(Ainv, 7) = dMn(A,0)*(dMn(A,6) *dMn(A,11)-dMn(A,7) *dMn(A,10))+\
346  dMn(A,2)*(dMn(A,7) *dMn(A,8) -dMn(A,4) *dMn(A,11))+\
347  dMn(A,3)*(dMn(A,4) *dMn(A,10)-dMn(A,6) *dMn(A,8));\
348  dMn(Ainv, 8) = dMn(A,4)*(dMn(A,9) *dMn(A,15)-dMn(A,11)*dMn(A,13))+\
349  dMn(A,5)*(dMn(A,11)*dMn(A,12)-dMn(A,8) *dMn(A,15))+\
350  dMn(A,7)*(dMn(A,8) *dMn(A,13)-dMn(A,9) *dMn(A,12));\
351  dMn(Ainv, 9) = dMn(A,0)*(dMn(A,11)*dMn(A,13)-dMn(A,9) *dMn(A,15))+\
352  dMn(A,1)*(dMn(A,8) *dMn(A,15)-dMn(A,11)*dMn(A,12))+\
353  dMn(A,3)*(dMn(A,9) *dMn(A,12)-dMn(A,8) *dMn(A,13));\
354  dMn(Ainv,10) = dMn(A,0)*(dMn(A,5) *dMn(A,15)-dMn(A,7) *dMn(A,13))+\
355  dMn(A,1)*(dMn(A,7) *dMn(A,12)-dMn(A,4) *dMn(A,15))+\
356  dMn(A,3)*(dMn(A,4) *dMn(A,13)-dMn(A,5) *dMn(A,12));\
357  dMn(Ainv,11) = dMn(A,0)*(dMn(A,7) *dMn(A,9) -dMn(A,5) *dMn(A,11))+\
358  dMn(A,1)*(dMn(A,4) *dMn(A,11)-dMn(A,7) *dMn(A,8))+\
359  dMn(A,3)*(dMn(A,5) *dMn(A,8) -dMn(A,4) *dMn(A,9));\
360  dMn(Ainv,12) = dMn(A,4)*(dMn(A,10)*dMn(A,13)-dMn(A,9) *dMn(A,14))+\
361  dMn(A,5)*(dMn(A,8) *dMn(A,14)-dMn(A,10)*dMn(A,12))+\
362  dMn(A,6)*(dMn(A,9) *dMn(A,12)-dMn(A,8) *dMn(A,13));\
363  dMn(Ainv,13) = dMn(A,0)*(dMn(A,9) *dMn(A,14)-dMn(A,10)*dMn(A,13))+\
364  dMn(A,1)*(dMn(A,10)*dMn(A,12)-dMn(A,8) *dMn(A,14))+\
365  dMn(A,2)*(dMn(A,8) *dMn(A,13)-dMn(A,9) *dMn(A,12));\
366  dMn(Ainv,14) = dMn(A,0)*(dMn(A,6) *dMn(A,13)-dMn(A,5) *dMn(A,14))+\
367  dMn(A,1)*(dMn(A,4) *dMn(A,14)-dMn(A,6) *dMn(A,12))+\
368  dMn(A,2)*(dMn(A,5) *dMn(A,12)-dMn(A,4) *dMn(A,13));\
369  dMn(Ainv,15) = dMn(A,0)*(dMn(A,5) *dMn(A,10)-dMn(A,6) *dMn(A,9))+\
370  dMn(A,1)*(dMn(A,6) *dMn(A,8) -dMn(A,4) *dMn(A,10))+\
371  dMn(A,2)*(dMn(A,4) *dMn(A,9) -dMn(A,5) *dMn(A,8));\
372  spduptmp0 = dMn(A,0)*(dMn(A,5)*(dMn(A,10)*dMn(A,15)-dMn(A,11)*dMn(A,14))\
373  +dMn(A,6)*(dMn(A,11)*dMn(A,13)-dMn(A,9) *dMn(A,15))\
374  +dMn(A,7)*(dMn(A,9) *dMn(A,14)-dMn(A,10)*dMn(A,13)))\
375  +dMn(A,1)*(dMn(A,4)*(dMn(A,11)*dMn(A,14)-dMn(A,10)*dMn(A,15))\
376  +dMn(A,6)*(dMn(A,8) *dMn(A,15)-dMn(A,11)*dMn(A,12))\
377  +dMn(A,7)*(dMn(A,10)*dMn(A,12)-dMn(A,8) *dMn(A,14)))\
378  +dMn(A,2)*(dMn(A,4)*(dMn(A,9) *dMn(A,15)-dMn(A,11)*dMn(A,13))\
379  +dMn(A,5)*(dMn(A,11)*dMn(A,12)-dMn(A,8) *dMn(A,15))\
380  +dMn(A,7)*(dMn(A,8) *dMn(A,13)-dMn(A,9) *dMn(A,12)))\
381  +dMn(A,3)*(dMn(A,4)*(dMn(A,10)*dMn(A,13)-dMn(A,9) *dMn(A,14))\
382  +dMn(A,5)*(dMn(A,8) *dMn(A,14)-dMn(A,10)*dMn(A,12))\
383  +dMn(A,6)*(dMn(A,9) *dMn(A,12)-dMn(A,8) *dMn(A,13))); \
384  if (spduptmp0==0.0) \
385  REPORT_ERROR(ERR_NUMERICAL,"4x4 matrix is not invertible"); \
386  spduptmp0 = 1.0 / spduptmp0; \
387  M4x4_BY_CT(Ainv, Ainv, spduptmp0); }
388 
390 template<typename T>
391 class Matrix2D
392 {
393 public:
394  // The array itself
395  T* mdata;
396 
397  // Destroy data
399 
400  // Mapped data
402 
403  // File descriptor for mapped files
404  int fdMap;
405 
406  // Mapped data original pointer
408 
409  // Number of elements in X
410  size_t mdimx;
411 
412  // Number of elements in Y
413  size_t mdimy;
414 
415  // Total number of elements
416  size_t mdim;
418 
420 
421 
424  {
425  coreInit();
426  }
427 
428  Matrix2D(const FileName &fnMappedMatrix, int Ydim, int Xdim, size_t offset=0)
429  {
430  coreInit(fnMappedMatrix,Ydim,Xdim,offset);
431  }
432 
435  Matrix2D(int Ydim, int Xdim)
436  {
437  coreInit();
438  initZeros(Ydim, Xdim);
439  }
440 
444  {
445  coreInit();
446  *this = v;
447  }
448 
452  {
453  coreDeallocate();
454  }
455 
466  Matrix2D<T>& operator=(const Matrix2D<T>& op1);
468 
470 
471 
473  void clear()
474  {
475  coreDeallocate();
476  coreInit();
477  }
478 
480  inline void convertTo(float out[3][3]) const {
481  for (int i = 0; i < 3; i++) {
482  for (int j = 0; j < 3; j++) {
483  out[i][j] = (*this)(i, j);
484  }
485  }
486  }
487 
490  void coreInit(const FileName &fn, int Ydim, int Xdim, size_t offset=0);
491 
495  void coreInit();
496 
499  void coreAllocate( int _mdimy, int _mdimx);
500 
504  void coreDeallocate();
506 
508 
509 
511  void resize(size_t Ydim, size_t Xdim, bool noCopy=false);
512 
525  template<typename T1>
526  void resize(const Matrix2D<T1> &v)
527  {
528  if (mdimx != v.mdimx || mdimy != v.mdimy)
529  resize(v.mdimy, v.mdimx);
530  }
531 
534  inline void resizeNoCopy(int Ydim, int Xdim)
535  {
536  resize(Ydim, Xdim, true);
537  }
538 
542  template<typename T1>
543  inline void resizeNoCopy(const Matrix2D<T1> &v)
544  {
545  if (mdimx != v.mdimx || mdimy != v.mdimy)
546  resize(v.mdimy, v.mdimx, true);
547  }
548 
554  void mapToFile(const FileName &fn, int Ydim, int Xdim, size_t offset=0);
555 
558  void submatrix(int i0, int j0, int iF, int jF);
559 
565  template <typename T1>
566  inline bool sameShape(const Matrix2D<T1>& op) const
567  {
568  return ((mdimx == op.mdimx) && (mdimy == op.mdimy));
569  }
570 
575  inline size_t Xdim() const
576  {
577  return mdimx;
578  }
579 
584  inline size_t Ydim() const
585  {
586  return mdimy;
587  }
589 
591 
592 
602  inline void initConstant(T val)
603  {
604  for (size_t j = 0; j < mdim; j++)
605  mdata[j] = val;
606  }
607 
610  inline void initConstant(size_t Ydim, size_t Xdim, T val)
611  {
612  if (mdimx!=Xdim || mdimy!=Ydim)
613  resizeNoCopy(Ydim, Xdim);
614  initConstant(val);
615  }
616 
626  inline void initZeros()
627  {
628  memset(mdata,0,mdimx*mdimy*sizeof(T));
629  }
630 
633  inline void initZeros(size_t Ydim, size_t Xdim)
634  {
635  if (mdimx!=Xdim || mdimy!=Ydim)
636  resizeNoCopy(Ydim, Xdim);
637  memset(mdata,0,mdimx*mdimy*sizeof(T));
638  }
639 
649  template <typename T1>
650  void initZeros(const Matrix2D<T1>& op)
651  {
652  if (mdimx!=op.mdimx || mdimy!=op.mdimy)
653  resizeNoCopy(op);
654  memset(mdata,0,mdimx*mdimy*sizeof(T));
655  }
656 
659  void initRandom(size_t Ydim, size_t Xdim, double op1, double op2, RandomMode mode = RND_UNIFORM);
660 
662  void initGaussian(int Ydim, int Xdim, double op1=0., double op2=1.);
663 
673  inline void initIdentity()
674  {
675  initIdentity(MAT_XSIZE(*this));
676  }
677 
686  inline void initIdentity(int dim)
687  {
688  initZeros(dim, dim);
689  for (int i = 0; i < dim; i++)
690  MAT_ELEM(*this,i,i) = 1;
691  }
700  void initGaussian(int dim, double var);
702 
704 
705 
708  inline T& operator()(int i, int j) const
709  {
710  return MAT_ELEM((*this),i,j);
711  }
714  inline void setVal(T val,int y, int x)
715  {
716  MAT_ELEM((*this),y,x)=val;
717  }
720  inline T getVal( int y, int x) const
721  {
722  return MAT_ELEM((*this),y,x);
723  }
724 
727  inline Matrix2D<T> operator*(T op1) const
728  {
729  Matrix2D<T> tmp(*this);
730  for (size_t i=0; i < mdim; i++)
731  tmp.mdata[i] = mdata[i] * op1;
732  return tmp;
733  }
734 
737  inline Matrix2D<T> operator/(T op1) const
738  {
739  Matrix2D<T> tmp(*this);
740  for (size_t i=0; i < mdim; i++)
741  tmp.mdata[i] = mdata[i] / op1;
742  return tmp;
743  }
744 
747  inline friend Matrix2D<T> operator*(T op1, const Matrix2D<T>& op2)
748  {
749  Matrix2D<T> tmp(op2);
750  for (size_t i=0; i < op2.mdim; i++)
751  tmp.mdata[i] = op1 * op2.mdata[i];
752  return tmp;
753  }
754 
757  inline void operator*=(T op1)
758  {
759  for (size_t i=0; i < mdim; i++)
760  mdata[i] *= op1;
761  }
762 
765  inline void operator/=(T op1)
766  {
767  for (size_t i=0; i < mdim; i++)
768  mdata[i] /= op1;
769  }
770 
777  Matrix1D<T> operator*(const Matrix1D<T>& op1) const;
778 
785  Matrix2D<T> operator*(const Matrix2D<T>& op1) const;
786 
793  Matrix2D<T> operator+(const Matrix2D<T>& op1) const;
794 
801  void operator+=(const Matrix2D<T>& op1) const;
802 
809  Matrix2D<T> operator-(const Matrix2D<T>& op1) const;
810 
817  void operator-=(const Matrix2D<T>& op1) const;
823  bool equal(const Matrix2D<T>& op,
824  double accuracy = XMIPP_EQUAL_ACCURACY) const;
830  bool equalAbs(const Matrix2D<T>& op,
831  double accuracy = XMIPP_EQUAL_ACCURACY) const;
833 
835 
836 
840  T computeMax() const;
841 
846  T computeMin() const;
847 
849  void computeMaxAndMin(T &maxValue, T &minValue) const;
850 
852  void rowSum(Matrix1D<T> &sum) const;
853 
855  void colSum(Matrix1D<T> &sum) const;
856 
859  void rowEnergySum(Matrix1D<T> &sum) const;
860 
867  T** adaptForNumericalRecipes() const;
868 
875  inline T* adaptForNumericalRecipes2() const
876  {
877  return mdata - 1 - mdimx;
878  }
879 
882  void loadFromNumericalRecipes(T** m, int Ydim, int Xdim);
883 
888  inline void killAdaptationForNumericalRecipes(T** m) const
889  {
890  free_Tmatrix(m, 1, mdimy, 1, mdimx);
891  }
892 
898  {}
899 
903  void read(const FileName &fn);
904 
907  void write(const FileName &fn) const;
910  friend std::ostream& operator<<(std::ostream& ostrm, const Matrix2D<T>& v)
911  {
912  if (v.Xdim() == 0 || v.Ydim() == 0)
913  ostrm << "NULL matrix\n";
914  else
915  {
916  ostrm << std::endl;
917  double max_val = v.computeMax();
918  int prec = bestPrecision(max_val, 10);
919 
920  for (size_t i = 0; i < v.Ydim(); i++)
921  {
922  for (size_t j = 0; j < v.Xdim(); j++)
923  {
924  ostrm << floatToString((double) v(i, j), 10, prec) << ' ';
925  }
926  ostrm << std::endl;
927  }
928  }
929 
930  return ostrm;
931  }
932 
943  void fromVector(const Matrix1D<T>& op1);
944 
956  void toVector(Matrix1D<T>& op1) const;
957 
960  void copyToVector(std::vector<T> &v)
961  {
962  v.assign(mdata, mdata+mdim);
963  }
966  inline void copyFromVector(std::vector<T> &v,int Xdim, int Ydim)
967  {
968  if (mdimx!=Xdim || mdimy!=Ydim)
969  resizeNoCopy(Ydim, Xdim);
970  copy( v.begin(), v.begin()+v.size(), mdata);
971  }
972 
984  void getRow(size_t i, Matrix1D<T>& v) const;
985 
996  void getCol(size_t j, Matrix1D<T>& v) const;
997 
1006  void setRow(size_t i, const Matrix1D<T>& v);
1007 
1017  void setCol(size_t j, const Matrix1D<T>& v);
1018 
1020  void computeRowMeans(Matrix1D<double> &Xmr) const;
1021 
1023  void computeColMeans(Matrix1D<double> &Xmr) const;
1024 
1028  inline void setConstantCol(size_t j, T v)
1029  {
1030  if (mdimx == 0 || mdimy == 0)
1031  REPORT_ERROR(ERR_MATRIX_EMPTY, "setCol: Target matrix is empty");
1032 
1033  if (j>= mdimx)
1034  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, "setCol: Matrix subscript (j) out of range");
1035 
1036  for (size_t i = 0; i < mdimy; i++)
1037  MAT_ELEM(*this,i, j) = v;
1038  }
1039 
1043  void getDiagonal(Matrix1D<T> &d) const;
1044 
1048  T trace() const;
1049 
1058  T det() const;
1059 
1061  inline T det3x3() const
1062  {
1063  return (
1064  dMij(*this,0,0)*( dMij(*this,2,2)*dMij(*this,1,1)-
1065  dMij(*this,2,1)*dMij(*this,1,2) )-
1066  dMij(*this,1,0)*( dMij(*this,2,2)*dMij(*this,0,1)-
1067  dMij(*this,2,1)*dMij(*this,0,2) )+
1068  dMij(*this,2,0)*( dMij(*this,1,2)*dMij(*this,0,1)-
1069  dMij(*this,1,1)*dMij(*this,0,2) )
1070  );
1071  }
1072 
1074  inline double norm()
1075  {
1076  double sum=0.;
1078  {
1079  T aux=MAT_ELEM(*this,i,j);
1080  sum+=aux*aux;
1081  }
1082  return sqrt(sum);
1083  }
1084 
1094  Matrix2D<T> transpose() const;
1095 
1106  void inv(Matrix2D<T>& result) const;
1107 
1118  void invAlgLib(Matrix2D<T>& result, bool use_lu=false) const;
1119 
1125  {
1126  svdcmp(*this, U, W, V);
1127  }
1128 
1132  void eigs(Matrix2D<double> &U, Matrix1D<double> &W, Matrix2D<double> &V, Matrix1D<int> &indexes) const;
1133 
1137  {
1138  Matrix2D<T> result;
1139  inv(result);
1140 
1141  return result;
1142  }
1143 
1147  {
1148  Matrix2D<T> auxMatrix(*this);
1149  auxMatrix.inv(*this);
1150  }
1151 
1159  bool isIdentity() const;
1161 };
1162 
1165 
1166 template<typename T>
1167 bool operator==(const Matrix2D<T>& op1, const Matrix2D<T>& op2)
1168 {
1169  return op1.equal(op2);
1170 }
1171 
1178 template<typename T>
1179 void ludcmp(const Matrix2D<T>& A, Matrix2D<T>& LU, Matrix1D< int >& indx, T& d);
1180 
1181 
1184 template<typename T>
1185 void lubksb(const Matrix2D<T>& LU, Matrix1D< int >& indx, Matrix1D<T>& b);
1186 
1191  Matrix2D< double >& v,
1192  Matrix1D< double >& b,
1194 
1195 //#define VIA_NR
1196 #define VIA_BILIB
1197 
1199 template<typename T>
1200 void svdcmp(const Matrix2D< T >& a,
1201  Matrix2D< double >& u,
1202  Matrix1D< double >& w,
1203  Matrix2D< double >& v);
1204 
1210 
1215 void firstEigs(const Matrix2D<double> &A, size_t M, Matrix1D<double> &D, Matrix2D<double> &P, bool Pneeded=true);
1216 
1221 void lastEigs(const Matrix2D<double> &A, size_t M, Matrix1D<double> &D, Matrix2D<double> &P);
1222 
1227 void eigsBetween(const Matrix2D<double> &A, size_t I1, size_t I2, Matrix1D<double> &D, Matrix2D<double> &P);
1228 
1230 void allEigs(const Matrix2D<double> &A, std::vector< std::complex<double> > &eigs);
1231 
1237 
1244 template<typename T1, typename T2>
1246 {
1247  if (v1.mdim == 0)
1248  {
1249  v2.clear();
1250  return;
1251  }
1252 
1253  if (v1.mdimx!=v2.mdimx || v1.mdimy!=v2.mdimy)
1254  v2.resizeNoCopy(v1);
1255  for (unsigned long int n = 0; n < v1.mdim; n++)
1256  v2.mdata[n] = static_cast< T2 > (v1.mdata[n]);
1257 }
1258 
1262 template<typename T1>
1264 {
1265  v2=v1;
1266 }
1267 
1270 
1275 
1280 
1285 
1288 
1291 
1294 
1297 
1300 
1303 
1306 
1309 
1313 
1316 
1319 
1322 
1324 void keepColumns(Matrix2D<double> &A, int j0, int jF);
1325 
1327 
1328 
1329 #endif /* MATRIX2D_H_ */
T getVal(int y, int x) const
Definition: matrix2d.h:720
Matrix2D(const Matrix2D< T > &v)
Definition: matrix2d.h:443
Matrix2D< double > DMatrix
Definition: matrix2d.h:1163
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
Index out of bounds.
Definition: xmipp_error.h:132
void resizeNoCopy(const Matrix2D< T1 > &v)
Definition: matrix2d.h:543
size_t Xdim() const
Definition: matrix2d.h:575
void initConstant(size_t Ydim, size_t Xdim, T val)
Definition: matrix2d.h:610
void eraseFirstColumn(Matrix2D< double > &A)
Definition: matrix2d.cpp:542
bool isIdentity() const
Definition: matrix2d.cpp:1323
void submatrix(int i0, int j0, int iF, int jF)
Definition: matrix2d.cpp:1095
void killAdaptationForNumericalRecipes2(T **m) const
Definition: matrix2d.h:897
void rowSum(Matrix1D< T > &sum) const
Definition: matrix2d.cpp:779
double norm()
Definition: matrix2d.h:1074
void coreInit()
Definition: matrix2d.cpp:971
Matrix2D< T > & operator=(const Matrix2D< T > &op1)
Definition: matrix2d.cpp:957
void subtractColumnMeans(Matrix2D< double > &A)
Definition: matrix2d.cpp:230
void eigsBetween(const Matrix2D< double > &A, size_t I1, size_t I2, Matrix1D< double > &D, Matrix2D< double > &P)
Definition: matrix2d.cpp:324
void setConstantCol(size_t j, T v)
Definition: matrix2d.h:1028
void getDiagonal(Matrix1D< T > &d) const
Definition: matrix2d.cpp:949
void normalizeColumns(Matrix2D< double > &A)
Definition: matrix2d.cpp:188
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void fromVector(const Matrix1D< T > &op1)
Definition: matrix2d.cpp:803
void colSum(Matrix1D< T > &sum) const
Definition: matrix2d.cpp:787
bool destroyData
Definition: matrix2d.h:398
bool sameShape(const Matrix2D< T1 > &op) const
Definition: matrix2d.h:566
void mapToFile(const FileName &fn, int Ydim, int Xdim, size_t offset=0)
Definition: matrix2d.cpp:1079
void initConstant(T val)
Definition: matrix2d.h:602
void matrixOperation_AB(const Matrix2D< double > &A, const Matrix2D< double > &B, Matrix2D< double > &C)
Definition: matrix2d.cpp:411
bool equal(const Matrix2D< T > &op, double accuracy=XMIPP_EQUAL_ACCURACY) const
Definition: matrix2d.cpp:1202
void killAdaptationForNumericalRecipes(T **m) const
Definition: matrix2d.h:888
Matrix2D< T > operator/(T op1) const
Definition: matrix2d.h:737
T & operator()(int i, int j) const
Definition: matrix2d.h:708
void sqrt(Image< double > &op)
void convertTo(float out[3][3]) const
Definition: matrix2d.h:480
static double * y
void computeMaxAndMin(T &maxValue, T &minValue) const
Definition: matrix2d.cpp:1262
Matrix2D(const FileName &fnMappedMatrix, int Ydim, int Xdim, size_t offset=0)
Definition: matrix2d.h:428
T trace() const
Definition: matrix2d.cpp:1304
The matrix is empty.
Definition: xmipp_error.h:151
char * mdataOriginal
Definition: matrix2d.h:407
void operator+=(const Matrix2D< T > &op1) const
Definition: matrix2d.cpp:1165
void toVector(Matrix1D< T > &op1) const
Definition: matrix2d.cpp:832
Matrix2D(int Ydim, int Xdim)
Definition: matrix2d.h:435
void inv(Matrix2D< T > &result) const
Definition: matrix2d.cpp:663
doublereal * w
int bestPrecision(float F, int _width)
T * mdata
Definition: matrix2d.h:395
RandomMode
Matrix2D< T > transpose() const
Definition: matrix2d.cpp:1314
void matrixOperation_IminusA(Matrix2D< double > &A)
Definition: matrix2d.cpp:533
String floatToString(float F, int _width, int _prec)
T * adaptForNumericalRecipes2() const
Definition: matrix2d.h:875
void free_Tmatrix(T **&m, int nrl, int nrh, int ncl, int nch)
Definition: xmipp_memory.h:61
Matrix2D< T > operator+(const Matrix2D< T > &op1) const
Definition: matrix2d.cpp:1150
void invAlgLib(Matrix2D< T > &result, bool use_lu=false) const
void matrixOperation_AtA(const Matrix2D< double > &A, Matrix2D< double > &B)
Definition: matrix2d.cpp:436
void typeCast(const Matrix2D< T1 > &v1, Matrix2D< T2 > &v2)
Definition: matrix2d.h:1245
doublereal * x
#define i
doublereal * d
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
void operator/=(T op1)
Definition: matrix2d.h:765
void matrixOperation_AAt(const Matrix2D< double > &A, Matrix2D< double > &C)
Definition: matrix2d.cpp:449
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void connectedComponentsOfUndirectedGraph(const Matrix2D< double > &G, Matrix1D< int > &component)
Definition: matrix2d.cpp:360
Definition: mask.h:36
void clear()
Definition: matrix2d.h:473
void normalizeColumnsBetween0and1(Matrix2D< double > &A)
Definition: matrix2d.cpp:221
void orthogonalizeColumnsGramSchmidt(Matrix2D< double > &M)
Definition: matrix2d.cpp:560
doublereal * b
void lubksb(const Matrix2D< T > &LU, Matrix1D< int > &indx, Matrix1D< T > &b)
Definition: matrix2d.cpp:596
Matrix2D< T > inv() const
Definition: matrix2d.h:1136
double v1
int fdMap
Definition: matrix2d.h:404
friend Matrix2D< T > operator*(T op1, const Matrix2D< T > &op2)
Definition: matrix2d.h:747
Matrix2D< T > operator*(T op1) const
Definition: matrix2d.h:727
void matrixOperation_AtB(const Matrix2D< double > &A, const Matrix2D< double > &B, Matrix2D< double > &C)
Definition: matrix2d.cpp:475
#define dMij(m, i, j)
Definition: matrix2d.h:139
Matrix2D< int > IMatrix
Definition: matrix2d.h:1164
void coreAllocate(int _mdimy, int _mdimx)
Definition: matrix2d.cpp:982
#define XMIPP_EQUAL_ACCURACY
Definition: xmipp_macros.h:119
void setCol(size_t j, const Matrix1D< T > &v)
Definition: matrix2d.cpp:929
void eigs(Matrix2D< double > &U, Matrix1D< double > &W, Matrix2D< double > &V, Matrix1D< int > &indexes) const
Definition: matrix2d.cpp:719
void rowEnergySum(Matrix1D< T > &sum) const
Definition: matrix2d.cpp:795
void initIdentity(int dim)
Definition: matrix2d.h:686
T det() const
Definition: matrix2d.cpp:38
void firstEigs(const Matrix2D< double > &A, size_t M, Matrix1D< double > &D, Matrix2D< double > &P, bool Pneeded=true)
Definition: matrix2d.cpp:284
void matrixOperation_IplusA(Matrix2D< double > &A)
Definition: matrix2d.cpp:527
void generalizedEigs(const Matrix2D< double > &A, const Matrix2D< double > &B, Matrix1D< double > &D, Matrix2D< double > &P)
Definition: matrix2d.cpp:267
bool mappedData
Definition: matrix2d.h:401
size_t Ydim() const
Definition: matrix2d.h:584
void matrixOperation_ABt(const Matrix2D< double > &A, const Matrix2D< double > &B, Matrix2D< double > &C)
Definition: matrix2d.cpp:462
void matrixOperation_Ax(const Matrix2D< double > &A, const Matrix1D< double > &x, Matrix1D< double > &y)
Definition: matrix2d.cpp:424
void mode
size_t mdimy
Definition: matrix2d.h:413
void coreDeallocate()
Definition: matrix2d.cpp:1002
void loadFromNumericalRecipes(T **m, int Ydim, int Xdim)
Definition: matrix2d.cpp:1293
void lastEigs(const Matrix2D< double > &A, size_t M, Matrix1D< double > &D, Matrix2D< double > &P)
Definition: matrix2d.cpp:305
T det3x3() const
determinat of 3x3 matrix
Definition: matrix2d.h:1061
void allEigs(const Matrix2D< double > &A, std::vector< std::complex< double > > &eigs)
Definition: matrix2d.cpp:345
void schur(const Matrix2D< double > &M, Matrix2D< double > &O, Matrix2D< double > &T)
Definition: matrix2d.cpp:251
void svdcmp(const Matrix2D< T > &a, Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v)
Definition: matrix2d.cpp:125
void initZeros(size_t Ydim, size_t Xdim)
Definition: matrix2d.h:633
void matrixOperation_Atx(const Matrix2D< double > &A, const Matrix1D< double > &x, Matrix1D< double > &y)
Definition: matrix2d.cpp:488
void copyToVector(std::vector< T > &v)
Definition: matrix2d.h:960
#define j
void setVal(T val, int y, int x)
Definition: matrix2d.h:714
void getCol(size_t j, Matrix1D< T > &v) const
Definition: matrix2d.cpp:890
int m
void ludcmp(const Matrix2D< T > &A, Matrix2D< T > &LU, Matrix1D< int > &indx, T &d)
Definition: matrix2d.cpp:586
void svd(Matrix2D< double > &U, Matrix1D< double > &W, Matrix2D< double > &V) const
Definition: matrix2d.h:1124
void resize(const Matrix2D< T1 > &v)
Definition: matrix2d.h:526
Matrix2D()
Definition: matrix2d.h:423
void setRow(size_t i, const Matrix1D< T > &v)
Definition: matrix2d.cpp:910
void operator*=(T op1)
Definition: matrix2d.h:757
Definition: ctf.h:38
Matrix2D< T > operator-(const Matrix2D< T > &op1) const
Definition: matrix2d.cpp:1176
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
void read(const FileName &fn)
Definition: matrix2d.cpp:101
void keepColumns(Matrix2D< double > &A, int j0, int jF)
Definition: matrix2d.cpp:551
void write(const FileName &fn) const
Definition: matrix2d.cpp:113
void initRandom(size_t Ydim, size_t Xdim, double op1, double op2, RandomMode mode=RND_UNIFORM)
Definition: matrix2d.cpp:1108
void initZeros(const Matrix2D< T1 > &op)
Definition: matrix2d.h:650
size_t mdim
Definition: matrix2d.h:416
T ** adaptForNumericalRecipes() const
Definition: matrix2d.cpp:1280
void initZeros()
Definition: matrix2d.h:626
void copyFromVector(std::vector< T > &v, int Xdim, int Ydim)
Definition: matrix2d.h:966
void computeRowMeans(Matrix1D< double > &Xmr) const
Definition: matrix2d.cpp:604
bool equalAbs(const Matrix2D< T > &op, double accuracy=XMIPP_EQUAL_ACCURACY) const
Definition: matrix2d.cpp:1219
doublereal * u
void operator-=(const Matrix2D< T > &op1) const
Definition: matrix2d.cpp:1191
size_t mdimx
Definition: matrix2d.h:410
T computeMax() const
Definition: matrix2d.cpp:1236
void initGaussian(int Ydim, int Xdim, double op1=0., double op2=1.)
Definition: matrix2d.cpp:1118
void getRow(size_t i, Matrix1D< T > &v) const
Definition: matrix2d.cpp:871
void matrixOperation_XtAX_symmetric(const Matrix2D< double > &X, const Matrix2D< double > &A, Matrix2D< double > &B)
Definition: matrix2d.cpp:513
void matrixOperation_AtBt(const Matrix2D< double > &A, const Matrix2D< double > &B, Matrix2D< double > &C)
Definition: matrix2d.cpp:500
int * n
void resize(size_t Ydim, size_t Xdim, bool noCopy=false)
Definition: matrix2d.cpp:1022
void cholesky(const Matrix2D< double > &M, Matrix2D< double > &L)
Definition: matrix2d.cpp:160
doublereal * a
void selfInverse()
Definition: matrix2d.h:1146
~Matrix2D()
Definition: matrix2d.h:451
void initIdentity()
Definition: matrix2d.h:673
void svbksb(Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v, Matrix1D< double > &b, Matrix1D< double > &x)
Definition: matrix2d.cpp:174
bool operator==(const Matrix2D< T > &op1, const Matrix2D< T > &op2)
Definition: matrix2d.h:1167
T computeMin() const
Definition: matrix2d.cpp:1249
void computeColMeans(Matrix1D< double > &Xmr) const
Definition: matrix2d.cpp:613