Xmipp  v3.23.11-Nereus
matrix2d.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Sjors H.W. Scheres (scheres@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 #include <queue>
27 #include <fstream>
28 #include "alglib/linalg.h"
29 #include "numerical_recipes.h"
30 #include "matrix2d.h"
31 #include "bilib/linearalgebra.h"
32 #include "xmipp_filename.h"
33 #include "matrix1d.h"
34 #include "xmipp_funcs.h"
35 #include <sys/stat.h>
36 
37 template<typename T>
39 {
40  // (see Numerical Recipes, Chapter 2 Section 5)
41  if (mdimx == 0 || mdimy == 0)
42  REPORT_ERROR(ERR_MATRIX_EMPTY, "determinant: Matrix is empty");
43 
44  if (mdimx != mdimy)
45  REPORT_ERROR(ERR_MATRIX_SIZE, "determinant: Matrix is not squared");
46 
47  for (size_t i = 0; i < mdimy; i++)
48  {
49  bool all_zeros = true;
50  for (size_t j = 0; j < mdimx; j++)
51  if (fabs(MAT_ELEM((*this),i, j)) > XMIPP_EQUAL_ACCURACY)
52  {
53  all_zeros = false;
54  break;
55  }
56 
57  if (all_zeros)
58  return 0;
59  }
60 
61  // Perform decomposition
62  Matrix1D< int > indx;
63  T d;
64  Matrix2D<T> LU;
65  ludcmp(*this, LU, indx, d);
66 
67  // Calculate determinant
68  for (size_t i = 0; i < mdimx; i++)
69  d *= (T) MAT_ELEM(LU,i , i);
70 
71  return d;
72 }
73 
74 template<typename T>
75 void Matrix2D<T>::coreInit(const FileName &fn, int Ydim, int Xdim, size_t offset)
76 {
77 #ifdef XMIPP_MMAP
78 
79  mdimx=Xdim;
80  mdimy=Ydim;
81  mdim=mdimx*mdimy;
82  destroyData=false;
83  mappedData=true;
84  fdMap = open(fn.c_str(), O_RDWR, S_IREAD | S_IWRITE);
85  if (fdMap == -1)
87  const size_t pagesize=sysconf(_SC_PAGESIZE);
88  size_t offsetPages=(offset/pagesize)*pagesize;
89  size_t offsetDiff=offset-offsetPages;
90  if ( (mdataOriginal = (char*) mmap(0,Ydim*Xdim*sizeof(T)+offsetDiff, PROT_READ | PROT_WRITE, MAP_SHARED, fdMap, offsetPages)) == MAP_FAILED )
91  REPORT_ERROR(ERR_MMAP_NOTADDR,(String)"mmap failed "+integerToString(errno));
92  mdata=(T*)(mdataOriginal+offsetDiff);
93 #else
94 
95  REPORT_ERROR(ERR_MMAP,"Mapping not supported in Windows");
96 #endif
97 
98 }
99 
100 template<typename T>
102 {
103  std::ifstream fhIn;
104  fhIn.open(fn.c_str());
105  if (!fhIn)
108  fhIn >> MAT_ELEM(*this,i,j);
109  fhIn.close();
110 }
111 
112 template<typename T>
113 void Matrix2D<T>::write(const FileName &fn) const
114 {
115  std::ofstream fhOut;
116  fhOut.open(fn.c_str());
117  if (!fhOut)
118  REPORT_ERROR(ERR_IO_NOTOPEN,(std::string)"write: Cannot open "+fn+" for output");
119  fhOut << *this;
120  fhOut.close();
121 }
122 
123 #define VIA_BILIB
124 template<typename T>
125 void svdcmp(const Matrix2D< T >& a,
129 {
130  // svdcmp only works with double
131  typeCast(a, u);
132 
133  // Set size of matrices
134  w.initZeros(u.mdimx);
135  v.initZeros(u.mdimx, u.mdimx);
136 
137  // Call to the numerical recipes routine
138 #ifdef VIA_NR
139 
140  svdcmp(u.mdata,
141  u.mdimy, u.mdimx,
142  w.vdata,
143  v.mdata);
144 #endif
145 
146 #ifdef VIA_BILIB
147 
148  int status;
150  u.mdimy, u.mdimx,
151  w.vdata,
152  v.mdata,
153  5000, &status);
154 #endif
155 }
156 #undef VIA_NR
157 #undef VIA_BILIB
158 
159 /* Cholesky decomposition -------------------------------------------------- */
161 {
162  L=M;
164  p.initZeros(MAT_XSIZE(M));
167  if (i==j)
168  MAT_ELEM(L,i,j)=VEC_ELEM(p,i);
169  else if (i<j)
170  MAT_ELEM(L,i,j)=0.0;
171 }
172 
173 /* Interface to numerical recipes: svbksb ---------------------------------- */
176 {
177  // Call to the numerical recipes routine. Results will be stored in X
181  u.mdimy, u.mdimx,
184 }
185 
186 
187 
189 {
190  if (MAT_YSIZE(A)<=1)
191  return;
192 
193  // Compute the mean and standard deviation of each column
194  Matrix1D<double> avg, stddev;
195  avg.initZeros(MAT_XSIZE(A));
196  stddev.initZeros(MAT_XSIZE(A));
197 
199  {
200  double x=MAT_ELEM(A,i,j);
201  VEC_ELEM(avg,j)+=x;
202  VEC_ELEM(stddev,j)+=x*x;
203  }
204 
205  double iN=1.0/MAT_YSIZE(A);
207  {
208  VEC_ELEM(avg,i)*=iN;
209  VEC_ELEM(stddev,i)=sqrt(fabs(VEC_ELEM(stddev,i)*iN - VEC_ELEM(avg,i)*VEC_ELEM(avg,i)));
210  if (VEC_ELEM(stddev,i)>XMIPP_EQUAL_ACCURACY)
211  VEC_ELEM(stddev,i)=1.0/VEC_ELEM(stddev,i);
212  else
213  VEC_ELEM(stddev,i)=0.0;
214  }
215 
216  // Now normalize
218  MAT_ELEM(A,i,j)=(MAT_ELEM(A,i,j)-VEC_ELEM(avg,j))*VEC_ELEM(stddev,j);
219 }
220 
222 {
223  double maxValue,minValue;
224  A.computeMaxAndMin(maxValue,minValue);
225  double iMaxValue=1.0/(maxValue-minValue);
227  MAT_ELEM(A,i,j)=(MAT_ELEM(A,i,j)-minValue)*iMaxValue;
228 }
229 
231 {
232  if (MAT_YSIZE(A)<1)
233  return;
234 
235  // Compute the mean and standard deviation of each column
236  Matrix1D<double> avg;
237  avg.initZeros(MAT_XSIZE(A));
238 
240  VEC_ELEM(avg,j)+=MAT_ELEM(A,i,j);
241 
242  double iN=1.0/MAT_YSIZE(A);
244  VEC_ELEM(avg,i)*=iN;
245 
246  // Now normalize
248  MAT_ELEM(A,i,j)=MAT_ELEM(A,i,j)-VEC_ELEM(avg,j);
249 }
250 
252 {
255  bool ok=rmatrixschur(a, MAT_YSIZE(M), s);
256  if (!ok)
257  REPORT_ERROR(ERR_NUMERICAL,"Could not perform Schur decomposition");
258  O.resizeNoCopy(M);
259  T.resizeNoCopy(M);
261  {
262  MAT_ELEM(O,i,j)=s(i,j);
263  MAT_ELEM(T,i,j)=a(i,j);
264  }
265 }
266 
268 {
269  int N=(int)MAT_YSIZE(A);
271  a.setcontent(N,N,MATRIX2D_ARRAY(A));
272  b.setcontent(N,N,MATRIX2D_ARRAY(B));
274  bool ok=smatrixgevd(a, N, true, b, true, true, 1, d, z);
275  if (!ok)
276  REPORT_ERROR(ERR_NUMERICAL,"Could not perform eigenvector decomposition");
277  D.resizeNoCopy(N);
278  memcpy(&VEC_ELEM(D,0),d.getcontent(),N*sizeof(double));
279  P.resizeNoCopy(A);
281  MAT_ELEM(P,i,j)=z(i,j);
282 }
283 
284 void firstEigs(const Matrix2D<double> &A, size_t M, Matrix1D<double> &D, Matrix2D<double> &P, bool Pneeded)
285 {
286  int N=(int)MAT_YSIZE(A);
288  a.setcontent(N,N,MATRIX2D_ARRAY(A));
290  bool ok=smatrixevdi(a, N, Pneeded, false, N-M, N-1, d, z);
291  if (!ok)
292  REPORT_ERROR(ERR_NUMERICAL,"Could not perform eigenvector decomposition");
293 
294  D.resizeNoCopy(M);
296  VEC_ELEM(D,i)=d(M-1-i);
297  if (Pneeded)
298  {
299  P.resizeNoCopy(N,M);
301  MAT_ELEM(P,i,j)=z(i,M-1-j);
302  }
303 }
304 
306 {
307  int N=(int)MAT_YSIZE(A);
309  a.setcontent(N,N,MATRIX2D_ARRAY(A));
311  bool ok=smatrixevdi(a, N, true, false, 0, M-1, d, z);
312  if (!ok)
313  REPORT_ERROR(ERR_NUMERICAL,"Could not perform eigenvector decomposition");
314 
315  D.resizeNoCopy(M);
317  VEC_ELEM(D,i)=d(M-1-i);
318  memcpy(&VEC_ELEM(D,0),d.getcontent(),M*sizeof(double));
319  P.resizeNoCopy(N,M);
321  MAT_ELEM(P,i,j)=z(i,j);
322 }
323 
324 void eigsBetween(const Matrix2D<double> &A, size_t I1, size_t I2, Matrix1D<double> &D, Matrix2D<double> &P)
325 {
326  size_t M = I2 - I1 + 1;
327  int N=(int)MAT_YSIZE(A);
329  a.setcontent(N,N,MATRIX2D_ARRAY(A));
331 
332  bool ok=smatrixevdi(a, N, true, false, I1, I2, d, z);
333  if (!ok)
334  REPORT_ERROR(ERR_NUMERICAL,"Could not perform eigenvector decomposition");
335 
336  D.resizeNoCopy(M);
338  VEC_ELEM(D,i)=d(M-1-i);
339  memcpy(&VEC_ELEM(D,0),d.getcontent(),M*sizeof(double));
340  P.resizeNoCopy(N,M);
342  MAT_ELEM(P,i,j)=z(i,j);
343 }
344 
345 void allEigs(const Matrix2D<double> &A, std::vector< std::complex<double> > &eigs)
346 {
347  int N=(int)MAT_YSIZE(A);
348  alglib::real_2d_array a, vl, vr;
349  a.setcontent(N,N,MATRIX2D_ARRAY(A));
350  alglib::real_1d_array wr, wi;
351 
352  bool ok=rmatrixevd(a, N, 0, wr, wi, vl, vr);
353  if (!ok)
354  REPORT_ERROR(ERR_NUMERICAL,"Could not perform eigenvector decomposition");
355  eigs.clear();
356  for (int n=0; n<N; ++n)
357  eigs.push_back(std::complex<double>(wr(n),wi(n)));
358 }
359 
361 {
362  size_t N=MAT_XSIZE(G);
363  component.resizeNoCopy(N);
364  component.initConstant(-1);
365 
366  int nextComponent=0;
367  bool workDone=false;
368  std::queue<size_t> toExplore;
369  do
370  {
371  workDone=false;
372  // Find next unvisited element
373  bool found=false;
374  size_t seed=0;
376  if (VEC_ELEM(component,i)<0)
377  {
378  seed=i;
379  found=true;
380  break;
381  }
382 
383  // If found, get its connected component
384  if (found)
385  {
386  int currentComponent=nextComponent;
387  nextComponent++;
388 
389  VEC_ELEM(component,seed)=currentComponent;
390  toExplore.push(seed);
391  while (toExplore.size()>0)
392  {
393  seed=toExplore.front();
394  toExplore.pop();
395  for (size_t j=seed+1; j<N; ++j)
396  if (MAT_ELEM(G,seed,j)>0)
397  {
398  if (VEC_ELEM(component,j)<0)
399  {
400  VEC_ELEM(component,j)=currentComponent;
401  toExplore.push(j);
402  }
403 
404  }
405  }
406  workDone=true;
407  }
408  } while (workDone);
409 }
410 
412 {
413  C.initZeros(MAT_YSIZE(A), MAT_XSIZE(B));
414  for (size_t i = 0; i < MAT_YSIZE(A); ++i)
415  for (size_t j = 0; j < MAT_XSIZE(B); ++j)
416  {
417  double aux=0.;
418  for (size_t k = 0; k < MAT_XSIZE(A); ++k)
419  aux += MAT_ELEM(A, i, k) * MAT_ELEM(B, k, j);
420  MAT_ELEM(C, i, j)=aux;
421  }
422 }
423 
425 {
426  y.initZeros(MAT_YSIZE(A));
427  for (size_t i = 0; i < MAT_YSIZE(A); ++i)
428  {
429  double aux=0.;
430  for (size_t k = 0; k < MAT_XSIZE(A); ++k)
431  aux += MAT_ELEM(A, i, k) * VEC_ELEM(x, k);
432  VEC_ELEM(y, i)=aux;
433  }
434 }
435 
437 {
438  B.resizeNoCopy(MAT_XSIZE(A), MAT_XSIZE(A));
439  for (size_t i = 0; i < MAT_XSIZE(A); ++i)
440  for (size_t j = i; j < MAT_XSIZE(A); ++j)
441  {
442  double aux=0.;
443  for (size_t k = 0; k < MAT_YSIZE(A); ++k)
444  aux += MAT_ELEM(A, k, i) * MAT_ELEM(A, k, j);
445  MAT_ELEM(B, j, i) = MAT_ELEM(B, i, j) = aux;
446  }
447 }
448 
450 {
451  C.initZeros(MAT_YSIZE(A), MAT_YSIZE(A));
452  for (size_t i = 0; i < MAT_YSIZE(A); ++i)
453  for (size_t j = i; j < MAT_YSIZE(A); ++j)
454  {
455  double aux=0.;
456  for (size_t k = 0; k < MAT_XSIZE(A); ++k)
457  aux += MAT_ELEM(A, i, k) * MAT_ELEM(A, j, k);
458  MAT_ELEM(C, j, i)=MAT_ELEM(C, i, j)=aux;
459  }
460 }
461 
463 {
464  C.initZeros(MAT_YSIZE(A), MAT_YSIZE(B));
465  for (size_t i = 0; i < MAT_YSIZE(A); ++i)
466  for (size_t j = 0; j < MAT_YSIZE(B); ++j)
467  {
468  double aux=0.;
469  for (size_t k = 0; k < MAT_XSIZE(A); ++k)
470  aux += MAT_ELEM(A, i, k) * MAT_ELEM(B, j, k);
471  MAT_ELEM(C, i, j)=aux;
472  }
473 }
474 
476 {
477  C.resizeNoCopy(MAT_XSIZE(A), MAT_XSIZE(B));
478  for (size_t i = 0; i < MAT_XSIZE(A); ++i)
479  for (size_t j = 0; j < MAT_XSIZE(B); ++j)
480  {
481  double aux=0.;
482  for (size_t k = 0; k < MAT_YSIZE(A); ++k)
483  aux += MAT_ELEM(A, k, i) * MAT_ELEM(B, k, j);
484  MAT_ELEM(C, i, j)=aux;
485  }
486 }
487 
489 {
490  y.resizeNoCopy(MAT_XSIZE(A));
491  for (size_t i = 0; i < MAT_XSIZE(A); ++i)
492  {
493  double aux=0.;
494  for (size_t k = 0; k < MAT_YSIZE(A); ++k)
495  aux += MAT_ELEM(A, k, i) * VEC_ELEM(x, k);
496  VEC_ELEM(y, i)=aux;
497  }
498 }
499 
501 {
502  C.initZeros(MAT_XSIZE(A), MAT_YSIZE(B));
503  for (size_t i = 0; i < MAT_XSIZE(A); ++i)
504  for (size_t j = 0; j < MAT_YSIZE(B); ++j)
505  {
506  double aux=0.;
507  for (size_t k = 0; k < MAT_YSIZE(A); ++k)
508  aux += MAT_ELEM(A, k, i) * MAT_ELEM(B, j, k);
509  MAT_ELEM(C, i, j)=aux;
510  }
511 }
512 
514 {
515  Matrix2D<double> AX=A*X;
516  B.resizeNoCopy(MAT_XSIZE(X), MAT_XSIZE(X));
517  for (size_t i = 0; i < MAT_XSIZE(X); ++i)
518  for (size_t j = i; j < MAT_XSIZE(X); ++j)
519  {
520  double aux=0.;
521  for (size_t k = 0; k < MAT_YSIZE(X); ++k)
522  aux += MAT_ELEM(X, k, i) * MAT_ELEM(AX, k, j);
523  MAT_ELEM(B, j, i) = MAT_ELEM(B, i, j) = aux;
524  }
525 }
526 
528 {
529  for (size_t i=0; i<MAT_YSIZE(A); ++i)
530  MAT_ELEM(A,i,i)+=1;
531 }
532 
534 {
536  if (i == j)
537  MAT_ELEM(A, i, j) = 1 - MAT_ELEM(A, i, j);
538  else
539  MAT_ELEM(A, i, j) = -MAT_ELEM(A, i, j);
540 }
541 
543 {
544  Matrix2D<double> Ap;
545  Ap.resize(MAT_YSIZE(A),MAT_XSIZE(A)-1);
546  for (size_t i = 0; i < MAT_YSIZE(A); ++i)
547  memcpy(&MAT_ELEM(Ap,i,0),&MAT_ELEM(A,i,1),MAT_XSIZE(Ap)*sizeof(double));
548  A=Ap;
549 }
550 
551 void keepColumns(Matrix2D<double> &A, int j0, int jF)
552 {
553  Matrix2D<double> Ap;
554  Ap.resize(MAT_YSIZE(A),jF-j0+1);
555  for (size_t i = 0; i < MAT_YSIZE(A); ++i)
556  memcpy(&MAT_ELEM(Ap,i,0),&MAT_ELEM(A,i,j0),MAT_XSIZE(Ap)*sizeof(double));
557  A=Ap;
558 }
559 
561 {
562  for(size_t j1=0; j1<MAT_XSIZE(M); j1++)
563  {
564  // Normalize column j1
565  double norm=0;
566  for (size_t i=0; i<MAT_YSIZE(M); i++)
567  norm+=MAT_ELEM(M,i,j1)*MAT_ELEM(M,i,j1);
568  double K=1.0/sqrt(norm);
569  for(size_t i = 0; i<MAT_YSIZE(M); i++)
570  MAT_ELEM(M,i,j1) *=K;
571 
572  // Update rest of columns
573  for(size_t j2=j1+1; j2<MAT_XSIZE(M); j2++)
574  {
575  // Compute the dot product
576  double K = 0;
577  for (size_t i=0; i<MAT_YSIZE(M); i++)
578  K+=MAT_ELEM(M,i,j1)*MAT_ELEM(M,i,j2);
579  for (size_t i=0; i<MAT_YSIZE(M); i++)
580  MAT_ELEM(M,i,j2) -= K*MAT_ELEM(M,i,j1);
581  }
582  }
583 }
584 
585 template<typename T>
586 void ludcmp(const Matrix2D<T>& A, Matrix2D<T>& LU, Matrix1D< int >& indx, T& d)
587 {
588  LU = A;
589  if (VEC_XSIZE(indx)!=A.mdimx)
590  indx.resizeNoCopy(A.mdimx);
592  indx.adaptForNumericalRecipes(), &d);
593 }
594 
595 template<typename T>
597 {
601 }
602 
603 template<typename T>
605 {
606  Xmr.initZeros(MAT_YSIZE(*this));
608  VEC_ELEM(Xmr,i)+=MAT_ELEM(*this,i,j);
609  Xmr*=1.0/MAT_XSIZE(*this);
610 }
611 
612 template<typename T>
614 {
615  Xmr.initZeros(MAT_YSIZE(*this));
617  VEC_ELEM(Xmr,j)+=MAT_ELEM(*this,i,j);
618  Xmr*=1.0/MAT_YSIZE(*this);
619  }
620 
621 template<>
622 void Matrix2D<double>::invAlgLib(Matrix2D<double>& result, bool use_lu) const
623 {
624  if (mdimx < 5) {
625  return this->inv(result);
626  }
627 
628  // copy data to alglib matrix
629  const auto N = this->mdimx;
631  a.setlength(N, N);
632  for(size_t i = 0; i < N; ++i) {
633  auto dest = a.c_ptr()->ptr.pp_double[i];
634  auto src = this->mdata + (i * N);
635  memcpy(dest, src, N * sizeof(double));
636  }
637 
638  // compute inverse
639  alglib::ae_int_t info;
641  if(use_lu) {
643  alglib::rmatrixlu(a, N, N, pivots);
644  alglib::rmatrixluinverse(a, pivots, info, rep);
645  } else {
646  alglib::rmatrixinverse(a, info, rep); // inplace inverse
647  }
648  if (1 != (int)info) {
649  REPORT_ERROR(ERR_NUMERICAL,"Could not perform matrix inversion using alglib");
650  }
651 
652  // get result from alglib matrix
653  result.resizeNoCopy(*this);
654  for(size_t i = 0; i < N; ++i) {
655  auto src = a.c_ptr()->ptr.pp_double[i];
656  auto dest = result.mdata + (i * N);
657  memcpy(dest, src, N * sizeof(double));
658  }
659 }
660 
661 
662 template<typename T>
663 void Matrix2D<T>::inv(Matrix2D<T>& result) const
664 {
665  if (mdimx == 0 || mdimy == 0)
666  REPORT_ERROR(ERR_MATRIX_EMPTY, "Inverse: Matrix is empty");
667  result.initZeros(mdimx, mdimy);
669  if (mdimx==2)
670  {
671  M2x2_INV(result,*this);
672  }
673  else if (mdimx==3)
674  {
675  M3x3_INV(result,*this);
676  }
677  else if (mdimx==4)
678  {
679  M4x4_INV(result,*this);
680  }
681  else
682  {
683  // Perform SVD decomposition
686  svdcmp(*this, u, w, v); // *this = U * W * V^t
687 
688  double tol = computeMax() * XMIPP_MAX(mdimx, mdimy) * 1e-14;
689 
690  // Compute W^-1
691  bool invertible = false;
693  {
694  if (fabs(VEC_ELEM(w,i)) > tol)
695  {
696  VEC_ELEM(w,i) = 1.0 / VEC_ELEM(w,i);
697  invertible = true;
698  }
699  else
700  VEC_ELEM(w,i) = 0.0;
701  }
702 
703  if (!invertible)
704  return;
705 
706  // Compute V*W^-1
708  MAT_ELEM(v,i,j) *= VEC_ELEM(w,j);
709 
710  // Compute Inverse
711  for (size_t i = 0; i < mdimx; i++)
712  for (size_t j = 0; j < mdimy; j++)
713  for (size_t k = 0; k < mdimx; k++)
714  MAT_ELEM(result,i,j) += MAT_ELEM(v,i,k) * MAT_ELEM(u,j,k);
715  }
716 }
717 
718 template<typename T>
720 {
721  svdcmp(*this, U, W, V);
722  indexes.resizeNoCopy(W);
723  indexes.enumerate();
724 
725  double dAux;
726  int iAux;
727 
729  {
730  for (int j = i; j > 0 && dMi(W, j) > dMi(W, j-1); --j)
731  {
732  VEC_SWAP(W, j, j-1, dAux);
733  VEC_SWAP(indexes, j, j-1, iAux);
734  }
735  }
736 }
737 
738 template<typename T>
740 {
741  Matrix1D<T> result;
742 
743  if (VEC_XSIZE(*this) != MAT_YSIZE(M))
744  REPORT_ERROR(ERR_MATRIX_SIZE, "Not compatible sizes in matrix by vector");
745 
746  if (!isRow())
747  REPORT_ERROR(ERR_MATRIX_DIM, "Vector is not a row");
748 
749  result.initZeros(MAT_XSIZE(M));
750  for (size_t j = 0; j < MAT_XSIZE(M); j++)
751  for (size_t i = 0; i < MAT_YSIZE(M); i++)
752  VEC_ELEM(result,j) += VEC_ELEM(*this,i) * MAT_ELEM(M,i, j);
753 
754  result.setRow();
755  return result;
756 }
757 
758 template<typename T>
760 {
761  Matrix1D<T> result;
762 
763  if (mdimx != VEC_XSIZE(op1))
764  REPORT_ERROR(ERR_MATRIX_SIZE, "Not compatible sizes in matrix by vector");
765 
766  if (!op1.isCol())
767  REPORT_ERROR(ERR_MATRIX, "Vector is not a column");
768 
769  result.initZeros(mdimy);
770  for (size_t i = 0; i < mdimy; i++)
771  for (size_t j = 0; j < mdimx; j++)
772  VEC_ELEM(result,i) += MAT_ELEM(*this,i, j) * VEC_ELEM(op1,j);
773 
774  result.setCol();
775  return result;
776 }
777 
778 template<typename T>
780 {
781  sum.initZeros(MAT_YSIZE(*this));
783  VEC_ELEM(sum,i)+=MAT_ELEM(*this,i,j);
784 }
785 
786 template<typename T>
788 {
789  sum.initZeros(MAT_XSIZE(*this));
791  VEC_ELEM(sum,j)+=MAT_ELEM(*this,i,j);
792 }
793 
794 template<typename T>
796 {
797  sum.initZeros(MAT_YSIZE(*this));
799  VEC_ELEM(sum,i)+=MAT_ELEM(*this,i,j)*MAT_ELEM(*this,i,j);
800 }
801 
802 template<typename T>
804 {
805  // Null vector => Null matrix
806  if (op1.size() == 0)
807  {
808  clear();
809  return;
810  }
811 
812  // Look at shape and copy values
813  if (op1.isRow())
814  {
815  if (mdimy!=1 || mdimx!=VEC_XSIZE(op1))
816  resizeNoCopy(1, VEC_XSIZE(op1));
817 
818  for (size_t j = 0; j < VEC_XSIZE(op1); j++)
819  MAT_ELEM(*this,0, j) = VEC_ELEM(op1,j);
820  }
821  else
822  {
823  if (mdimy!=1 || mdimx!=VEC_XSIZE(op1))
824  resizeNoCopy(VEC_XSIZE(op1), 1);
825 
826  for (size_t i = 0; i < VEC_XSIZE(op1); i++)
827  MAT_ELEM(*this, i, 0) = VEC_ELEM(op1,i);
828  }
829 }
830 
831 template<typename T>
833 {
834  // Null matrix => Null vector
835  if (mdimx == 0 || mdimy == 0)
836  {
837  op1.clear();
838  return;
839  }
840 
841  // If matrix is not a vector, produce an error
842  if (!(mdimx == 1 || mdimy == 1))
844  "toVector: Matrix cannot be converted to vector");
845 
846  // Look at shape and copy values
847  if (mdimy == 1)
848  {
849  // Row vector
850  if (VEC_XSIZE(op1)!=mdimx)
851  op1.resizeNoCopy(mdimx);
852 
853  memcpy(&VEC_ELEM(op1,0),&MAT_ELEM(*this,0,0),mdimx*sizeof(double));
854 
855  op1.setRow();
856  }
857  else
858  {
859  // Column vector
860  if (VEC_XSIZE(op1)!=mdimy)
861  op1.resizeNoCopy(mdimy);
862 
863  for (size_t i = 0; i < mdimy; i++)
864  VEC_ELEM(op1,i) = MAT_ELEM(*this,i, 0);
865 
866  op1.setCol();
867  }
868 }
869 
870 template<typename T>
871 void Matrix2D<T>::getRow(size_t i, Matrix1D<T>& v) const
872 {
873  if (mdimx == 0 || mdimy == 0)
874  {
875  v.clear();
876  return;
877  }
878 
879  if (i >= mdimy)
880  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, "getRow: Matrix subscript (i) greater than matrix dimension");
881 
882  if (VEC_XSIZE(v)!=mdimx)
883  v.resizeNoCopy(mdimx);
884  memcpy(&VEC_ELEM(v,0),&MAT_ELEM(*this,i,0),mdimx*sizeof(T));
885 
886  v.setRow();
887 }
888 
889 template<typename T>
890 void Matrix2D<T>::getCol(size_t j, Matrix1D<T>& v) const
891 {
892  if (mdimx == 0 || mdimy == 0)
893  {
894  v.clear();
895  return;
896  }
897 
898  if (j >= mdimx)
899  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS,"getCol: Matrix subscript (j) greater than matrix dimension");
900 
901  if (VEC_XSIZE(v)!=mdimy)
902  v.resizeNoCopy(mdimy);
903  for (size_t i = 0; i < mdimy; i++)
904  VEC_ELEM(v,i) = MAT_ELEM(*this,i, j);
905 
906  v.setCol();
907 }
908 
909 template<typename T>
910 void Matrix2D<T>::setRow(size_t i, const Matrix1D<T>& v)
911 {
912  if (mdimx == 0 || mdimy == 0)
913  REPORT_ERROR(ERR_MATRIX_EMPTY, "setRow: Target matrix is empty");
914 
915  if (i >= mdimy)
916  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, "setRow: Matrix subscript (i) out of range");
917 
918  if (VEC_XSIZE(v) != mdimx)
920  "setRow: Vector dimension different from matrix one");
921 
922  if (!v.isRow())
923  REPORT_ERROR(ERR_MATRIX_DIM, "setRow: Not a row vector in assignment");
924 
925  memcpy(&MAT_ELEM(*this,i,0),&VEC_ELEM(v,0),mdimx*sizeof(double));
926 }
927 
928 template<typename T>
929 void Matrix2D<T>::setCol(size_t j, const Matrix1D<T>& v)
930 {
931  if (mdimx == 0 || mdimy == 0)
932  REPORT_ERROR(ERR_MATRIX_EMPTY, "setCol: Target matrix is empty");
933 
934  if (j>= mdimx)
935  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, "setCol: Matrix subscript (j) out of range");
936 
937  if (VEC_XSIZE(v) != mdimy)
939  "setCol: Vector dimension different from matrix one");
940 
941  if (!v.isCol())
942  REPORT_ERROR(ERR_MATRIX_DIM, "setCol: Not a column vector in assignment");
943 
944  for (size_t i = 0; i < mdimy; i++)
945  MAT_ELEM(*this,i, j) = VEC_ELEM(v,i);
946 }
947 
948 template<typename T>
950 {
951  d.resizeNoCopy(MAT_XSIZE(*this));
952  for (size_t i=0; i<MAT_XSIZE(*this); ++i)
953  VEC_ELEM(d,i)=MAT_ELEM(*this,i,i);
954 }
955 
956 template<typename T>
958 {
959  if (&op1 != this)
960  {
961  if (MAT_XSIZE(*this)!=MAT_XSIZE(op1) ||
962  MAT_YSIZE(*this)!=MAT_YSIZE(op1))
963  resizeNoCopy(op1);
964  memcpy(mdata,op1.mdata,op1.mdim*sizeof(T));
965  }
966 
967  return *this;
968 }
969 
970 template<typename T>
972 {
973  mdimx=mdimy=mdim=0;
974  mdata=NULL;
975  mdataOriginal=NULL;
976  destroyData=true;
977  mappedData=false;
978  fdMap=-1;
979 }
980 
981 template<typename T>
982 void Matrix2D<T>::coreAllocate( int _mdimy, int _mdimx)
983 {
984  if (_mdimy <= 0 ||_mdimx<=0)
985  {
986  clear();
987  return;
988  }
989 
990  mdimx=_mdimx;
991  mdimy=_mdimy;
992  mdim=_mdimx*_mdimy;
993  mdata = new T [mdim];
994  mdataOriginal = NULL;
995  mappedData=false;
996  fdMap=-1;
997  if (mdata == NULL)
998  REPORT_ERROR(ERR_MEM_NOTENOUGH, "coreAllocate: No space left");
999 }
1000 
1001 template<typename T>
1003 {
1004  if (mdata != NULL && destroyData)
1005  delete[] mdata;
1006  if (mappedData)
1007  {
1008 #ifdef XMIPP_MMAP
1009  munmap(mdataOriginal,mdimx*mdimy*sizeof(T));
1010  close(fdMap);
1011 #else
1012 
1013  REPORT_ERROR(ERR_MMAP,"Mapping not supported in Windows");
1014 #endif
1015 
1016  }
1017  mdata=NULL;
1018  mdataOriginal=NULL;
1019 }
1020 
1021 template<typename T>
1022 void Matrix2D<T>::resize(size_t Ydim, size_t Xdim, bool noCopy)
1023 {
1024 
1025  if (Xdim == mdimx && Ydim == mdimy)
1026  return;
1027 
1028  if (Xdim <= 0 || Ydim <= 0)
1029  {
1030  clear();
1031  return;
1032  }
1033 
1034  T * new_mdata;
1035  size_t YXdim=Ydim*Xdim;
1036 
1037  try
1038  {
1039  new_mdata = new T [YXdim];
1040  }
1041  catch (std::bad_alloc &)
1042  {
1043  REPORT_ERROR(ERR_MEM_NOTENOUGH, "Allocate: No space left");
1044  }
1045 
1046  // Copy needed elements, fill with 0 if necessary
1047  if (!noCopy)
1048  {
1049  T zero=0; // Useful for complexes
1050  for (size_t i = 0; i < Ydim; i++)
1051  for (size_t j = 0; j < Xdim; j++)
1052  {
1053  T *val=NULL;
1054  if (i >= mdimy)
1055  val = &zero;
1056  else if (j >= mdimx)
1057  val = &zero;
1058  else
1059  val = &mdata[i*mdimx + j];
1060  new_mdata[i*Xdim+j] = *val;
1061  }
1062  }
1063  else
1064  memset(new_mdata,0,YXdim*sizeof(T));
1065 
1066  // deallocate old vector
1067  coreDeallocate();
1068 
1069  // assign *this vector to the newly created
1070  mdata = new_mdata;
1071  mdimx = Xdim;
1072  mdimy = Ydim;
1073  mdim = Xdim * Ydim;
1074  mappedData = false;
1075 }
1076 
1077 
1078 template<typename T>
1079 void Matrix2D<T>::mapToFile(const FileName &fn, int Ydim, int Xdim, size_t offset)
1080 {
1081  if (mdata!=NULL)
1082  clear();
1083 
1084 #ifdef XMIPP_MMAP
1085 
1086  coreInit(fn,Ydim,Xdim,offset);
1087 #else
1088 
1089  resizeNoCopy(Ydim, Xdim);
1090 #endif
1091 
1092 }
1093 
1094 template<typename T>
1095 void Matrix2D<T>::submatrix(int i0, int j0, int iF, int jF)
1096 {
1097  if (i0 < 0 || j0 < 0 || iF >= MAT_YSIZE(*this) || jF >= MAT_XSIZE(*this))
1098  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS,"Submatrix indexes out of bounds");
1099  Matrix2D<T> result(iF - i0 + 1, jF - j0 + 1);
1100 
1102  MAT_ELEM(result, i, j) = MAT_ELEM(*this, i+i0, j+j0);
1103 
1104  *this = result;
1105 }
1106 
1107 template<typename T>
1108 void Matrix2D<T>::initRandom(size_t Ydim, size_t Xdim, double op1, double op2, RandomMode mode)
1109 {
1110  if (mdimx!=Xdim || mdimy!=Ydim)
1111  resizeNoCopy(Ydim, Xdim);
1112  for (size_t j = 0; j < mdim; j++)
1113  mdata[j] = static_cast< T > (mode == RND_UNIFORM ? rnd_unif(op1, op2) : rnd_gaus(op1, op2));
1114 }
1115 
1117 template<typename T>
1118 void Matrix2D<T>::initGaussian(int Ydim, int Xdim, double op1, double op2)
1119 {
1120  initRandom(Ydim, Xdim, op1, op2, RND_GAUSSIAN);
1121 }
1122 
1123 
1124 template<typename T>
1125 void Matrix2D<T>::initGaussian(int dim, double var)
1126 {
1127  double center = ((double)dim)/2;
1128  initZeros(dim, dim);
1129  for (int i = 0; i < dim; i++)
1130  for (int j = 0; j < dim; j++)
1131  MAT_ELEM(*this,i,j) = std::exp(-( (i-center)*(i-center)+(j-center)*(j-center) )/(2*var*var));
1132 }
1133 
1134 template<typename T>
1136 {
1137  Matrix2D<T> result;
1138  if (mdimx != op1.mdimy)
1139  REPORT_ERROR(ERR_MATRIX_SIZE, "Not compatible sizes in matrix multiplication");
1140 
1141  result.initZeros(mdimy, op1.mdimx);
1142  for (size_t i = 0; i < mdimy; i++)
1143  for (size_t j = 0; j < op1.mdimx; j++)
1144  for (size_t k = 0; k < mdimx; k++)
1145  MAT_ELEM(result,i, j) += MAT_ELEM(*this,i, k) * MAT_ELEM(op1, k, j);
1146  return result;
1147 }
1148 
1149 template<typename T>
1151 {
1152  Matrix2D<T> result;
1153  if (mdimx != op1.mdimx || mdimy != op1.mdimy)
1154  REPORT_ERROR(ERR_MATRIX_SIZE, "operator+: Not same sizes in matrix summation");
1155 
1156  result.initZeros(mdimy, mdimx);
1157  for (size_t i = 0; i < mdimy; i++)
1158  for (size_t j = 0; j < mdimx; j++)
1159  result(i, j) = (*this)(i, j) + op1(i, j);
1160 
1161  return result;
1162 }
1163 
1164 template<typename T>
1165 void Matrix2D<T>::operator+=(const Matrix2D<T>& op1) const
1166 {
1167  if (mdimx != op1.mdimx || mdimy != op1.mdimy)
1168  REPORT_ERROR(ERR_MATRIX_SIZE, "operator+=: Not same sizes in matrix summation");
1169 
1170  for (size_t i = 0; i < mdimy; i++)
1171  for (size_t j = 0; j < mdimx; j++)
1172  MAT_ELEM(*this,i, j) += MAT_ELEM(op1, i, j);
1173 }
1174 
1175 template<typename T>
1177 {
1178  Matrix2D<T> result;
1179  if (mdimx != op1.mdimx || mdimy != op1.mdimy)
1180  REPORT_ERROR(ERR_MATRIX_SIZE, "operator-: Not same sizes in matrix summation");
1181 
1182  result.initZeros(mdimy, mdimx);
1183  for (size_t i = 0; i < mdimy; i++)
1184  for (size_t j = 0; j < mdimx; j++)
1185  result(i, j) = (*this)(i, j) - op1(i, j);
1186 
1187  return result;
1188 }
1189 
1190 template<typename T>
1191 void Matrix2D<T>::operator-=(const Matrix2D<T>& op1) const
1192 {
1193  if (mdimx != op1.mdimx || mdimy != op1.mdimy)
1194  REPORT_ERROR(ERR_MATRIX_SIZE, "operator-=: Not same sizes in matrix summation");
1195 
1196  for (size_t i = 0; i < mdimy; i++)
1197  for (size_t j = 0; j < mdimx; j++)
1198  MAT_ELEM(*this,i, j) -= MAT_ELEM(op1, i, j);
1199 }
1200 
1201 template<typename T>
1203  double accuracy) const
1204 {
1205  if (!sameShape(op))
1206  return false;
1207  for (size_t i = 0; i < mdimy; i++)
1208  for (size_t j = 0; j < mdimx; j++)
1209  if (fabs( MAT_ELEM(*this,i,j) - MAT_ELEM(op,i,j) ) > accuracy)
1210  {
1211  //std::cerr << "DEBUG_ROB: MAT_ELEM(*this,i,j): " << MAT_ELEM(*this,i,j) << std::endl;
1212  //std::cerr << "DEBUG_ROB: MAT_ELEM(op,i,j): " << MAT_ELEM(op,i,j) << std::endl;
1213  return false;
1214  }
1215  return true;
1216 }
1217 
1218 template<typename T>
1220  double accuracy) const
1221 {
1222  if (!sameShape(op))
1223  return false;
1224  for (size_t i = 0; i < mdimy; i++)
1225  for (size_t j = 0; j < mdimx; j++)
1226  if ( (fabs( MAT_ELEM(*this,i,j)) - fabs(MAT_ELEM(op,i,j)) )> accuracy)
1227  {
1228  //std::cerr << "DEBUG_ROB: MAT_ELEM(*this,i,j): " << MAT_ELEM(*this,i,j) << std::endl;
1229  //std::cerr << "DEBUG_ROB: MAT_ELEM(op,i,j): " << MAT_ELEM(op,i,j) << std::endl;
1230  return false;
1231  }
1232  return true;
1233 }
1234 
1235 template<typename T>
1237 {
1238  if (mdim <= 0)
1239  return static_cast< T >(0);
1240 
1241  T maxval = mdata[0];
1242  for (size_t n = 0; n < mdim; n++)
1243  if (mdata[n] > maxval)
1244  maxval = mdata[n];
1245  return maxval;
1246 }
1247 
1248 template<typename T>
1250 {
1251  if (mdim <= 0)
1252  return static_cast< T >(0);
1253 
1254  T minval = mdata[0];
1255  for (size_t n = 0; n < mdim; n++)
1256  if (mdata[n] < minval)
1257  minval = mdata[n];
1258  return minval;
1259 }
1260 
1261 template<typename T>
1262 void Matrix2D<T>::computeMaxAndMin(T &maxValue, T &minValue) const
1263 {
1264  maxValue=minValue=0;
1265  if (mdim <= 0)
1266  return;
1267 
1268  maxValue = minValue = mdata[0];
1269  for (size_t n = 0; n < mdim; n++)
1270  {
1271  T val=mdata[n];
1272  if (val < minValue)
1273  minValue = val;
1274  else if (val > maxValue)
1275  maxValue = val;
1276  }
1277 }
1278 
1279 template<typename T>
1281 {
1282  T** m = NULL;
1283  ask_Tmatrix(m, 1, mdimy, 1, mdimx);
1284 
1285  for (int i = 0; i < mdimy; i++)
1286  for (int j = 0; j < mdimx; j++)
1287  m[i+1][j+1] = mdata[i*mdimx + j];
1288 
1289  return m;
1290 }
1291 
1292 template<typename T>
1294 {
1295  if (mdimx!=Xdim || mdimy!=Ydim)
1296  resizeNoCopy(Ydim, Xdim);
1297 
1298  for (int i = 1; i <= Ydim; i++)
1299  for (int j = 1; j <= Xdim; j++)
1300  MAT_ELEM(*this,i - 1, j - 1) = m[i][j];
1301 }
1302 
1303 template<typename T>
1305 {
1306  size_t d=std::min(MAT_XSIZE(*this),MAT_YSIZE(*this));
1307  T retval=0;
1308  for (size_t i=0; i<d; ++i)
1309  retval+=MAT_ELEM(*this,i,i);
1310  return retval;
1311 }
1312 
1313 template<typename T>
1315 {
1316  Matrix2D<T> result(mdimx, mdimy);
1318  MAT_ELEM(result,i,j) = MAT_ELEM((*this),j,i);
1319  return result;
1320 }
1321 
1322 template<typename T>
1324 {
1325  for (size_t i = 0; i < mdimy; i++)
1326  for (size_t j = 0; j < mdimx; j++)
1327  if (i != j)
1328  {
1329  if (MAT_ELEM(*this,i,j)!=0)
1330  return false;
1331  }
1332  else
1333  {
1334  if (MAT_ELEM(*this,i,j)!=1)
1335  return false;
1336  }
1337  return true;
1338 }
1339 
1340 template void ludcmp<double>(Matrix2D<double> const&, Matrix2D<double>&, Matrix1D<int>&, double&);
1345 template class Matrix2D<double>;
1346 template class Matrix2D<int>;
1347 template class Matrix2D<float>;
1348 template class Matrix2D<unsigned char>;
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
Index out of bounds.
Definition: xmipp_error.h:132
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
size_t Xdim() const
Definition: matrix2d.h:575
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 min(Image< double > &op1, const Image< double > &op2)
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
void rmatrixlu(real_2d_array &a, const ae_int_t m, const ae_int_t n, integer_1d_array &pivots)
Definition: linalg.cpp:2942
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
void rowSum(Matrix1D< T > &sum) const
Definition: matrix2d.cpp:779
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 clear()
Definition: matrix1d.cpp:67
void getDiagonal(Matrix1D< T > &d) const
Definition: matrix2d.cpp:949
void normalizeColumns(Matrix2D< double > &A)
Definition: matrix2d.cpp:188
#define VEC_SWAP(v, i, j, aux)
Definition: matrix1d.h:248
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define SPEED_UP_temps0
Definition: xmipp_macros.h:394
size_t size() const
Definition: matrix1d.h:508
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
Problem with matrix size.
Definition: xmipp_error.h:152
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
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
Global mmap error.
Definition: xmipp_error.h:170
template void svdcmp< double >(Matrix2D< double > const &, Matrix2D< double > &, Matrix1D< double > &, Matrix2D< double > &)
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 sqrt(Image< double > &op)
Matrix error.
Definition: xmipp_error.h:149
union alglib_impl::ae_matrix::@12 ptr
template void svdcmp< float >(Matrix2D< float > const &, Matrix2D< double > &, Matrix1D< double > &, Matrix2D< double > &)
Matrix1D< T > operator*(T op1) const
Definition: matrix1d.cpp:114
static double * y
void computeMaxAndMin(T &maxValue, T &minValue) const
Definition: matrix2d.cpp:1262
bool smatrixgevd(const real_2d_array &a, const ae_int_t n, const bool isuppera, const real_2d_array &b, const bool isupperb, const ae_int_t zneeded, const ae_int_t problemtype, real_1d_array &d, real_2d_array &z)
Definition: linalg.cpp:6778
There is not enough memory for allocation.
Definition: xmipp_error.h:166
T trace() const
Definition: matrix2d.cpp:1304
void choldc(double *a, int n, double *p)
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
doublereal * w
T * mdata
Definition: matrix2d.h:395
void enumerate()
Definition: matrix1d.cpp:98
RandomMode
String integerToString(int I, int _width, char fill_with)
Matrix2D< T > transpose() const
Definition: matrix2d.cpp:1314
template void lubksb< double >(Matrix2D< double > const &, Matrix1D< int > &, Matrix1D< double > &)
const alglib_impl::ae_matrix * c_ptr() const
Definition: ap.cpp:6463
T norm(const std::vector< T > &v)
Definition: vector_ops.h:399
void matrixOperation_IminusA(Matrix2D< double > &A)
Definition: matrix2d.cpp:533
T * adaptForNumericalRecipes2() const
Definition: matrix2d.h:875
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
doublereal * x
#define i
void setCol()
Definition: matrix1d.h:554
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
doublereal * d
bool smatrixevdi(const real_2d_array &a, const ae_int_t n, const ae_int_t zneeded, const bool isupper, const ae_int_t i1, const ae_int_t i2, real_1d_array &w, real_2d_array &z)
Definition: linalg.cpp:2009
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
#define M4x4_INV(Ainv, A)
Definition: matrix2d.h:323
void matrixOperation_AAt(const Matrix2D< double > &A, Matrix2D< double > &C)
Definition: matrix2d.cpp:449
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
double rnd_unif()
bool rmatrixevd(const real_2d_array &a, const ae_int_t n, const ae_int_t vneeded, real_1d_array &wr, real_1d_array &wi, real_2d_array &vl, real_2d_array &vr)
Definition: linalg.cpp:2468
void connectedComponentsOfUndirectedGraph(const Matrix2D< double > &G, Matrix1D< int > &component)
Definition: matrix2d.cpp:360
Definition: mask.h:36
Map addressing of file has failed.
Definition: xmipp_error.h:171
void clear()
Definition: matrix2d.h:473
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
#define M3x3_INV(Ainv, A)
Definition: matrix2d.h:302
void normalizeColumnsBetween0and1(Matrix2D< double > &A)
Definition: matrix2d.cpp:221
void orthogonalizeColumnsGramSchmidt(Matrix2D< double > &M)
Definition: matrix2d.cpp:560
bool rmatrixschur(real_2d_array &a, const ae_int_t n, real_2d_array &s)
Definition: linalg.cpp:7050
doublereal * b
void lubksb(const Matrix2D< T > &LU, Matrix1D< int > &indx, Matrix1D< T > &b)
Definition: matrix2d.cpp:596
void rmatrixluinverse(real_2d_array &a, const integer_1d_array &pivots, const ae_int_t n, ae_int_t &info, matinvreport &rep)
Definition: linalg.cpp:3771
Matrix2D< T > inv() const
Definition: matrix2d.h:1136
void cholesky(const Matrix2D< double > &M, Matrix2D< double > &L)
Definition: matrix2d.cpp:160
int fdMap
Definition: matrix2d.h:404
int isRow() const
Definition: matrix1d.h:520
Problem with matrix dimensions.
Definition: xmipp_error.h:150
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
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
T det() const
Definition: matrix2d.cpp:38
void firstEigs(const Matrix2D< double > &A, size_t M, Matrix1D< double > &D, Matrix2D< double > &P, bool Pneeded)
Definition: matrix2d.cpp:284
void matrixOperation_IplusA(Matrix2D< double > &A)
Definition: matrix2d.cpp:527
Error related to numerical calculation.
Definition: xmipp_error.h:179
void generalizedEigs(const Matrix2D< double > &A, const Matrix2D< double > &B, Matrix1D< double > &D, Matrix2D< double > &P)
Definition: matrix2d.cpp:267
double z
bool mappedData
Definition: matrix2d.h:401
size_t Ydim() const
Definition: matrix2d.h:584
File or directory does not exist.
Definition: xmipp_error.h:136
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
int isCol() const
Definition: matrix1d.h:532
void coreDeallocate()
Definition: matrix2d.cpp:1002
void loadFromNumericalRecipes(T **m, int Ydim, int Xdim)
Definition: matrix2d.cpp:1293
void initZeros()
Definition: matrix1d.h:592
void lastEigs(const Matrix2D< double > &A, size_t M, Matrix1D< double > &D, Matrix2D< double > &P)
Definition: matrix2d.cpp:305
void schur(const Matrix2D< double > &M, Matrix2D< double > &O, Matrix2D< double > &T)
Definition: matrix2d.cpp:251
void allEigs(const Matrix2D< double > &A, std::vector< std::complex< double > > &eigs)
Definition: matrix2d.cpp:345
void svdcmp(const Matrix2D< T > &a, Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v)
Definition: matrix2d.cpp:125
void matrixOperation_Atx(const Matrix2D< double > &A, const Matrix1D< double > &x, Matrix1D< double > &y)
Definition: matrix2d.cpp:488
#define dMi(v, i)
Definition: matrix1d.h:246
#define j
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
File cannot be open.
Definition: xmipp_error.h:137
double ** pp_double
Definition: ap.h:455
template void ludcmp< double >(Matrix2D< double > const &, Matrix2D< double > &, Matrix1D< int > &, double &)
void setRow(size_t i, const Matrix1D< T > &v)
Definition: matrix2d.cpp:910
Matrix2D< T > operator-(const Matrix2D< T > &op1) const
Definition: matrix2d.cpp:1176
void typeCast(const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
Definition: matrix1d.h:1227
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
double * getcontent()
Definition: ap.cpp:6207
void read(const FileName &fn)
Definition: matrix2d.cpp:101
std::string String
Definition: xmipp_strings.h:34
void keepColumns(Matrix2D< double > &A, int j0, int jF)
Definition: matrix2d.cpp:551
void write(const FileName &fn) const
Definition: matrix2d.cpp:113
int SingularValueDecomposition(double *U, long Lines, long Columns, double W[], double *V, long MaxIterations, int *Status)
double rnd_gaus()
void initRandom(size_t Ydim, size_t Xdim, double op1, double op2, RandomMode mode=RND_UNIFORM)
Definition: matrix2d.cpp:1108
size_t mdim
Definition: matrix2d.h:416
T ** adaptForNumericalRecipes() const
Definition: matrix2d.cpp:1280
void initZeros()
Definition: matrix2d.h:626
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
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
doublereal * u
void operator-=(const Matrix2D< T > &op1) const
Definition: matrix2d.cpp:1191
void setcontent(ae_int_t irows, ae_int_t icols, const double *pContent)
Definition: ap.cpp:6660
void ask_Tmatrix(T **&m, int nrl, int nrh, int ncl, int nch)
Definition: xmipp_memory.h:40
size_t mdimx
Definition: matrix2d.h:410
constexpr int K
void rmatrixinverse(real_2d_array &a, const ae_int_t n, ae_int_t &info, matinvreport &rep)
Definition: linalg.cpp:3865
T computeMax() const
Definition: matrix2d.cpp:1236
T * vdata
The array itself.
Definition: matrix1d.h:258
#define M2x2_INV(Ainv, A)
Definition: matrix2d.h:286
void initGaussian(int Ydim, int Xdim, double op1=0., double op2=1.)
Definition: matrix2d.cpp:1118
T * adaptForNumericalRecipes() const
Definition: matrix1d.h:844
alglib_impl::ae_int_t ae_int_t
Definition: ap.h:889
#define MATRIX2D_ARRAY(m)
Definition: matrix2d.h:89
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 initConstant(T val)
Definition: matrix1d.cpp:83
void matrixOperation_AtBt(const Matrix2D< double > &A, const Matrix2D< double > &B, Matrix2D< double > &C)
Definition: matrix2d.cpp:500
void setlength(ae_int_t rows, ae_int_t cols)
Definition: ap.cpp:6409
int * n
void resize(size_t Ydim, size_t Xdim, bool noCopy=false)
Definition: matrix2d.cpp:1022
doublereal * a
ql0001_ & zero(ctemp+1),(cvec+1),(a+1),(b+1),(bl+1),(bu+1),(x+1),(w+1), &iout, ifail, &zero,(w+3), &lwar2,(iw+1), &leniw, &glob_grd.epsmac
void svbksb(Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v, Matrix1D< double > &b, Matrix1D< double > &x)
Definition: matrix2d.cpp:174
void setRow()
Definition: matrix1d.h:543
T computeMin() const
Definition: matrix2d.cpp:1249
void computeColMeans(Matrix1D< double > &Xmr) const
Definition: matrix2d.cpp:613