Xmipp  v3.23.11-Nereus
Public Attributes | List of all members
Matrix2D< T > Class Template Reference

#include <mask.h>

Collaboration diagram for Matrix2D< T >:
Collaboration graph
[legend]

Public Member Functions

Constructors
 Matrix2D ()
 
 Matrix2D (const FileName &fnMappedMatrix, int Ydim, int Xdim, size_t offset=0)
 
 Matrix2D (int Ydim, int Xdim)
 
 Matrix2D (const Matrix2D< T > &v)
 
 ~Matrix2D ()
 
Matrix2D< T > & operator= (const Matrix2D< T > &op1)
 
Core memory operations for Matrix2D
void clear ()
 
void convertTo (float out[3][3]) const
 
void coreInit (const FileName &fn, int Ydim, int Xdim, size_t offset=0)
 
void coreInit ()
 
void coreAllocate (int _mdimy, int _mdimx)
 
void coreDeallocate ()
 
Size and shape of Matrix2D
void resize (size_t Ydim, size_t Xdim, bool noCopy=false)
 
template<typename T1 >
void resize (const Matrix2D< T1 > &v)
 
void resizeNoCopy (int Ydim, int Xdim)
 
template<typename T1 >
void resizeNoCopy (const Matrix2D< T1 > &v)
 
void mapToFile (const FileName &fn, int Ydim, int Xdim, size_t offset=0)
 
void submatrix (int i0, int j0, int iF, int jF)
 
template<typename T1 >
bool sameShape (const Matrix2D< T1 > &op) const
 
size_t Xdim () const
 
size_t Ydim () const
 
Initialization of Matrix2D values
void initConstant (T val)
 
void initConstant (size_t Ydim, size_t Xdim, T val)
 
void initZeros ()
 
void initZeros (size_t Ydim, size_t Xdim)
 
template<typename T1 >
void initZeros (const Matrix2D< T1 > &op)
 
void initRandom (size_t Ydim, size_t Xdim, double op1, double op2, RandomMode mode=RND_UNIFORM)
 
void initGaussian (int Ydim, int Xdim, double op1=0., double op2=1.)
 
void initIdentity ()
 
void initIdentity (int dim)
 
void initGaussian (int dim, double var)
 

Public Attributes

T * mdata
 
bool destroyData
 
bool mappedData
 
int fdMap
 
char * mdataOriginal
 
size_t mdimx
 
size_t mdimy
 
size_t mdim
 

Operators for Matrix2D

T & operator() (int i, int j) const
 
void setVal (T val, int y, int x)
 
getVal (int y, int x) const
 
Matrix2D< T > operator* (T op1) const
 
Matrix2D< T > operator/ (T op1) const
 
void operator*= (T op1)
 
void operator/= (T op1)
 
Matrix1D< T > operator* (const Matrix1D< T > &op1) const
 
Matrix2D< T > operator* (const Matrix2D< T > &op1) const
 
Matrix2D< T > operator+ (const Matrix2D< T > &op1) const
 
void operator+= (const Matrix2D< T > &op1) const
 
Matrix2D< T > operator- (const Matrix2D< T > &op1) const
 
void operator-= (const Matrix2D< T > &op1) const
 
bool equal (const Matrix2D< T > &op, double accuracy=XMIPP_EQUAL_ACCURACY) const
 
bool equalAbs (const Matrix2D< T > &op, double accuracy=XMIPP_EQUAL_ACCURACY) const
 
Matrix2D< T > operator* (T op1, const Matrix2D< T > &op2)
 

Utilities for Matrix2D

computeMax () const
 
computeMin () const
 
void computeMaxAndMin (T &maxValue, T &minValue) const
 
void rowSum (Matrix1D< T > &sum) const
 
void colSum (Matrix1D< T > &sum) const
 
void rowEnergySum (Matrix1D< T > &sum) const
 
T ** adaptForNumericalRecipes () const
 
T * adaptForNumericalRecipes2 () const
 
void loadFromNumericalRecipes (T **m, int Ydim, int Xdim)
 
void killAdaptationForNumericalRecipes (T **m) const
 
void killAdaptationForNumericalRecipes2 (T **m) const
 
void read (const FileName &fn)
 
void write (const FileName &fn) const
 
void fromVector (const Matrix1D< T > &op1)
 
void toVector (Matrix1D< T > &op1) const
 
void copyToVector (std::vector< T > &v)
 
void copyFromVector (std::vector< T > &v, int Xdim, int Ydim)
 
void getRow (size_t i, Matrix1D< T > &v) const
 
void getCol (size_t j, Matrix1D< T > &v) const
 
void setRow (size_t i, const Matrix1D< T > &v)
 
void setCol (size_t j, const Matrix1D< T > &v)
 
void computeRowMeans (Matrix1D< double > &Xmr) const
 
void computeColMeans (Matrix1D< double > &Xmr) const
 
void setConstantCol (size_t j, T v)
 
void getDiagonal (Matrix1D< T > &d) const
 
trace () const
 
det () const
 
det3x3 () const
 determinat of 3x3 matrix More...
 
double norm ()
 
Matrix2D< T > transpose () const
 
void inv (Matrix2D< T > &result) const
 
void invAlgLib (Matrix2D< T > &result, bool use_lu=false) const
 
void svd (Matrix2D< double > &U, Matrix1D< double > &W, Matrix2D< double > &V) const
 
void eigs (Matrix2D< double > &U, Matrix1D< double > &W, Matrix2D< double > &V, Matrix1D< int > &indexes) const
 
Matrix2D< T > inv () const
 
void selfInverse ()
 
bool isIdentity () const
 
std::ostream & operator<< (std::ostream &ostrm, const Matrix2D< T > &v)
 

Detailed Description

template<typename T>
class Matrix2D< T >

Matrix2D class

Definition at line 36 of file mask.h.

Constructor & Destructor Documentation

◆ Matrix2D() [1/4]

template<typename T>
Matrix2D< T >::Matrix2D ( )
inline

Empty constructor

Definition at line 423 of file matrix2d.h.

424  {
425  coreInit();
426  }
void coreInit()
Definition: matrix2d.cpp:971

◆ Matrix2D() [2/4]

template<typename T>
Matrix2D< T >::Matrix2D ( const FileName fnMappedMatrix,
int  Ydim,
int  Xdim,
size_t  offset = 0 
)
inline

Definition at line 428 of file matrix2d.h.

429  {
430  coreInit(fnMappedMatrix,Ydim,Xdim,offset);
431  }
size_t Xdim() const
Definition: matrix2d.h:575
void coreInit()
Definition: matrix2d.cpp:971
size_t Ydim() const
Definition: matrix2d.h:584

◆ Matrix2D() [3/4]

template<typename T>
Matrix2D< T >::Matrix2D ( int  Ydim,
int  Xdim 
)
inline

Dimension constructor

Definition at line 435 of file matrix2d.h.

436  {
437  coreInit();
438  initZeros(Ydim, Xdim);
439  }
size_t Xdim() const
Definition: matrix2d.h:575
void coreInit()
Definition: matrix2d.cpp:971
size_t Ydim() const
Definition: matrix2d.h:584
void initZeros()
Definition: matrix2d.h:626

◆ Matrix2D() [4/4]

template<typename T>
Matrix2D< T >::Matrix2D ( const Matrix2D< T > &  v)
inline

Copy constructor

Definition at line 443 of file matrix2d.h.

444  {
445  coreInit();
446  *this = v;
447  }
void coreInit()
Definition: matrix2d.cpp:971

◆ ~Matrix2D()

template<typename T>
Matrix2D< T >::~Matrix2D ( )
inline

Destructor.

Definition at line 451 of file matrix2d.h.

452  {
453  coreDeallocate();
454  }
void coreDeallocate()
Definition: matrix2d.cpp:1002

Member Function Documentation

◆ adaptForNumericalRecipes()

template<typename T >
T ** Matrix2D< T >::adaptForNumericalRecipes ( ) const

Produce a 2D array suitable for working with Numerical Recipes

This function must be used only as a preparation for routines which need that the first physical index is 1 and not 0 as it usually is in C. New memory is needed to hold the new double pointer array.

Definition at line 1280 of file matrix2d.cpp.

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 }
T * mdata
Definition: matrix2d.h:395
#define i
size_t mdimy
Definition: matrix2d.h:413
#define j
int m
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

◆ adaptForNumericalRecipes2()

template<typename T>
T* Matrix2D< T >::adaptForNumericalRecipes2 ( ) const
inline

Produce a 1D pointer suitable for working with Numerical Recipes (2)

This function meets the same goal as the one before, however this one work with 2D arrays as a single pointer. The first element of the array is pointed by result[1*Xdim+1], and in general result[i*Xdim+j]

Definition at line 875 of file matrix2d.h.

876  {
877  return mdata - 1 - mdimx;
878  }
T * mdata
Definition: matrix2d.h:395
size_t mdimx
Definition: matrix2d.h:410

◆ clear()

template<typename T>
void Matrix2D< T >::clear ( )
inline

Clear.

Definition at line 473 of file matrix2d.h.

474  {
475  coreDeallocate();
476  coreInit();
477  }
void coreInit()
Definition: matrix2d.cpp:971
void coreDeallocate()
Definition: matrix2d.cpp:1002

◆ colSum()

template<typename T>
void Matrix2D< T >::colSum ( Matrix1D< T > &  sum) const

Get column sum.

Definition at line 787 of file matrix2d.cpp.

788 {
789  sum.initZeros(MAT_XSIZE(*this));
791  VEC_ELEM(sum,j)+=MAT_ELEM(*this,i,j);
792 }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void initZeros()
Definition: matrix1d.h:592
#define j
#define MAT_XSIZE(m)
Definition: matrix2d.h:120

◆ computeColMeans()

template<typename T >
void Matrix2D< T >::computeColMeans ( Matrix1D< double > &  Xmr) const

Compute row means

Definition at line 613 of file matrix2d.cpp.

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  }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void initZeros()
Definition: matrix1d.h:592
#define j

◆ computeMax()

template<typename T >
T Matrix2D< T >::computeMax ( ) const

Maximum of the values in the array.

The returned value is of the same type as the type of the array.

Definition at line 1236 of file matrix2d.cpp.

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 }
T * mdata
Definition: matrix2d.h:395
size_t mdim
Definition: matrix2d.h:416
int * n

◆ computeMaxAndMin()

template<typename T>
void Matrix2D< T >::computeMaxAndMin ( T &  maxValue,
T &  minValue 
) const

Maximum and minimum of the values in the array.

Definition at line 1262 of file matrix2d.cpp.

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 }
T * mdata
Definition: matrix2d.h:395
size_t mdim
Definition: matrix2d.h:416
int * n

◆ computeMin()

template<typename T >
T Matrix2D< T >::computeMin ( ) const

Minimum of the values in the array.

The returned value is of the same type as the type of the array.

Definition at line 1249 of file matrix2d.cpp.

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 }
T * mdata
Definition: matrix2d.h:395
size_t mdim
Definition: matrix2d.h:416
int * n

◆ computeRowMeans()

template<typename T >
void Matrix2D< T >::computeRowMeans ( Matrix1D< double > &  Xmr) const

Compute row means

Definition at line 604 of file matrix2d.cpp.

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 }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void initZeros()
Definition: matrix1d.h:592
#define j
#define MAT_XSIZE(m)
Definition: matrix2d.h:120

◆ convertTo()

template<typename T>
void Matrix2D< T >::convertTo ( float  out[3][3]) const
inline

Method will convert Matrix2D matrix to float[3][3]

Definition at line 480 of file matrix2d.h.

480  {
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  }
#define i
#define j

◆ copyFromVector()

template<typename T>
void Matrix2D< T >::copyFromVector ( std::vector< T > &  v,
int  Xdim,
int  Ydim 
)
inline

Copy stl::vector to matrix

Definition at line 966 of file matrix2d.h.

967  {
968  if (mdimx!=Xdim || mdimy!=Ydim)
970  copy( v.begin(), v.begin()+v.size(), mdata);
971  }
size_t Xdim() const
Definition: matrix2d.h:575
T * mdata
Definition: matrix2d.h:395
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
size_t Ydim() const
Definition: matrix2d.h:584
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410

◆ copyToVector()

template<typename T>
void Matrix2D< T >::copyToVector ( std::vector< T > &  v)
inline

Copy matrix to stl::vector

Definition at line 960 of file matrix2d.h.

961  {
962  v.assign(mdata, mdata+mdim);
963  }
T * mdata
Definition: matrix2d.h:395
size_t mdim
Definition: matrix2d.h:416

◆ coreAllocate()

template<typename T >
void Matrix2D< T >::coreAllocate ( int  _mdimy,
int  _mdimx 
)

Core allocate.

Definition at line 982 of file matrix2d.cpp.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
There is not enough memory for allocation.
Definition: xmipp_error.h:166
char * mdataOriginal
Definition: matrix2d.h:407
T * mdata
Definition: matrix2d.h:395
void clear()
Definition: matrix2d.h:473
int fdMap
Definition: matrix2d.h:404
bool mappedData
Definition: matrix2d.h:401
size_t mdimy
Definition: matrix2d.h:413
size_t mdim
Definition: matrix2d.h:416
size_t mdimx
Definition: matrix2d.h:410

◆ coreDeallocate()

template<typename T >
void Matrix2D< T >::coreDeallocate ( )

Core deallocate. Free all mdata.

Definition at line 1002 of file matrix2d.cpp.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
bool destroyData
Definition: matrix2d.h:398
Global mmap error.
Definition: xmipp_error.h:170
char * mdataOriginal
Definition: matrix2d.h:407
T * mdata
Definition: matrix2d.h:395
int fdMap
Definition: matrix2d.h:404
bool mappedData
Definition: matrix2d.h:401
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410

◆ coreInit() [1/2]

template<typename T >
void Matrix2D< T >::coreInit ( const FileName fn,
int  Ydim,
int  Xdim,
size_t  offset = 0 
)

Core init from mapped file. Offset is in bytes.

Definition at line 75 of file matrix2d.cpp.

76 {
77 #ifdef XMIPP_MMAP
78 
79  mdimx=Xdim;
80  mdimy=Ydim;
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 }
size_t Xdim() const
Definition: matrix2d.h:575
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
bool destroyData
Definition: matrix2d.h:398
Global mmap error.
Definition: xmipp_error.h:170
char * mdataOriginal
Definition: matrix2d.h:407
T * mdata
Definition: matrix2d.h:395
String integerToString(int I, int _width, char fill_with)
Map addressing of file has failed.
Definition: xmipp_error.h:171
int fdMap
Definition: matrix2d.h:404
bool mappedData
Definition: matrix2d.h:401
size_t Ydim() const
Definition: matrix2d.h:584
size_t mdimy
Definition: matrix2d.h:413
File cannot be open.
Definition: xmipp_error.h:137
std::string String
Definition: xmipp_strings.h:34
size_t mdim
Definition: matrix2d.h:416
size_t mdimx
Definition: matrix2d.h:410

◆ coreInit() [2/2]

template<typename T >
void Matrix2D< T >::coreInit ( )

Core init. Initialize everything to 0

Definition at line 971 of file matrix2d.cpp.

972 {
973  mdimx=mdimy=mdim=0;
974  mdata=NULL;
975  mdataOriginal=NULL;
976  destroyData=true;
977  mappedData=false;
978  fdMap=-1;
979 }
bool destroyData
Definition: matrix2d.h:398
char * mdataOriginal
Definition: matrix2d.h:407
T * mdata
Definition: matrix2d.h:395
int fdMap
Definition: matrix2d.h:404
bool mappedData
Definition: matrix2d.h:401
size_t mdimy
Definition: matrix2d.h:413
size_t mdim
Definition: matrix2d.h:416
size_t mdimx
Definition: matrix2d.h:410

◆ det()

template<typename T >
T Matrix2D< T >::det ( ) const

Determinant of a matrix

An exception is thrown if the matrix is not squared or it is empty.

double det = m.det();

Definition at line 38 of file matrix2d.cpp.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
The matrix is empty.
Definition: xmipp_error.h:151
#define i
doublereal * d
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
Definition: mask.h:36
#define XMIPP_EQUAL_ACCURACY
Definition: xmipp_macros.h:119
size_t mdimy
Definition: matrix2d.h:413
#define j
void ludcmp(const Matrix2D< T > &A, Matrix2D< T > &LU, Matrix1D< int > &indx, T &d)
Definition: matrix2d.cpp:586
size_t mdimx
Definition: matrix2d.h:410

◆ det3x3()

template<typename T>
T Matrix2D< T >::det3x3 ( ) const
inline

determinat of 3x3 matrix

Definition at line 1061 of file matrix2d.h.

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  }
#define dMij(m, i, j)
Definition: matrix2d.h:139

◆ eigs()

template<typename T >
void Matrix2D< T >::eigs ( Matrix2D< double > &  U,
Matrix1D< double > &  W,
Matrix2D< double > &  V,
Matrix1D< int > &  indexes 
) const

Perform SVD decomposition and add an index vector with the descending order of singular values

Definition at line 719 of file matrix2d.cpp.

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 }
#define VEC_SWAP(v, i, j, aux)
Definition: matrix1d.h:248
void enumerate()
Definition: matrix1d.cpp:98
#define i
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
void svdcmp(const Matrix2D< T > &a, Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v)
Definition: matrix2d.cpp:125
#define dMi(v, i)
Definition: matrix1d.h:246
#define j
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458

◆ equal()

template<typename T>
bool Matrix2D< T >::equal ( const Matrix2D< T > &  op,
double  accuracy = XMIPP_EQUAL_ACCURACY 
) const

Equality.

Returns true if this object has got the same shape (origin and size) than the argument and the same values (within accuracy).

Definition at line 1202 of file matrix2d.cpp.

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 }
bool sameShape(const Matrix2D< T1 > &op) const
Definition: matrix2d.h:566
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t mdimy
Definition: matrix2d.h:413
#define j
size_t mdimx
Definition: matrix2d.h:410

◆ equalAbs()

template<typename T>
bool Matrix2D< T >::equalAbs ( const Matrix2D< T > &  op,
double  accuracy = XMIPP_EQUAL_ACCURACY 
) const

Equality.

Returns true if this object has got the same shape (origin and size) than the argument and the same values (within accuracy and without SIGN).

Definition at line 1219 of file matrix2d.cpp.

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 }
bool sameShape(const Matrix2D< T1 > &op) const
Definition: matrix2d.h:566
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t mdimy
Definition: matrix2d.h:413
#define j
size_t mdimx
Definition: matrix2d.h:410

◆ fromVector()

template<typename T>
void Matrix2D< T >::fromVector ( const Matrix1D< T > &  op1)

Makes a matrix from a vector

The origin of the matrix is set such that it has one of the index origins (X or Y) to the same value as the vector, and the other set to 0 according to the shape.

Definition at line 803 of file matrix2d.cpp.

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 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
size_t size() const
Definition: matrix1d.h:508
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
#define i
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void clear()
Definition: matrix2d.h:473
int isRow() const
Definition: matrix1d.h:520
size_t mdimy
Definition: matrix2d.h:413
#define j
size_t mdimx
Definition: matrix2d.h:410

◆ getCol()

template<typename T>
void Matrix2D< T >::getCol ( size_t  j,
Matrix1D< T > &  v 
) const

Get Column

This function returns a column vector corresponding to the chosen column.

std::vector< double > v;
m.getCol(-1, v);

Definition at line 890 of file matrix2d.cpp.

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 }
Index out of bounds.
Definition: xmipp_error.h:132
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
void clear()
Definition: matrix1d.cpp:67
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
#define i
void setCol()
Definition: matrix1d.h:554
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t mdimy
Definition: matrix2d.h:413
#define j
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
size_t mdimx
Definition: matrix2d.h:410

◆ getDiagonal()

template<typename T>
void Matrix2D< T >::getDiagonal ( Matrix1D< T > &  d) const

Get diagonal. It is assumed that the matrix is squared

Definition at line 949 of file matrix2d.cpp.

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 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458

◆ getRow()

template<typename T>
void Matrix2D< T >::getRow ( size_t  i,
Matrix1D< T > &  v 
) const

Get row

This function returns a row vector corresponding to the chosen row inside the nth 2D matrix, the numbering of the rows is also logical not physical.

std::vector< double > v;
m.getRow(-2, v);

Definition at line 871 of file matrix2d.cpp.

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 }
Index out of bounds.
Definition: xmipp_error.h:132
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
void clear()
Definition: matrix1d.cpp:67
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t mdimy
Definition: matrix2d.h:413
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
size_t mdimx
Definition: matrix2d.h:410
void setRow()
Definition: matrix1d.h:543

◆ getVal()

template<typename T>
T Matrix2D< T >::getVal ( int  y,
int  x 
) const
inline

Parenthesis operator for phyton

Definition at line 720 of file matrix2d.h.

721  {
722  return MAT_ELEM((*this),y,x);
723  }
static double * y
doublereal * x
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116

◆ initConstant() [1/2]

template<typename T>
void Matrix2D< T >::initConstant ( val)
inline

Same value in all components.

The constant must be of a type compatible with the array type, ie, you cannot assign a double to an integer array without a casting. It is not an error if the array is empty, then nothing is done.

v.initConstant(3.14);

Definition at line 602 of file matrix2d.h.

603  {
604  for (size_t j = 0; j < mdim; j++)
605  mdata[j] = val;
606  }
T * mdata
Definition: matrix2d.h:395
#define j
size_t mdim
Definition: matrix2d.h:416

◆ initConstant() [2/2]

template<typename T>
void Matrix2D< T >::initConstant ( size_t  Ydim,
size_t  Xdim,
val 
)
inline

Initialize to zeros with a given size.

Definition at line 610 of file matrix2d.h.

611  {
612  if (mdimx!=Xdim || mdimy!=Ydim)
614  initConstant(val);
615  }
size_t Xdim() const
Definition: matrix2d.h:575
void initConstant(T val)
Definition: matrix2d.h:602
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
size_t Ydim() const
Definition: matrix2d.h:584
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410

◆ initGaussian() [1/2]

template<typename T >
void Matrix2D< T >::initGaussian ( int  Ydim,
int  Xdim,
double  op1 = 0.,
double  op2 = 1. 
)

Initialize to gaussian numbers

Definition at line 1118 of file matrix2d.cpp.

1119 {
1120  initRandom(Ydim, Xdim, op1, op2, RND_GAUSSIAN);
1121 }
size_t Xdim() const
Definition: matrix2d.h:575
size_t Ydim() const
Definition: matrix2d.h:584
void initRandom(size_t Ydim, size_t Xdim, double op1, double op2, RandomMode mode=RND_UNIFORM)
Definition: matrix2d.cpp:1108

◆ initGaussian() [2/2]

template<typename T >
void Matrix2D< T >::initGaussian ( int  dim,
double  var 
)

2D gaussian matrix of a given size and with a given variance. The amplitude of the Gaussian is set to 1.

A (dim x dim) gaussian matrix is generated.

m.initGaussian(3,1);

Definition at line 1125 of file matrix2d.cpp.

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 }
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define j
void initZeros()
Definition: matrix2d.h:626

◆ initIdentity() [1/2]

template<typename T>
void Matrix2D< T >::initIdentity ( )
inline

2D Identity matrix of current size

If actually the matrix is not squared then an identity matrix is generated of size (Xdim x Xdim).

m.initIdentity();

Definition at line 673 of file matrix2d.h.

674  {
675  initIdentity(MAT_XSIZE(*this));
676  }
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
void initIdentity()
Definition: matrix2d.h:673

◆ initIdentity() [2/2]

template<typename T>
void Matrix2D< T >::initIdentity ( int  dim)
inline

2D Identity matrix of a given size

A (dim x dim) identity matrix is generated.

m.initIdentity(3);

Definition at line 686 of file matrix2d.h.

687  {
688  initZeros(dim, dim);
689  for (int i = 0; i < dim; i++)
690  MAT_ELEM(*this,i,i) = 1;
691  }
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void initZeros()
Definition: matrix2d.h:626

◆ initRandom()

template<typename T >
void Matrix2D< T >::initRandom ( size_t  Ydim,
size_t  Xdim,
double  op1,
double  op2,
RandomMode  mode = RND_UNIFORM 
)

Initialize to random random numbers, uniform or gaussian

Definition at line 1108 of file matrix2d.cpp.

1109 {
1110  if (mdimx!=Xdim || mdimy!=Ydim)
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 }
size_t Xdim() const
Definition: matrix2d.h:575
T * mdata
Definition: matrix2d.h:395
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
double rnd_unif()
size_t Ydim() const
Definition: matrix2d.h:584
void mode
size_t mdimy
Definition: matrix2d.h:413
#define j
double rnd_gaus()
size_t mdim
Definition: matrix2d.h:416
size_t mdimx
Definition: matrix2d.h:410

◆ initZeros() [1/3]

template<typename T>
void Matrix2D< T >::initZeros ( )
inline

Initialize to zeros with current size.

All values are set to 0. The current size and origin are kept. It is not an error if the array is empty, then nothing is done.

v.initZeros();

Definition at line 626 of file matrix2d.h.

627  {
628  memset(mdata,0,mdimx*mdimy*sizeof(T));
629  }
T * mdata
Definition: matrix2d.h:395
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410

◆ initZeros() [2/3]

template<typename T>
void Matrix2D< T >::initZeros ( size_t  Ydim,
size_t  Xdim 
)
inline

Initialize to zeros with a given size.

Definition at line 633 of file matrix2d.h.

634  {
635  if (mdimx!=Xdim || mdimy!=Ydim)
637  memset(mdata,0,mdimx*mdimy*sizeof(T));
638  }
size_t Xdim() const
Definition: matrix2d.h:575
T * mdata
Definition: matrix2d.h:395
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
size_t Ydim() const
Definition: matrix2d.h:584
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410

◆ initZeros() [3/3]

template<typename T>
template<typename T1 >
void Matrix2D< T >::initZeros ( const Matrix2D< T1 > &  op)
inline

Initialize to zeros following a pattern.

All values are set to 0, and the origin and size of the pattern are adopted.

v2.initZeros(v1);

Definition at line 650 of file matrix2d.h.

651  {
652  if (mdimx!=op.mdimx || mdimy!=op.mdimy)
653  resizeNoCopy(op);
654  memset(mdata,0,mdimx*mdimy*sizeof(T));
655  }
T * mdata
Definition: matrix2d.h:395
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410

◆ inv() [1/2]

template<typename T>
void Matrix2D< T >::inv ( Matrix2D< T > &  result) const

Inverse of a matrix

The matrix is inverted using a SVD decomposition. In fact the pseudoinverse is returned.

m1.inv(m1_inv);

Definition at line 663 of file matrix2d.cpp.

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 }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define SPEED_UP_temps0
Definition: xmipp_macros.h:394
The matrix is empty.
Definition: xmipp_error.h:151
doublereal * w
#define i
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
#define M4x4_INV(Ainv, A)
Definition: matrix2d.h:323
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
#define M3x3_INV(Ainv, A)
Definition: matrix2d.h:302
size_t mdimy
Definition: matrix2d.h:413
void svdcmp(const Matrix2D< T > &a, Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v)
Definition: matrix2d.cpp:125
#define j
void initZeros()
Definition: matrix2d.h:626
doublereal * u
size_t mdimx
Definition: matrix2d.h:410
T computeMax() const
Definition: matrix2d.cpp:1236
#define M2x2_INV(Ainv, A)
Definition: matrix2d.h:286

◆ inv() [2/2]

template<typename T>
Matrix2D<T> Matrix2D< T >::inv ( ) const
inline

Inverse of a matrix

Definition at line 1136 of file matrix2d.h.

1137  {
1138  Matrix2D<T> result;
1139  inv(result);
1140 
1141  return result;
1142  }
Definition: mask.h:36
Matrix2D< T > inv() const
Definition: matrix2d.h:1136

◆ invAlgLib()

template<typename T>
void Matrix2D< T >::invAlgLib ( Matrix2D< T > &  result,
bool  use_lu = false 
) const

Inverse of a matrix

The matrix is inverted using a AlgLib. Set LU to use LU decomposition

m1.inv(m1_inv);

◆ isIdentity()

template<typename T >
bool Matrix2D< T >::isIdentity ( ) const

True if the matrix is identity

if (m.isIdentity())
std::cout << "The matrix is identity\n";

Definition at line 1323 of file matrix2d.cpp.

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 }
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t mdimy
Definition: matrix2d.h:413
#define j
size_t mdimx
Definition: matrix2d.h:410

◆ killAdaptationForNumericalRecipes()

template<typename T>
void Matrix2D< T >::killAdaptationForNumericalRecipes ( T **  m) const
inline

Kill a 2D array produced for numerical recipes

The allocated memory is freed.

Definition at line 888 of file matrix2d.h.

889  {
890  free_Tmatrix(m, 1, mdimy, 1, mdimx);
891  }
void free_Tmatrix(T **&m, int nrl, int nrh, int ncl, int nch)
Definition: xmipp_memory.h:61
size_t mdimy
Definition: matrix2d.h:413
int m
size_t mdimx
Definition: matrix2d.h:410

◆ killAdaptationForNumericalRecipes2()

template<typename T>
void Matrix2D< T >::killAdaptationForNumericalRecipes2 ( T **  m) const
inline

Kill a 2D array produced for numerical recipes, 2.

Nothing needs to be done.

Definition at line 897 of file matrix2d.h.

898  {}

◆ loadFromNumericalRecipes()

template<typename T>
void Matrix2D< T >::loadFromNumericalRecipes ( T **  m,
int  Ydim,
int  Xdim 
)

Load 2D array from numerical recipes result.

Definition at line 1293 of file matrix2d.cpp.

1294 {
1295  if (mdimx!=Xdim || mdimy!=Ydim)
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 }
size_t Xdim() const
Definition: matrix2d.h:575
#define i
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t Ydim() const
Definition: matrix2d.h:584
size_t mdimy
Definition: matrix2d.h:413
#define j
int m
size_t mdimx
Definition: matrix2d.h:410

◆ mapToFile()

template<typename T >
void Matrix2D< T >::mapToFile ( const FileName fn,
int  Ydim,
int  Xdim,
size_t  offset = 0 
)

Map to file. The matrix is mapped to a file. The file is presumed to be already created with enough space for a matrix of size Ydim x Xdim. Offset is in bytes.

Definition at line 1079 of file matrix2d.cpp.

1080 {
1081  if (mdata!=NULL)
1082  clear();
1083 
1084 #ifdef XMIPP_MMAP
1085 
1086  coreInit(fn,Ydim,Xdim,offset);
1087 #else
1088 
1090 #endif
1091 
1092 }
size_t Xdim() const
Definition: matrix2d.h:575
void coreInit()
Definition: matrix2d.cpp:971
T * mdata
Definition: matrix2d.h:395
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
void clear()
Definition: matrix2d.h:473
size_t Ydim() const
Definition: matrix2d.h:584

◆ norm()

template<typename T>
double Matrix2D< T >::norm ( )
inline

Frobenius norm of a matrix

Definition at line 1074 of file matrix2d.h.

1075  {
1076  double sum=0.;
1078  {
1079  T aux=MAT_ELEM(*this,i,j);
1080  sum+=aux*aux;
1081  }
1082  return sqrt(sum);
1083  }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
void sqrt(Image< double > &op)
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define j

◆ operator()()

template<typename T>
T& Matrix2D< T >::operator() ( int  i,
int  j 
) const
inline

Matrix element access

Definition at line 708 of file matrix2d.h.

709  {
710  return MAT_ELEM((*this),i,j);
711  }
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define j

◆ operator*() [1/3]

template<typename T>
Matrix2D<T> Matrix2D< T >::operator* ( op1) const
inline

v3 = v1 * k.

Definition at line 727 of file matrix2d.h.

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  }
T * mdata
Definition: matrix2d.h:395
#define i
Definition: mask.h:36
size_t mdim
Definition: matrix2d.h:416

◆ operator*() [2/3]

template<typename T>
Matrix1D< T > Matrix2D< T >::operator* ( const Matrix1D< T > &  op1) const

Matrix by vector multiplication

v2 = A*v1;

Definition at line 759 of file matrix2d.cpp.

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 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
Matrix error.
Definition: xmipp_error.h:149
#define i
void setCol()
Definition: matrix1d.h:554
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t mdimy
Definition: matrix2d.h:413
int isCol() const
Definition: matrix1d.h:532
void initZeros()
Definition: matrix1d.h:592
#define j
Definition: ctf.h:38
size_t mdimx
Definition: matrix2d.h:410

◆ operator*() [3/3]

template<typename T>
Matrix2D< T > Matrix2D< T >::operator* ( const Matrix2D< T > &  op1) const

Matrix by Matrix multiplication

C = A*B;

Definition at line 1135 of file matrix2d.cpp.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
#define i
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
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
Definition: mask.h:36
size_t mdimy
Definition: matrix2d.h:413
#define j
void initZeros()
Definition: matrix2d.h:626
size_t mdimx
Definition: matrix2d.h:410

◆ operator*=()

template<typename T>
void Matrix2D< T >::operator*= ( op1)
inline

v3 *= k.

Definition at line 757 of file matrix2d.h.

758  {
759  for (size_t i=0; i < mdim; i++)
760  mdata[i] *= op1;
761  }
T * mdata
Definition: matrix2d.h:395
#define i
size_t mdim
Definition: matrix2d.h:416

◆ operator+()

template<typename T>
Matrix2D< T > Matrix2D< T >::operator+ ( const Matrix2D< T > &  op1) const

Matrix summation

C = A + B;

Definition at line 1150 of file matrix2d.cpp.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
#define i
Definition: mask.h:36
size_t mdimy
Definition: matrix2d.h:413
#define j
void initZeros()
Definition: matrix2d.h:626
size_t mdimx
Definition: matrix2d.h:410

◆ operator+=()

template<typename T>
void Matrix2D< T >::operator+= ( const Matrix2D< T > &  op1) const

Matrix summation

A += B;

Definition at line 1165 of file matrix2d.cpp.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t mdimy
Definition: matrix2d.h:413
#define j
size_t mdimx
Definition: matrix2d.h:410

◆ operator-()

template<typename T>
Matrix2D< T > Matrix2D< T >::operator- ( const Matrix2D< T > &  op1) const

Matrix subtraction

C = A - B;

Definition at line 1176 of file matrix2d.cpp.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
#define i
Definition: mask.h:36
size_t mdimy
Definition: matrix2d.h:413
#define j
void initZeros()
Definition: matrix2d.h:626
size_t mdimx
Definition: matrix2d.h:410

◆ operator-=()

template<typename T>
void Matrix2D< T >::operator-= ( const Matrix2D< T > &  op1) const

Matrix subtraction

A -= B;

Definition at line 1191 of file matrix2d.cpp.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t mdimy
Definition: matrix2d.h:413
#define j
size_t mdimx
Definition: matrix2d.h:410

◆ operator/()

template<typename T>
Matrix2D<T> Matrix2D< T >::operator/ ( op1) const
inline

v3 = v1 / k.

Definition at line 737 of file matrix2d.h.

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  }
T * mdata
Definition: matrix2d.h:395
#define i
Definition: mask.h:36
size_t mdim
Definition: matrix2d.h:416

◆ operator/=()

template<typename T>
void Matrix2D< T >::operator/= ( op1)
inline

v3 /= k.

Definition at line 765 of file matrix2d.h.

766  {
767  for (size_t i=0; i < mdim; i++)
768  mdata[i] /= op1;
769  }
T * mdata
Definition: matrix2d.h:395
#define i
size_t mdim
Definition: matrix2d.h:416

◆ operator=()

template<typename T>
Matrix2D< T > & Matrix2D< T >::operator= ( const Matrix2D< T > &  op1)

Assignment.

You can build as complex assignment expressions as you like. Multiple assignment is allowed.

v1 = v2 + v3;
v1 = v2 = v3;

Definition at line 957 of file matrix2d.cpp.

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 }
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
T * mdata
Definition: matrix2d.h:395
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
size_t mdim
Definition: matrix2d.h:416

◆ read()

template<typename T >
void Matrix2D< T >::read ( const FileName fn)

Read this matrix from file. The matrix is assumed to be already resized.

Definition at line 101 of file matrix2d.cpp.

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 }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
File or directory does not exist.
Definition: xmipp_error.h:136
#define j

◆ resize() [1/2]

template<typename T >
void Matrix2D< T >::resize ( size_t  Ydim,
size_t  Xdim,
bool  noCopy = false 
)

Resize to a given size

Definition at line 1022 of file matrix2d.cpp.

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 }
size_t Xdim() const
Definition: matrix2d.h:575
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
There is not enough memory for allocation.
Definition: xmipp_error.h:166
T * mdata
Definition: matrix2d.h:395
#define i
void clear()
Definition: matrix2d.h:473
bool mappedData
Definition: matrix2d.h:401
size_t Ydim() const
Definition: matrix2d.h:584
size_t mdimy
Definition: matrix2d.h:413
void coreDeallocate()
Definition: matrix2d.cpp:1002
#define j
size_t mdim
Definition: matrix2d.h:416
size_t mdimx
Definition: matrix2d.h:410
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

◆ resize() [2/2]

template<typename T>
template<typename T1 >
void Matrix2D< T >::resize ( const Matrix2D< T1 > &  v)
inline

Resize according to a pattern.

This function resize the actual array to the same size and origin as the input pattern. If the actual array is larger than the pattern then the trailing values are lost, if it is smaller then 0's are added at the end

v2.resize(v1);
// v2 has got now the same structure as v1

Definition at line 526 of file matrix2d.h.

527  {
528  if (mdimx != v.mdimx || mdimy != v.mdimy)
529  resize(v.mdimy, v.mdimx);
530  }
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410
void resize(size_t Ydim, size_t Xdim, bool noCopy=false)
Definition: matrix2d.cpp:1022

◆ resizeNoCopy() [1/2]

template<typename T>
void Matrix2D< T >::resizeNoCopy ( int  Ydim,
int  Xdim 
)
inline

Resize to a given size (don't copy old elements)

Definition at line 534 of file matrix2d.h.

535  {
536  resize(Ydim, Xdim, true);
537  }
size_t Xdim() const
Definition: matrix2d.h:575
size_t Ydim() const
Definition: matrix2d.h:584
void resize(size_t Ydim, size_t Xdim, bool noCopy=false)
Definition: matrix2d.cpp:1022

◆ resizeNoCopy() [2/2]

template<typename T>
template<typename T1 >
void Matrix2D< T >::resizeNoCopy ( const Matrix2D< T1 > &  v)
inline

Resize according to a pattern. Do not copy old elements.

Definition at line 543 of file matrix2d.h.

544  {
545  if (mdimx != v.mdimx || mdimy != v.mdimy)
546  resize(v.mdimy, v.mdimx, true);
547  }
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410
void resize(size_t Ydim, size_t Xdim, bool noCopy=false)
Definition: matrix2d.cpp:1022

◆ rowEnergySum()

template<typename T>
void Matrix2D< T >::rowEnergySum ( Matrix1D< T > &  sum) const

Get row energy sum. Sum of the squared values by row

Definition at line 795 of file matrix2d.cpp.

796 {
797  sum.initZeros(MAT_YSIZE(*this));
799  VEC_ELEM(sum,i)+=MAT_ELEM(*this,i,j)*MAT_ELEM(*this,i,j);
800 }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void initZeros()
Definition: matrix1d.h:592
#define j

◆ rowSum()

template<typename T>
void Matrix2D< T >::rowSum ( Matrix1D< T > &  sum) const

Get row sum.

Definition at line 779 of file matrix2d.cpp.

780 {
781  sum.initZeros(MAT_YSIZE(*this));
783  VEC_ELEM(sum,i)+=MAT_ELEM(*this,i,j);
784 }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void initZeros()
Definition: matrix1d.h:592
#define j

◆ sameShape()

template<typename T>
template<typename T1 >
bool Matrix2D< T >::sameShape ( const Matrix2D< T1 > &  op) const
inline

Same shape.

Returns true if this object has got the same shape (origin and size) than the argument

Definition at line 566 of file matrix2d.h.

567  {
568  return ((mdimx == op.mdimx) && (mdimy == op.mdimy));
569  }
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410

◆ selfInverse()

template<typename T>
void Matrix2D< T >::selfInverse ( )
inline

Inverse the current matrix

Definition at line 1146 of file matrix2d.h.

1147  {
1148  Matrix2D<T> auxMatrix(*this);
1149  auxMatrix.inv(*this);
1150  }
Definition: mask.h:36

◆ setCol()

template<typename T>
void Matrix2D< T >::setCol ( size_t  j,
const Matrix1D< T > &  v 
)

Set Column

This function sets a column vector corresponding to the chosen column inside matrix.

m.setCol(0, (m.row(1)).transpose()); // Copies row 1 in column 0

Definition at line 929 of file matrix2d.cpp.

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 }
Index out of bounds.
Definition: xmipp_error.h:132
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
The matrix is empty.
Definition: xmipp_error.h:151
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
Problem with matrix dimensions.
Definition: xmipp_error.h:150
size_t mdimy
Definition: matrix2d.h:413
int isCol() const
Definition: matrix1d.h:532
#define j
size_t mdimx
Definition: matrix2d.h:410

◆ setConstantCol()

template<typename T>
void Matrix2D< T >::setConstantCol ( size_t  j,
v 
)
inline

Set constant column. Set a given column to a constant value A(i,j)=val;

Definition at line 1028 of file matrix2d.h.

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  }
Index out of bounds.
Definition: xmipp_error.h:132
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
The matrix is empty.
Definition: xmipp_error.h:151
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
size_t mdimy
Definition: matrix2d.h:413
#define j
size_t mdimx
Definition: matrix2d.h:410

◆ setRow()

template<typename T>
void Matrix2D< T >::setRow ( size_t  i,
const Matrix1D< T > &  v 
)

Set Row

This function sets a row vector corresponding to the chosen row in the 2D Matrix

m.setRow(-2, m.row(1)); // Copies row 1 in row -2

Definition at line 910 of file matrix2d.cpp.

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 }
Index out of bounds.
Definition: xmipp_error.h:132
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
The matrix is empty.
Definition: xmipp_error.h:151
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
int isRow() const
Definition: matrix1d.h:520
Problem with matrix dimensions.
Definition: xmipp_error.h:150
size_t mdimy
Definition: matrix2d.h:413
size_t mdimx
Definition: matrix2d.h:410

◆ setVal()

template<typename T>
void Matrix2D< T >::setVal ( val,
int  y,
int  x 
)
inline

Parenthesis operator for phyton

Definition at line 714 of file matrix2d.h.

715  {
716  MAT_ELEM((*this),y,x)=val;
717  }
static double * y
doublereal * x
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116

◆ submatrix()

template<typename T >
void Matrix2D< T >::submatrix ( int  i0,
int  j0,
int  iF,
int  jF 
)

Extract submatrix and assign to this object.

Definition at line 1095 of file matrix2d.cpp.

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 }
#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
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
Definition: mask.h:36
#define j
#define MAT_XSIZE(m)
Definition: matrix2d.h:120

◆ svd()

template<typename T>
void Matrix2D< T >::svd ( Matrix2D< double > &  U,
Matrix1D< double > &  W,
Matrix2D< double > &  V 
) const
inline

Perform SVD decomposition *this = U * W * V^t

Definition at line 1124 of file matrix2d.h.

1125  {
1126  svdcmp(*this, U, W, V);
1127  }
void svdcmp(const Matrix2D< T > &a, Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v)
Definition: matrix2d.cpp:125

◆ toVector()

template<typename T>
void Matrix2D< T >::toVector ( Matrix1D< T > &  op1) const

Makes a vector from a matrix

An exception is thrown if the matrix is not a single row or a single column. The origin of the vector is set according to the one of the matrix.

m.toVector(v);

Definition at line 832 of file matrix2d.cpp.

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 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
void clear()
Definition: matrix1d.cpp:67
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
#define i
void setCol()
Definition: matrix1d.h:554
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
Problem with matrix dimensions.
Definition: xmipp_error.h:150
size_t mdimy
Definition: matrix2d.h:413
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
size_t mdimx
Definition: matrix2d.h:410
void setRow()
Definition: matrix1d.h:543

◆ trace()

template<typename T >
T Matrix2D< T >::trace ( ) const

Trace Sum of the values in the diagonal

Definition at line 1304 of file matrix2d.cpp.

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 }
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
void min(Image< double > &op1, const Image< double > &op2)
#define i
doublereal * d
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define MAT_XSIZE(m)
Definition: matrix2d.h:120

◆ transpose()

template<typename T >
Matrix2D< T > Matrix2D< T >::transpose ( ) const

Algebraic transpose of a Matrix

You can use the transpose in as complex expressions as you like. The origin of the vector is not changed.

v2 = v1.transpose();

Definition at line 1314 of file matrix2d.cpp.

1315 {
1316  Matrix2D<T> result(mdimx, mdimy);
1318  MAT_ELEM(result,i,j) = MAT_ELEM((*this),j,i);
1319  return result;
1320 }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
Definition: mask.h:36
size_t mdimy
Definition: matrix2d.h:413
#define j
size_t mdimx
Definition: matrix2d.h:410

◆ write()

template<typename T >
void Matrix2D< T >::write ( const FileName fn) const

Write this matrix to file

Definition at line 113 of file matrix2d.cpp.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
File cannot be open.
Definition: xmipp_error.h:137

◆ Xdim()

template<typename T>
size_t Matrix2D< T >::Xdim ( ) const
inline

X dimension

Returns X dimension

Definition at line 575 of file matrix2d.h.

576  {
577  return mdimx;
578  }
size_t mdimx
Definition: matrix2d.h:410

◆ Ydim()

template<typename T>
size_t Matrix2D< T >::Ydim ( ) const
inline

Y dimension

Returns Y dimension

Definition at line 584 of file matrix2d.h.

585  {
586  return mdimy;
587  }
size_t mdimy
Definition: matrix2d.h:413

Friends And Related Function Documentation

◆ operator*

template<typename T>
Matrix2D<T> operator* ( op1,
const Matrix2D< T > &  op2 
)
friend

v3 = k * v2.

Definition at line 747 of file matrix2d.h.

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  }
T * mdata
Definition: matrix2d.h:395
#define i
Definition: mask.h:36
size_t mdim
Definition: matrix2d.h:416

◆ operator<<

template<typename T>
std::ostream& operator<< ( std::ostream &  ostrm,
const Matrix2D< T > &  v 
)
friend

Show matrix

Definition at line 910 of file matrix2d.h.

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  }
size_t Xdim() const
Definition: matrix2d.h:575
int bestPrecision(float F, int _width)
String floatToString(float F, int _width, int _prec)
#define i
size_t Ydim() const
Definition: matrix2d.h:584
#define j
T computeMax() const
Definition: matrix2d.cpp:1236

Member Data Documentation

◆ destroyData

template<typename T>
bool Matrix2D< T >::destroyData

Definition at line 398 of file matrix2d.h.

◆ fdMap

template<typename T>
int Matrix2D< T >::fdMap

Definition at line 404 of file matrix2d.h.

◆ mappedData

template<typename T>
bool Matrix2D< T >::mappedData

Definition at line 401 of file matrix2d.h.

◆ mdata

template<typename T>
T* Matrix2D< T >::mdata

Definition at line 395 of file matrix2d.h.

◆ mdataOriginal

template<typename T>
char* Matrix2D< T >::mdataOriginal

Definition at line 407 of file matrix2d.h.

◆ mdim

template<typename T>
size_t Matrix2D< T >::mdim

Definition at line 416 of file matrix2d.h.

◆ mdimx

template<typename T>
size_t Matrix2D< T >::mdimx

Definition at line 410 of file matrix2d.h.

◆ mdimy

template<typename T>
size_t Matrix2D< T >::mdimy

Definition at line 413 of file matrix2d.h.


The documentation for this class was generated from the following files: