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

#include <ctf.h>

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

Public Member Functions

bool isAnyNaN ()
 
Constructors
 Matrix1D (bool column=true)
 
template<class T1 >
 Matrix1D (T1 dim, bool column=true)
 
 Matrix1D (const Matrix1D< T > &v)
 
 ~Matrix1D ()
 
Matrix1D< T > & operator= (const Matrix1D< T > &op1)
 
bool operator== (const Matrix1D< T > &op1) const
 
Matrix1D< T > & operator= (const std::vector< T > &op1)
 
void clear ()
 
void coreInit ()
 
void coreAllocate (int _vdim)
 
void coreDeallocate ()
 
Size and shape of Matrix1D
void resize (size_t Xdim, bool copy=true)
 
void resizeNoCopy (int Xdim)
 
template<typename T1 >
void resize (const Matrix1D< T1 > &v)
 
template<typename T1 >
void resizeNoCopy (const Matrix1D< T1 > &v)
 
template<typename T1 >
bool sameShape (const Matrix1D< T1 > &op) const
 
size_t size () const
 
int isRow () const
 
int isCol () const
 
void setRow ()
 
void setCol ()
 
Initialization of Matrix1D values
void initConstant (T val)
 
void initConstant (size_t Xdim, T val)
 
void enumerate ()
 
void initZeros ()
 
void initZeros (size_t Xdim)
 
template<typename T1 >
void initZeros (const Matrix1D< T1 > &op)
 

Public Attributes

T * vdata
 The array itself. More...
 
bool destroyData
 Destroy data. More...
 
size_t vdim
 Number of elements. More...
 
bool row
 <0=column vector (default), 1=row vector More...
 

Matrix1D operators

Matrix1D< T > operator* (T op1) const
 
Matrix1D< T > operator/ (T op1) const
 
Matrix1D< T > operator+ (T op1) const
 
Matrix1D< T > operator- (T op1) const
 
void operator+= (const Matrix1D< T > &op1) const
 
void operator-= (const Matrix1D< T > &op1) const
 
void operator*= (T op1)
 
void operator/= (T op1)
 
void operator+= (T op1)
 
void operator-= (T op1)
 
Matrix1D< T > operator* (const Matrix1D< T > &op1) const
 
Matrix1D< T > operator/ (const Matrix1D< T > &op1) const
 
Matrix1D< T > operator+ (const Matrix1D< T > &op1) const
 
Matrix1D< T > operator- (const Matrix1D< T > &op1) const
 
void operator*= (const Matrix1D< T > &op1)
 
void operator/= (const Matrix1D< T > &op1)
 
void operator+= (const Matrix1D< T > &op1)
 
void operator-= (const Matrix1D< T > &op1)
 
Matrix1D< T > operator- () const
 
Matrix1D< T > operator* (const Matrix2D< T > &M)
 
T & operator() (int i) const
 
T & operator[] (int idx)
 
operator[] (int idx) const
 
Matrix1D< T > operator* (T op1, const Matrix1D< T > &op2)
 
Matrix1D< T > operator/ (T op1, const Matrix1D< T > &op2)
 
Matrix1D< T > operator+ (T op1, const Matrix1D< T > &op2)
 
Matrix1D< T > operator- (T op1, const Matrix1D< T > &op2)
 

Utilities for Matrix1D

T * adaptForNumericalRecipes () const
 
void killAdaptationForNumericalRecipes (T *m) const
 
void selfCEIL ()
 
void selfFLOOR ()
 
void selfROUND ()
 
Matrix1D< T > sort () const
 
void indexSort (Matrix1D< int > &indx) const
 
double computeMean () const
 
computeMax () const
 
void computeMinMax (T &minval, T &maxval) const
 
void computeMeanAndStddev (double &mean, double &stddev) const
 
computeMedian () const
 
int maxIndex () const
 
int minIndex () const
 
Matrix1D< T > transpose () const
 
void selfTranspose ()
 
double sum (bool average=false) const
 
double sum2 () const
 
double dotProduct (const Matrix1D< T > &op1) const
 
double module () const
 
double angle () const
 
void selfNormalize ()
 
void selfReverse ()
 
void showWithGnuPlot (const std::string &xlabel, const std::string &title)
 
void numericalDerivative (Matrix1D< double > &result) const
 
void read (const FileName &fn)
 
void write (const FileName &fn) const
 
void edit ()
 
std::istream & operator>> (std::istream &in, Matrix1D< T > &v)
 
std::ostream & operator<< (std::ostream &ostrm, const Matrix1D< T > &v)
 

Detailed Description

template<typename T>
class Matrix1D< T >

Matrix1D class.

Definition at line 38 of file ctf.h.

Constructor & Destructor Documentation

◆ Matrix1D() [1/3]

template<typename T>
Matrix1D< T >::Matrix1D ( bool  column = true)
inline

Empty constructor

The empty constructor creates a vector with no memory associated, origin=0, size=0, no statistics, ... You can choose between a column vector (by default), or a row one.

// both are examples of empty column vectors
// empty row vector

Definition at line 286 of file matrix1d.h.

287  {
288  coreInit();
289  row = !column;
290  }
void coreInit()
Definition: matrix1d.cpp:74
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267

◆ Matrix1D() [2/3]

template<typename T>
template<class T1 >
Matrix1D< T >::Matrix1D ( T1  dim,
bool  column = true 
)
inline

Dimension constructor

The dimension constructor creates a vector with memory associated (but not assigned to anything, could be full of garbage) origin=0, size=the given one. You can choose between a column vector (by default), or a row one.

// both are examples of column vectors of dimensions 6
// empty row vector

Definition at line 309 of file matrix1d.h.

310  {
311  coreInit();
312  row = !column;
313  initZeros(dim);
314  }
void coreInit()
Definition: matrix1d.cpp:74
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267
void initZeros()
Definition: matrix1d.h:592

◆ Matrix1D() [3/3]

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

Copy constructor

The created vector is a perfect copy of the input vector but with a different memory assignment.

Definition at line 325 of file matrix1d.h.

326  {
327  coreInit();
328  *this = v;
329  row = v.row;
330  }
void coreInit()
Definition: matrix1d.cpp:74
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267

◆ ~Matrix1D()

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

Destructor.

Definition at line 334 of file matrix1d.h.

335  {
336  coreDeallocate();
337  }
void coreDeallocate()
Definition: matrix1d.h:389

Member Function Documentation

◆ adaptForNumericalRecipes()

template<typename T>
T* Matrix1D< T >::adaptForNumericalRecipes ( ) const
inline

Produce a vector 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. In fact the vector provided for Numerical recipes is exactly this same one but with the indexes changed.

This function is not ported to Python.

Definition at line 844 of file matrix1d.h.

845  {
846  return MATRIX1D_ARRAY(*this) - 1;
847  }
#define MATRIX1D_ARRAY(v)
Definition: matrix1d.h:58

◆ angle()

template<typename T>
double Matrix1D< T >::angle ( ) const
inline

Angle of the vector

Supposing this vector is in R2 this function returns the angle of this vector with X axis, ie, atan2(YY(v), XX(v))

Definition at line 993 of file matrix1d.h.

994  {
995  return atan2((double) YY(*this), (double) XX(*this));
996  }
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93

◆ clear()

template<typename T >
void Matrix1D< T >::clear ( )

Clear.

Definition at line 67 of file matrix1d.cpp.

68 {
70  coreInit();
71 }
void coreInit()
Definition: matrix1d.cpp:74
void coreDeallocate()
Definition: matrix1d.h:389

◆ computeMax()

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

Maximum element

Definition at line 558 of file matrix1d.cpp.

559 {
560  if (vdim == 0)
561  return 0;
562 
563  T maxval = VEC_ELEM(*this,0);
564  for (size_t j = 0; j < vdim; ++j)
565  if (VEC_ELEM(*this,j) > maxval)
566  maxval = VEC_ELEM(*this,j);
567  return maxval;
568 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define j
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ computeMean()

template<typename T >
double Matrix1D< T >::computeMean ( ) const

Mean value of the vector

Definition at line 546 of file matrix1d.cpp.

547 {
548  if (vdim == 0)
549  return 0;
550 
551  double sum = 0;
552  for (size_t j = 0; j < vdim; ++j)
553  sum+=VEC_ELEM(*this,j);
554  return sum/vdim;
555 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
double sum(bool average=false) const
Definition: matrix1d.cpp:652
#define j
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ computeMeanAndStddev()

template<typename T >
void Matrix1D< T >::computeMeanAndStddev ( double &  mean,
double &  stddev 
) const

Mean and stddev value of the vector

Definition at line 588 of file matrix1d.cpp.

589 {
590  mean=stddev=0;
591  if (vdim == 0)
592  return;
593 
594  double sum = 0, sum2 = 0;
595  for (size_t j = 0; j < vdim; ++j)
596  {
597  double val=VEC_ELEM(*this,j);
598  sum+=val;
599  sum2+=val*val;
600  }
601  mean=sum/vdim;
602  stddev=sum2/vdim-mean*mean;
603  if (stddev<0)
604  stddev=0;
605  else
606  stddev=sqrt(stddev);
607 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
void sqrt(Image< double > &op)
double sum2() const
Definition: matrix1d.cpp:673
double sum(bool average=false) const
Definition: matrix1d.cpp:652
#define j
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ computeMedian()

template<typename T >
T Matrix1D< T >::computeMedian ( ) const

Median of the vector

Definition at line 842 of file matrix1d.cpp.

843 {
844  Matrix1D<T> aux;
845  aux=*this;
846  std::nth_element(aux.vdata,aux.vdata+vdim/2,aux.vdata+vdim);
847  return VEC_ELEM(aux,vdim/2);
848 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
Definition: ctf.h:38
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ computeMinMax()

template<typename T>
void Matrix1D< T >::computeMinMax ( T &  minval,
T &  maxval 
) const

Minimum and maximum element

Definition at line 571 of file matrix1d.cpp.

572 {
573  if (vdim == 0)
574  return;
575 
576  maxval = minval = VEC_ELEM(*this,0);
577  for (size_t j = 0; j < vdim; ++j)
578  {
579  T val=VEC_ELEM(*this,j);
580  if (val > maxval)
581  maxval = val;
582  else if (val<minval)
583  minval = val;
584  }
585 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define j
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ coreAllocate()

template<typename T>
void Matrix1D< T >::coreAllocate ( int  _vdim)
inline

Core allocate.

Definition at line 370 of file matrix1d.h.

371  {
372  if (_vdim <= 0)
373  {
374  clear();
375  return;
376  }
377 
378  vdim = _vdim;
379  vdata = new T[vdim];
380  memset(vdata, 0, vdim * sizeof(T));
381  if (vdata == NULL
382  )
383  REPORT_ERROR(ERR_MEM_NOTENOUGH, "Allocate: No space left");
384  }
void clear()
Definition: matrix1d.cpp:67
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
There is not enough memory for allocation.
Definition: xmipp_error.h:166
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ coreDeallocate()

template<typename T>
void Matrix1D< T >::coreDeallocate ( )
inline

Core deallocate. Free all vdata.

Definition at line 389 of file matrix1d.h.

390  {
391  if (vdata != NULL && destroyData)
392  delete[] vdata;
393  vdata = NULL;
394  }
T * vdata
The array itself.
Definition: matrix1d.h:258
bool destroyData
Destroy data.
Definition: matrix1d.h:261

◆ coreInit()

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

Core init. Initialize everything to 0

Definition at line 74 of file matrix1d.cpp.

75 {
76  vdim = 0;
77  row = false;
78  vdata = NULL;
79  destroyData = true;
80 }
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267
T * vdata
The array itself.
Definition: matrix1d.h:258
bool destroyData
Destroy data.
Definition: matrix1d.h:261
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ dotProduct()

template<typename T>
double Matrix1D< T >::dotProduct ( const Matrix1D< T > &  op1) const

Dot product <*this,op1>.

Definition at line 704 of file matrix1d.cpp.

705 {
706  double sum = 0;
707  const T *ptr1 = &VEC_ELEM(*this,0);
708  const T *ptr2 = &VEC_ELEM(op1,0);
709  size_t iBlockMax = vdim / 4;
710  for (size_t i = 0; i < iBlockMax; i++)
711  {
712  sum += (*ptr1++) * (*ptr2++);
713  sum += (*ptr1++) * (*ptr2++);
714  sum += (*ptr1++) * (*ptr2++);
715  sum += (*ptr1++) * (*ptr2++);
716  }
717  for (size_t i = iBlockMax * 4; i < vdim; ++i)
718  sum += (*ptr1++) * (*ptr2++);
719  return sum;
720 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
double sum(bool average=false) const
Definition: matrix1d.cpp:652
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ edit()

template<typename T >
void Matrix1D< T >::edit ( )

Edit with xmipp_editor.

This function generates a random filename starting with PPP and edits it with xmipp_editor. After closing the editor the file is removed.

Definition at line 823 of file matrix1d.cpp.

824 {
825  FileName nam;
826  nam.initRandom(15);
827 
828  nam = static_cast<std::string>("PPP" + nam + ".txt");
829  write
830  (nam);
831 
832  auto res = system(
833  (static_cast<std::string>("xmipp_edit -i " + nam + " -remove &").c_str()));
834  if (0 != res) {
835  REPORT_ERROR(
837  "Something went wrong when working with xmipp_edit. Please report this error to developers.");
838  }
839 }
Just to locate unclassified errors.
Definition: xmipp_error.h:192
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void write(const FileName &fn) const
Definition: matrix1d.cpp:794
void initRandom(int length)

◆ enumerate()

template<typename T >
void Matrix1D< T >::enumerate ( )

Enumerate starting from 0 to size

Definition at line 98 of file matrix1d.cpp.

99 {
100  for (size_t j = 0; j < vdim; j++)
101  vdata[j] = j;
102 }
#define j
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ indexSort()

template<typename T >
void Matrix1D< T >::indexSort ( Matrix1D< int > &  indx) const

Gives a vector with the indexes for a sorted vector

This function returns the indexes of a sorted vector. The input vector is not modified at all. For instance, if the input vector is [3 2 -1 0] the result of this function would be [3 4 2 1] meaning that the lowest value is at index 3, then comes the element at index 4, ... Note that indexes start at 1.

Definition at line 861 of file matrix1d.cpp.

862 {
863  Matrix1D< double > temp;
864  indx.clear();
865 
866  if (VEC_XSIZE(*this) == 0)
867  return;
868 
869  if (VEC_XSIZE(*this) == 1)
870  {
871  indx.resizeNoCopy(1);
872  VEC_ELEM(indx,0) = 1;
873  return;
874  }
875 
876  // Initialise data
877  indx.resizeNoCopy(VEC_XSIZE(*this));
878  typeCast(*this, temp);
879 
880  // Sort indexes
881  indexx(VEC_XSIZE(*this), MATRIX1D_ARRAY(temp)-1, MATRIX1D_ARRAY(indx)-1);
882 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
void clear()
Definition: matrix1d.cpp:67
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
void indexx(int n, double arrin[], int indx[])
#define MATRIX1D_ARRAY(v)
Definition: matrix1d.h:58
void typeCast(const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
Definition: matrix1d.h:1227
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458

◆ initConstant() [1/2]

template<typename T>
void Matrix1D< T >::initConstant ( val)

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 83 of file matrix1d.cpp.

84 {
85  for (size_t j = 0; j < vdim; j++)
86  vdata[j] = val;
87 }
#define j
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ initConstant() [2/2]

template<typename T>
void Matrix1D< T >::initConstant ( size_t  Xdim,
val 
)

Initialize to constant with a given size.

Definition at line 90 of file matrix1d.cpp.

91 {
92  if (vdim != Xdim)
93  resizeNoCopy(Xdim);
94  initConstant(val);
95 }
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
void initConstant(T val)
Definition: matrix1d.cpp:83
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ initZeros() [1/3]

template<typename T>
void Matrix1D< 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 592 of file matrix1d.h.

593  {
594  memset(vdata, 0, vdim * sizeof(T));
595  }
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ initZeros() [2/3]

template<typename T>
void Matrix1D< T >::initZeros ( size_t  Xdim)
inline

Initialize to zeros with a given size.

Definition at line 599 of file matrix1d.h.

600  {
601  if (vdim != Xdim)
602  resizeNoCopy(Xdim);
603  memset(vdata, 0, vdim * sizeof(T));
604  }
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ initZeros() [3/3]

template<typename T>
template<typename T1 >
void Matrix1D< T >::initZeros ( const Matrix1D< 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 616 of file matrix1d.h.

617  {
618  if (vdim != op.vdim)
619  resize(op);
620  memset(vdata, 0, vdim * sizeof(T));
621  }
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ isAnyNaN()

template<typename T >
bool Matrix1D< T >::isAnyNaN ( )

Check if any element of the array is NaN.

Definition at line 105 of file matrix1d.cpp.

106 {
107  for (size_t j = 0; j < vdim; j++)
108  if (ISNAN(vdata[j]))
109  return true;
110  return false;
111 }
#define ISNAN(x)
Definition: xmipp_macros.h:64
#define j
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ isCol()

template<typename T>
int Matrix1D< T >::isCol ( ) const
inline

True if vector is a column

if (v.isCol())
std::cout << "v is a column vector\n";

Definition at line 532 of file matrix1d.h.

533  {
534  return !row;
535  }
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267

◆ isRow()

template<typename T>
int Matrix1D< T >::isRow ( ) const
inline

True if vector is a row.

if (v.isRow())
std::cout << "v is a row vector\n";

Definition at line 520 of file matrix1d.h.

521  {
522  return row;
523  }
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267

◆ killAdaptationForNumericalRecipes()

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

Kill an array produced for Numerical Recipes.

Nothing needs to be done in fact.

This function is not ported to Python.

Definition at line 855 of file matrix1d.h.

856  {}

◆ maxIndex()

template<typename T >
int Matrix1D< T >::maxIndex ( ) const

Index for the maximum element.

This function returns the index of the maximum element of an matrix1d. Returns -1 if the array is empty

Definition at line 610 of file matrix1d.cpp.

611 {
612  if (vdim == 0)
613  return -1;
614 
615  int jmax = 0;
616  T maxval = VEC_ELEM(*this, 0);
617  for (size_t j = 0; j < vdim; ++j)
618  if (VEC_ELEM(*this,j) > maxval)
619  {
620  jmax = j;
621  maxval = VEC_ELEM(*this,j);
622  }
623  return jmax;
624 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define j
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ minIndex()

template<typename T >
int Matrix1D< T >::minIndex ( ) const

Index for the minimum element.

This function returns the index of the minimum element of an matrix1d. Returns -1 if the array is empty

Definition at line 627 of file matrix1d.cpp.

628 {
629  if (vdim == 0)
630  return -1;
631 
632  int jmin = 0;
633  T minval = VEC_ELEM(*this, 0);
634  for (size_t j = 0; j < vdim; ++j)
635  if (VEC_ELEM(*this,j) < minval)
636  {
637  jmin = j;
638  minval = VEC_ELEM(*this,j);
639  }
640  return jmin;
641 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define j
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ module()

template<typename T>
double Matrix1D< T >::module ( ) const
inline

Module of the vector

This module is defined as the square root of the sum of the squared components. Euclidean norm of the vector.

double mod = v.module();

Definition at line 983 of file matrix1d.h.

984  {
985  return sqrt(sum2());
986  }
void sqrt(Image< double > &op)
double sum2() const
Definition: matrix1d.cpp:673

◆ numericalDerivative()

template<typename T >
void Matrix1D< T >::numericalDerivative ( Matrix1D< double > &  result) const

Compute numerical derivative

The numerical derivative is of the same size as the input vector. However, the first two and the last two samples are set to 0, because the numerical method is not able to correctly estimate the derivative there.

Definition at line 746 of file matrix1d.cpp.

747 {
748  const double i12 = 1.0 / 12.0;
749  result.initZeros(*this);
750  for (int i = 2; i <= vdim - 2; i++)
751  if constexpr (std::is_signed_v<T> || std::is_floating_point_v<T>) {
752  result(i) = i12
753  * (-(*this)(i + 2) + 8 * (*this)(i + 1) - 8 * (*this)(i - 1)
754  + (*this)(i + 2));
755  } else {
756  REPORT_ERROR(
758  static_cast<std::string>("Can not use unitary minus on unsigned datatypes"));
759  }
760 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define i
void initZeros()
Definition: matrix1d.h:592
Incorrect type received.
Definition: xmipp_error.h:190
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator()()

template<typename T>
T& Matrix1D< T >::operator() ( int  i) const
inline

Vector element access

Returns the value of a vector logical position. In our example we could access from v(-2) to v(2). The elements can be used either by value or by reference.

v(-2) = 1;
val = v(-2);

Definition at line 819 of file matrix1d.h.

820  {
821  return vdata[i];
822  }
#define i
T * vdata
The array itself.
Definition: matrix1d.h:258

◆ operator*() [1/3]

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

v3 = v1 * k.

Definition at line 114 of file matrix1d.cpp.

115 {
116  Matrix1D<T> tmp(*this);
117  T *ptr1 = &VEC_ELEM(tmp,0);
118  const T *ptr2 = &VEC_ELEM(*this,0);
119  size_t iBlockMax = vdim / 4;
120  for (size_t i = 0; i < iBlockMax; i++)
121  {
122  (*ptr1++) = (*ptr2++) * op1;
123  (*ptr1++) = (*ptr2++) * op1;
124  (*ptr1++) = (*ptr2++) * op1;
125  (*ptr1++) = (*ptr2++) * op1;
126  }
127  for (size_t i = iBlockMax * 4; i < vdim; ++i)
128  (*ptr1++) = (*ptr2++) * op1;
129  return tmp;
130 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator*() [2/3]

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

v3 = v1 * v2.

Definition at line 296 of file matrix1d.cpp.

297 {
298  Matrix1D<T> tmp(op1);
299  T *ptr1 = &VEC_ELEM(tmp,0);
300  const T *ptr2 = &VEC_ELEM(*this,0);
301  const T *ptr3 = &VEC_ELEM(op1,0);
302  size_t iBlockMax = vdim / 4;
303  for (size_t i = 0; i < iBlockMax; i++)
304  {
305  (*ptr1++) = (*ptr2++) * (*ptr3++);
306  (*ptr1++) = (*ptr2++) * (*ptr3++);
307  (*ptr1++) = (*ptr2++) * (*ptr3++);
308  (*ptr1++) = (*ptr2++) * (*ptr3++);
309  }
310  for (size_t i = iBlockMax * 4; i < vdim; ++i)
311  (*ptr1++) = (*ptr2++) * (*ptr3++);
312  return tmp;
313 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator*() [3/3]

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

Vector by matrix

Algebraic vector by matrix multiplication. This function is actually implemented in xmippMatrices2D

Definition at line 739 of file matrix2d.cpp.

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 }
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
#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
#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
void initZeros()
Definition: matrix1d.h:592
#define j
Definition: ctf.h:38
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
void setRow()
Definition: matrix1d.h:543

◆ operator*=() [1/2]

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

v3 *= k.

Definition at line 231 of file matrix1d.cpp.

232 {
233  T *ptr1 = &VEC_ELEM(*this,0);
234  size_t iBlockMax = vdim / 4;
235  for (size_t i = 0; i < iBlockMax; i++)
236  {
237  (*ptr1++) *= op1;
238  (*ptr1++) *= op1;
239  (*ptr1++) *= op1;
240  (*ptr1++) *= op1;
241  }
242  for (size_t i = iBlockMax * 4; i < vdim; ++i)
243  (*ptr1++) *= op1;
244 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator*=() [2/2]

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

v3 *= v2.

Definition at line 376 of file matrix1d.cpp.

377 {
378  T *ptr1 = &VEC_ELEM(*this,0);
379  const T *ptr2 = &VEC_ELEM(op1,0);
380  size_t iBlockMax = vdim / 4;
381  for (size_t i = 0; i < iBlockMax; i++)
382  {
383  (*ptr1++) *= (*ptr2++);
384  (*ptr1++) *= (*ptr2++);
385  (*ptr1++) *= (*ptr2++);
386  (*ptr1++) *= (*ptr2++);
387  }
388  for (size_t i = iBlockMax * 4; i < vdim; ++i)
389  (*ptr1++) *= (*ptr2++);
390 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator+() [1/2]

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

v3 = v1 + k.

Definition at line 153 of file matrix1d.cpp.

154 {
155  Matrix1D<T> tmp(*this);
156  T *ptr1 = &VEC_ELEM(tmp,0);
157  const T *ptr2 = &VEC_ELEM(*this,0);
158  size_t iBlockMax = vdim / 4;
159  for (size_t i = 0; i < iBlockMax; i++)
160  {
161  (*ptr1++) = (*ptr2++) + op1;
162  (*ptr1++) = (*ptr2++) + op1;
163  (*ptr1++) = (*ptr2++) + op1;
164  (*ptr1++) = (*ptr2++) + op1;
165  }
166  for (size_t i = iBlockMax * 4; i < vdim; ++i)
167  (*ptr1++) = (*ptr2++) + op1;
168  return tmp;
169 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator+() [2/2]

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

v3 = v1 + v2.

Definition at line 336 of file matrix1d.cpp.

337 {
338  Matrix1D<T> tmp(op1);
339  T *ptr1 = &VEC_ELEM(tmp,0);
340  const T *ptr2 = &VEC_ELEM(*this,0);
341  const T *ptr3 = &VEC_ELEM(op1,0);
342  size_t iBlockMax = vdim / 4;
343  for (size_t i = 0; i < iBlockMax; i++)
344  {
345  (*ptr1++) = (*ptr2++) + (*ptr3++);
346  (*ptr1++) = (*ptr2++) + (*ptr3++);
347  (*ptr1++) = (*ptr2++) + (*ptr3++);
348  (*ptr1++) = (*ptr2++) + (*ptr3++);
349  }
350  for (size_t i = iBlockMax * 4; i < vdim; ++i)
351  (*ptr1++) = (*ptr2++) + (*ptr3++);
352  return tmp;
353 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator+=() [1/3]

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

Vector summation

A += B;

Definition at line 191 of file matrix1d.cpp.

192 {
193  if (vdim != op1.vdim)
194  REPORT_ERROR(ERR_MATRIX_SIZE, "Not same sizes in vector summation");
195 
196  T *ptr1 = &VEC_ELEM(*this,0);
197  const T *ptr2 = &VEC_ELEM(op1,0);
198  size_t iBlockMax = vdim / 4;
199  for (size_t i = 0; i < iBlockMax; i++)
200  {
201  (*ptr1++) += (*ptr2++);
202  (*ptr1++) += (*ptr2++);
203  (*ptr1++) += (*ptr2++);
204  (*ptr1++) += (*ptr2++);
205  }
206  for (size_t i = iBlockMax * 4; i < vdim; ++i)
207  (*ptr1++) += (*ptr2++);
208 }
#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 i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator+=() [2/3]

template<typename T>
void Matrix1D< T >::operator+= ( op1)

v3 += k.

Definition at line 264 of file matrix1d.cpp.

265 {
266  T *ptr1 = &VEC_ELEM(*this,0);
267  size_t iBlockMax = vdim / 4;
268  for (size_t i = 0; i < iBlockMax; i++)
269  {
270  (*ptr1++) += op1;
271  (*ptr1++) += op1;
272  (*ptr1++) += op1;
273  (*ptr1++) += op1;
274  }
275  for (size_t i = iBlockMax * 4; i < vdim; ++i)
276  (*ptr1++) += op1;
277 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator+=() [3/3]

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

v3 += v2.

Definition at line 410 of file matrix1d.cpp.

411  {
412  T *ptr1 = &VEC_ELEM(*this,0);
413  const T *ptr2 = &VEC_ELEM(op1,0);
414  size_t iBlockMax = vdim / 4;
415  for (size_t i = 0; i < iBlockMax; i++)
416  {
417  (*ptr1++) += (*ptr2++);
418  (*ptr1++) += (*ptr2++);
419  (*ptr1++) += (*ptr2++);
420  (*ptr1++) += (*ptr2++);
421  }
422  for (size_t i = iBlockMax * 4; i < vdim; ++i)
423  (*ptr1++) += (*ptr2++);
424  }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator-() [1/3]

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

v3 = v1 - k.

Definition at line 172 of file matrix1d.cpp.

173  {
174  Matrix1D<T> tmp(*this);
175  T *ptr1 = &VEC_ELEM(tmp,0);
176  const T *ptr2 = &VEC_ELEM(*this,0);
177  size_t iBlockMax = vdim / 4;
178  for (size_t i = 0; i < iBlockMax; i++)
179  {
180  (*ptr1++) = (*ptr2++) - op1;
181  (*ptr1++) = (*ptr2++) - op1;
182  (*ptr1++) = (*ptr2++) - op1;
183  (*ptr1++) = (*ptr2++) - op1;
184  }
185  for (size_t i = iBlockMax * 4; i < vdim; ++i)
186  (*ptr1++) = (*ptr2++) - op1;
187  return tmp;
188  }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator-() [2/3]

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

v3 = v1 - v2.

Definition at line 356 of file matrix1d.cpp.

357 {
358  Matrix1D<T> tmp(op1);
359  T *ptr1 = &VEC_ELEM(tmp,0);
360  const T *ptr2 = &VEC_ELEM(*this,0);
361  const T *ptr3 = &VEC_ELEM(op1,0);
362  size_t iBlockMax = vdim / 4;
363  for (size_t i = 0; i < iBlockMax; i++)
364  {
365  (*ptr1++) = (*ptr2++) - (*ptr3++);
366  (*ptr1++) = (*ptr2++) - (*ptr3++);
367  (*ptr1++) = (*ptr2++) - (*ptr3++);
368  (*ptr1++) = (*ptr2++) - (*ptr3++);
369  }
370  for (size_t i = iBlockMax * 4; i < vdim; ++i)
371  (*ptr1++) = (*ptr2++) - (*ptr3++);
372  return tmp;
373 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator-() [3/3]

template<typename T>
Matrix1D< T > Matrix1D< T >::operator- ( void  ) const

Unary minus.

It is used to build arithmetic expressions. You can make a minus of anything as long as it is correct semantically.

v1 = -v2;
v1 = -v2.transpose();

Definition at line 444 of file matrix1d.cpp.

445 {
446  Matrix1D<T> tmp(*this);
447  T *ptr1 = &VEC_ELEM(tmp,0);
448  const T *ptr2 = &VEC_ELEM(*this,0);
449  size_t iBlockMax = vdim / 4;
450  for (size_t i = 0; i < iBlockMax; i++)
451  {
452  if constexpr (std::is_signed_v<T> || std::is_floating_point_v<T>) {
453  (*ptr1++) = -(*ptr2++);
454  (*ptr1++) = -(*ptr2++);
455  (*ptr1++) = -(*ptr2++);
456  (*ptr1++) = -(*ptr2++);
457  } else {
458  REPORT_ERROR(
460  static_cast<std::string>("Can not use unitary minus on unsigned datatypes"));
461  }
462 
463  }
464  for (size_t i = iBlockMax * 4; i < vdim; ++i)
465  if constexpr (std::is_signed_v<T> || std::is_floating_point_v<T>) {
466  (*ptr1++) = -(*ptr2++);
467  } else {
468  REPORT_ERROR(
470  static_cast<std::string>("Can not use unitary minus on unsigned datatypes"));
471  }
472  return tmp;
473 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define i
Definition: ctf.h:38
Incorrect type received.
Definition: xmipp_error.h:190
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator-=() [1/3]

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

Vector subtraction

A -= B;

Definition at line 211 of file matrix1d.cpp.

212 {
213  if (vdim != op1.vdim)
214  REPORT_ERROR(ERR_MATRIX_SIZE, "Not same sizes in vector summation");
215 
216  T *ptr1 = &VEC_ELEM(*this,0);
217  const T *ptr2 = &VEC_ELEM(op1,0);
218  size_t iBlockMax = vdim / 4;
219  for (size_t i = 0; i < iBlockMax; i++)
220  {
221  (*ptr1++) -= (*ptr2++);
222  (*ptr1++) -= (*ptr2++);
223  (*ptr1++) -= (*ptr2++);
224  (*ptr1++) -= (*ptr2++);
225  }
226  for (size_t i = iBlockMax * 4; i < vdim; ++i)
227  (*ptr1++) -= (*ptr2++);
228 }
#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 i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator-=() [2/3]

template<typename T>
void Matrix1D< T >::operator-= ( op1)

v3 -= k.

Definition at line 280 of file matrix1d.cpp.

281 {
282  T *ptr1 = &VEC_ELEM(*this,0);
283  size_t iBlockMax = vdim / 4;
284  for (size_t i = 0; i < iBlockMax; i++)
285  {
286  (*ptr1++) -= op1;
287  (*ptr1++) -= op1;
288  (*ptr1++) -= op1;
289  (*ptr1++) -= op1;
290  }
291  for (size_t i = iBlockMax * 4; i < vdim; ++i)
292  (*ptr1++) -= op1;
293 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator-=() [3/3]

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

v3 -= v2.

Definition at line 427 of file matrix1d.cpp.

428  {
429  T *ptr1 = &VEC_ELEM(*this,0);
430  const T *ptr2 = &VEC_ELEM(op1,0);
431  size_t iBlockMax = vdim / 4;
432  for (size_t i = 0; i < iBlockMax; i++)
433  {
434  (*ptr1++) -= (*ptr2++);
435  (*ptr1++) -= (*ptr2++);
436  (*ptr1++) -= (*ptr2++);
437  (*ptr1++) -= (*ptr2++);
438  }
439  for (size_t i = iBlockMax * 4; i < vdim; ++i)
440  (*ptr1++) -= (*ptr2++);
441  }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator/() [1/2]

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

v3 = v1 / k.

Definition at line 133 of file matrix1d.cpp.

134 {
135  Matrix1D<T> tmp(*this);
136  T iop1 = 1 / op1;
137  T *ptr1 = &VEC_ELEM(tmp,0);
138  const T *ptr2 = &VEC_ELEM(*this,0);
139  size_t iBlockMax = vdim / 4;
140  for (size_t i = 0; i < iBlockMax; i++)
141  {
142  (*ptr1++) = (*ptr2++) * iop1;
143  (*ptr1++) = (*ptr2++) * iop1;
144  (*ptr1++) = (*ptr2++) * iop1;
145  (*ptr1++) = (*ptr2++) * iop1;
146  }
147  for (size_t i = iBlockMax * 4; i < vdim; ++i)
148  (*ptr1++) = (*ptr2++) * iop1;
149  return tmp;
150 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator/() [2/2]

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

v3 = v1 / v2.

Definition at line 316 of file matrix1d.cpp.

317 {
318  Matrix1D<T> tmp(op1);
319  T *ptr1 = &VEC_ELEM(tmp,0);
320  const T *ptr2 = &VEC_ELEM(*this,0);
321  const T *ptr3 = &VEC_ELEM(op1,0);
322  size_t iBlockMax = vdim / 4;
323  for (size_t i = 0; i < iBlockMax; i++)
324  {
325  (*ptr1++) = (*ptr2++) / (*ptr3++);
326  (*ptr1++) = (*ptr2++) / (*ptr3++);
327  (*ptr1++) = (*ptr2++) / (*ptr3++);
328  (*ptr1++) = (*ptr2++) / (*ptr3++);
329  }
330  for (size_t i = iBlockMax * 4; i < vdim; ++i)
331  (*ptr1++) = (*ptr2++) / (*ptr3++);
332  return tmp;
333 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator/=() [1/2]

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

v3 /= k.

Definition at line 247 of file matrix1d.cpp.

248 {
249  T iop1 = 1 / op1;
250  T * ptr1 = &VEC_ELEM(*this,0);
251  size_t iBlockMax = vdim / 4;
252  for (size_t i = 0; i < iBlockMax; i++)
253  {
254  (*ptr1++) *= iop1;
255  (*ptr1++) *= iop1;
256  (*ptr1++) *= iop1;
257  (*ptr1++) *= iop1;
258  }
259  for (size_t i = iBlockMax * 4; i < vdim; ++i)
260  (*ptr1++) *= iop1;
261 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator/=() [2/2]

template<typename T>
void Matrix1D< T >::operator/= ( const Matrix1D< T > &  op1)

v3 /= v2.

Definition at line 393 of file matrix1d.cpp.

394 {
395  T *ptr1 = &VEC_ELEM(*this,0);
396  const T *ptr2 = &VEC_ELEM(op1,0);
397  size_t iBlockMax = vdim / 4;
398  for (size_t i = 0; i < iBlockMax; i++)
399  {
400  (*ptr1++) /= (*ptr2++);
401  (*ptr1++) /= (*ptr2++);
402  (*ptr1++) /= (*ptr2++);
403  (*ptr1++) /= (*ptr2++);
404  }
405  for (size_t i = iBlockMax * 4; i < vdim; ++i)
406  (*ptr1++) /= (*ptr2++);
407 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator=() [1/2]

template<typename T>
Matrix1D< T > & Matrix1D< T >::operator= ( const Matrix1D< 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 33 of file matrix1d.cpp.

34 {
35  if (&op1 != this)
36  {
37  resizeNoCopy(op1);
38  memcpy(vdata, op1.vdata, vdim * sizeof(T));
39  row = op1.row;
40  }
41 
42  return *this;
43 }
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator=() [2/2]

template<typename T>
Matrix1D< T > & Matrix1D< T >::operator= ( const std::vector< T > &  op1)

Assignment.

Definition at line 58 of file matrix1d.cpp.

59 {
60  resizeNoCopy(op1.size());
61  memcpy(&vdata[0],&(op1[0]),vdim*sizeof(T));
62  row = false;
63  return *this;
64 }
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator==()

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

Equals.

Definition at line 46 of file matrix1d.cpp.

47 {
48  if (row != op1.row)
49  return false;
50 
51  for (size_t i = 0; i < vdim; ++i)
52  if (!XMIPP_EQUAL_REAL(vdata[i], op1.vdata[i]))
53  return false;
54  return true;
55 }
#define i
#define XMIPP_EQUAL_REAL(x, y)
Definition: xmipp_macros.h:122
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator[]() [1/2]

template<typename T>
T& Matrix1D< T >::operator[] ( int  idx)
inline

Definition at line 824 of file matrix1d.h.

824  {
825  return vdata[idx];
826  }
T * vdata
The array itself.
Definition: matrix1d.h:258

◆ operator[]() [2/2]

template<typename T>
T Matrix1D< T >::operator[] ( int  idx) const
inline

Definition at line 828 of file matrix1d.h.

828  {
829  return vdata[idx];
830  }
T * vdata
The array itself.
Definition: matrix1d.h:258

◆ read()

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

Read from an ASCII file.

The array must be previously resized to the correct size.

Definition at line 808 of file matrix1d.cpp.

809 {
810  std::ifstream in;
811  in.open(fn.c_str(), std::ios::in);
812 
813  if (!in)
814  REPORT_ERROR(
816  static_cast< std::string >("MultidimArray::read: File " + fn + " not found"));
817 
818  in >> *this;
819  in.close();
820 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
int in
File cannot be open.
Definition: xmipp_error.h:137

◆ resize() [1/2]

template<typename T>
void Matrix1D< T >::resize ( size_t  Xdim,
bool  copy = true 
)
inline

Resize to a given size

This function resize the actual array to the given size. The origin is not modified. If the actual array is larger than the pattern then the values outside the new size are lost, if it is smaller then 0's are added. An exception is thrown if there is no memory.

V1.resize(3, 3, 2);

Definition at line 410 of file matrix1d.h.

411  {
412  if (Xdim == vdim)
413  return;
414 
415  if (Xdim <= 0)
416  {
417  clear();
418  return;
419  }
420 
421  T * new_vdata;
422  try
423  {
424  new_vdata = new T[(size_t) Xdim]; //TODO: Kino: Valgrind points here there is something wrong, but I could'nt resolve
425  memset(new_vdata, 0, Xdim * sizeof(T));
426  }
427  catch (std::bad_alloc &)
428  {
429  REPORT_ERROR(ERR_MEM_NOTENOUGH, "Allocate: No space left");
430  }
431 
432  // Copy needed elements, fill with 0 if necessary
433  if (copy)
434  {
435  T zero=0; // Useful for complexes
436  for (size_t j = 0; j < Xdim; j++)
437  {
438  T *val=NULL;
439  if (j >= vdim)
440  val = &zero;
441  else
442  val = &vdata[j];
443  new_vdata[j] = *val;
444  }
445  }
446 
447  // deallocate old vector
448  coreDeallocate();
449 
450  // assign *this vector to the newly created
451  vdata = new_vdata;
452  vdim = Xdim;
453 
454  }
void clear()
Definition: matrix1d.cpp:67
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void coreDeallocate()
Definition: matrix1d.h:389
There is not enough memory for allocation.
Definition: xmipp_error.h:166
#define j
T * vdata
The array itself.
Definition: matrix1d.h:258
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
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ resize() [2/2]

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

Resize according to a pattern.

This function resize the actual array to the same size 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 476 of file matrix1d.h.

477  {
478  if (vdim != v.vdim)
479  resize(v.vdim);
480  }
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ resizeNoCopy() [1/2]

template<typename T>
void Matrix1D< T >::resizeNoCopy ( int  Xdim)
inline

Resize a single 1D image with no copy

Definition at line 458 of file matrix1d.h.

459  {
460  resize(Xdim, false);
461  }
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410

◆ resizeNoCopy() [2/2]

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

Resize a single 1D image with no copy

Definition at line 485 of file matrix1d.h.

486  {
487  if (vdim != v.vdim)
488  resize(v.vdim, false);
489  }
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ sameShape()

template<typename T>
template<typename T1 >
bool Matrix1D< T >::sameShape ( const Matrix1D< 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 497 of file matrix1d.h.

498  {
499  return (vdim == op.vdim);
500  }
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ selfCEIL()

template<typename T >
void Matrix1D< T >::selfCEIL ( )

CEILING

Applies a CEILING (look for the nearest larger integer) to each array element.

Definition at line 476 of file matrix1d.cpp.

477 {
478  T *ptr1 = &VEC_ELEM(*this,0);
479  size_t iBlockMax = vdim / 4;
480  for (size_t i = 0; i < iBlockMax; i++)
481  {
482  *ptr1 = ceil(*ptr1);
483  ++ptr1;
484  *ptr1 = ceil(*ptr1);
485  ++ptr1;
486  *ptr1 = ceil(*ptr1);
487  ++ptr1;
488  *ptr1 = ceil(*ptr1);
489  ++ptr1;
490  }
491  for (size_t i = iBlockMax * 4; i < vdim; ++i)
492  {
493  *ptr1 = ceil(*ptr1);
494  ++ptr1;
495  }
496 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ selfFLOOR()

template<typename T >
void Matrix1D< T >::selfFLOOR ( )

FLOOR

Applies a FLOOR (look for the nearest larger integer) to each array element.

Definition at line 499 of file matrix1d.cpp.

500 {
501  T *ptr1 = &VEC_ELEM(*this,0);
502  size_t iBlockMax = vdim / 4;
503  for (size_t i = 0; i < iBlockMax; i++)
504  {
505  *ptr1 = floor(*ptr1);
506  ++ptr1;
507  *ptr1 = floor(*ptr1);
508  ++ptr1;
509  *ptr1 = floor(*ptr1);
510  ++ptr1;
511  *ptr1 = floor(*ptr1);
512  ++ptr1;
513  }
514  for (size_t i = iBlockMax * 4; i < vdim; ++i)
515  {
516  *ptr1 = floor(*ptr1);
517  ++ptr1;
518  }
519 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
__host__ __device__ float2 floor(const float2 v)
#define i
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ selfNormalize()

template<typename T >
void Matrix1D< T >::selfNormalize ( )

Normalize this vector, store the result here

Definition at line 723 of file matrix1d.cpp.

724 {
725  double m = module();
726  if (fabs(m) > XMIPP_EQUAL_ACCURACY)
727  {
728  T im = (T) (1.0 / m);
729  *this *= im;
730  }
731  else
732  initZeros();
733 }
double module() const
Definition: matrix1d.h:983
#define XMIPP_EQUAL_ACCURACY
Definition: xmipp_macros.h:119
void initZeros()
Definition: matrix1d.h:592
int m

◆ selfReverse()

template<typename T >
void Matrix1D< T >::selfReverse ( )

Reverse vector values, keep in this object.

Definition at line 736 of file matrix1d.cpp.

737 {
738  for (int j = 0; j <= (int) (vdim - 1) / 2; j++)
739  {
740  T aux;
741  SWAP(vdata[j], vdata[vdim-1-j], aux);
742  }
743 }
#define j
#define SWAP(a, b, tmp)
Definition: xmipp_macros.h:428
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ selfROUND()

template<typename T >
void Matrix1D< T >::selfROUND ( )

ROUND

Applies a ROUND (look for the nearest larger integer) to each array element.

Definition at line 522 of file matrix1d.cpp.

523 {
524  T *ptr1 = &VEC_ELEM(*this,0);
525  size_t iBlockMax = vdim / 4;
526  for (size_t i = 0; i < iBlockMax; i++)
527  {
528  *ptr1 = round(*ptr1);
529  ++ptr1;
530  *ptr1 = round(*ptr1);
531  ++ptr1;
532  *ptr1 = round(*ptr1);
533  ++ptr1;
534  *ptr1 = round(*ptr1);
535  ++ptr1;
536  }
537  for (size_t i = iBlockMax * 4; i < vdim; ++i)
538  {
539  *ptr1 = round(*ptr1);
540  ++ptr1;
541  }
542 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
int round(double x)
Definition: ap.cpp:7245
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ selfTranspose()

template<typename T>
void Matrix1D< T >::selfTranspose ( )
inline

Algebraic transpose of vector

The same as before but the result is stored in this same object.

Definition at line 944 of file matrix1d.h.

945  {
946  row = !row;
947  }
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267

◆ setCol()

template<typename T>
void Matrix1D< T >::setCol ( )
inline

Forces the vector to be a column vector

v.setCol();

Definition at line 554 of file matrix1d.h.

555  {
556  row = false;
557  }
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267

◆ setRow()

template<typename T>
void Matrix1D< T >::setRow ( )
inline

Forces the vector to be a row vector

v.setRow();

Definition at line 543 of file matrix1d.h.

544  {
545  row = true;
546  }
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267

◆ showWithGnuPlot()

template<typename T >
void Matrix1D< T >::showWithGnuPlot ( const std::string &  xlabel,
const std::string &  title 
)

Show using gnuplot

This function uses gnuplot to plot this vector. You must supply the xlabel and title.

Definition at line 763 of file matrix1d.cpp.

764 {
765  FileName fn_tmp;
766  fn_tmp.initRandom(10);
767  Matrix1D<T>::write(static_cast<std::string>("PPP") + fn_tmp + ".txt");
768 
769  std::ofstream fh_gplot;
770  fh_gplot.open(
771  (static_cast<std::string>("PPP") + fn_tmp + ".gpl").c_str());
772  if (!fh_gplot)
773  REPORT_ERROR(
775  static_cast<std::string>("vector::showWithGnuPlot: Cannot open PPP") + fn_tmp + ".gpl for output");
776  fh_gplot << "set xlabel \"" + xlabel + "\"\n";
777  fh_gplot
778  << "plot \"PPP" + fn_tmp + ".txt\" title \"" + title
779  + "\" w l\n";
780  fh_gplot << "pause 300 \"\"\n";
781  fh_gplot.close();
782  auto res = system(
783  (static_cast<std::string>("(gnuplot PPP") + fn_tmp
784  + ".gpl; rm PPP" + fn_tmp + ".txt PPP" + fn_tmp
785  + ".gpl) &").c_str());
786  if (0 != res) {
787  REPORT_ERROR(
789  "Something went wrong when working with GNUPlot. Please report this error to developers.");
790  }
791 }
Just to locate unclassified errors.
Definition: xmipp_error.h:192
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void write(const FileName &fn) const
Definition: matrix1d.cpp:794
void initRandom(int length)

◆ size()

template<typename T>
size_t Matrix1D< T >::size ( ) const
inline

Returns the size of this vector

int nn = a.size();

Definition at line 508 of file matrix1d.h.

509  {
510  return vdim;
511  }
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ sort()

template<typename T >
Matrix1D< T > Matrix1D< T >::sort ( ) const

Sort 1D vector elements

Sort in ascending order the vector elements. You can use the "reverse" function to sort in descending order.

v2 = v1.sort();

Definition at line 850 of file matrix1d.cpp.

851 {
852  Matrix1D<T> temp(*this);
853  if (vdim == 0)
854  return temp;
855 
856  std::sort(temp.vdata, temp.vdata + temp.vdim);
857  return temp;
858 }
void sort(struct DCEL_T *dcel)
Definition: sorting.cpp:18
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ sum()

template<typename T >
double Matrix1D< T >::sum ( bool  average = false) const

Sum of vector values.

This function returns the sum of all internal values.

double sum = m.sum();

Definition at line 652 of file matrix1d.cpp.

653 {
654  double sum = 0;
655  const T *ptr1 = &VEC_ELEM(*this,0);
656  size_t iBlockMax = vdim / 4;
657  for (size_t i = 0; i < iBlockMax; i++)
658  {
659  sum += (*ptr1++);
660  sum += (*ptr1++);
661  sum += (*ptr1++);
662  sum += (*ptr1++);
663  }
664  for (size_t i = iBlockMax * 4; i < vdim; ++i)
665  sum += (*ptr1++);
666  if (average)
667  return sum / (double) vdim;
668  else
669  return sum;
670 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
double sum(bool average=false) const
Definition: matrix1d.cpp:652
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ sum2()

template<typename T >
double Matrix1D< T >::sum2 ( ) const

Sum of squared vector values.

This function returns the sum of all internal values to the second power.

double sum2 = m.sum2();

Definition at line 673 of file matrix1d.cpp.

674 {
675  double sum = 0;
676  const T *ptr1 = &VEC_ELEM(*this,0);
677  double val;
678  size_t iBlockMax = vdim / 4;
679  for (size_t i = 0; i < iBlockMax; i++)
680  {
681  val = *ptr1;
682  sum += val * val;
683  ++ptr1;
684  val = *ptr1;
685  sum += val * val;
686  ++ptr1;
687  val = *ptr1;
688  sum += val * val;
689  ++ptr1;
690  val = *ptr1;
691  sum += val * val;
692  ++ptr1;
693  }
694  for (size_t i = iBlockMax * 4; i < vdim; ++i)
695  {
696  val = *ptr1;
697  sum += val * val;
698  ++ptr1;
699  }
700  return sum;
701 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
double sum(bool average=false) const
Definition: matrix1d.cpp:652
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ transpose()

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

Algebraic transpose of vector

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 644 of file matrix1d.cpp.

645 {
646  Matrix1D<T> temp(*this);
647  temp.selfTranspose();
648  return temp;
649 }
Definition: ctf.h:38

◆ write()

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

Write to an ASCII file.

Definition at line 794 of file matrix1d.cpp.

795 {
796  std::ofstream out;
797  out.open(fn.c_str(), std::ios::out);
798  if (!out)
799  REPORT_ERROR(
801  static_cast< std::string >("Matrix1D::write: File " + fn + " cannot be opened for output"));
802 
803  out << *this;
804  out.close();
805 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
File cannot be open.
Definition: xmipp_error.h:137

Friends And Related Function Documentation

◆ operator*

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

v3 = k * v2.

Definition at line 648 of file matrix1d.h.

649  {
650  Matrix1D<T> tmp(op2);
651  T *ptr1 = &VEC_ELEM(tmp,0);
652  const T *ptr2 = &VEC_ELEM(op2,0);
653  size_t iBlockMax = op2.vdim / 4;
654  for (size_t i = 0; i < iBlockMax; i++)
655  {
656  (*ptr1++) = (*ptr2++) * op1;
657  (*ptr1++) = (*ptr2++) * op1;
658  (*ptr1++) = (*ptr2++) * op1;
659  (*ptr1++) = (*ptr2++) * op1;
660  }
661  for (size_t i = iBlockMax * 4; i < op2.vdim; ++i)
662  (*ptr1++) = (*ptr2++) * op1;
663  return tmp;
664  }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator+

template<typename T>
Matrix1D<T> operator+ ( op1,
const Matrix1D< T > &  op2 
)
friend

v3 = k + v2.

Definition at line 688 of file matrix1d.h.

689  {
690  Matrix1D<T> tmp(op2);
691  T *ptr1 = &VEC_ELEM(tmp,0);
692  const T *ptr2 = &VEC_ELEM(op2,0);
693  size_t iBlockMax = op2.vdim / 4;
694  for (size_t i = 0; i < iBlockMax; i++)
695  {
696  (*ptr1++) = (*ptr2++) + op1;
697  (*ptr1++) = (*ptr2++) + op1;
698  (*ptr1++) = (*ptr2++) + op1;
699  (*ptr1++) = (*ptr2++) + op1;
700  }
701  for (size_t i = iBlockMax * 4; i < op2.vdim; ++i)
702  (*ptr1++) = (*ptr2++) + op1;
703  return tmp;
704  }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator-

template<typename T>
Matrix1D<T> operator- ( op1,
const Matrix1D< T > &  op2 
)
friend

v3 = k - v2.

Definition at line 708 of file matrix1d.h.

709  {
710  Matrix1D<T> tmp(op2);
711  T *ptr1 = &VEC_ELEM(tmp,0);
712  const T *ptr2 = &VEC_ELEM(op2,0);
713  size_t iBlockMax = op2.vdim / 4;
714  for (size_t i = 0; i < iBlockMax; i++)
715  {
716  (*ptr1++) = op1 - (*ptr2++);
717  (*ptr1++) = op1 - (*ptr2++);
718  (*ptr1++) = op1 - (*ptr2++);
719  (*ptr1++) = op1 - (*ptr2++);
720  }
721  for (size_t i = iBlockMax * 4; i < op2.vdim; ++i)
722  (*ptr1++) = op1 - (*ptr2++);
723  return tmp;
724  }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator/

template<typename T>
Matrix1D<T> operator/ ( op1,
const Matrix1D< T > &  op2 
)
friend

v3 = k / v2.

Definition at line 668 of file matrix1d.h.

669  {
670  Matrix1D<T> tmp(op2);
671  T *ptr1 = &VEC_ELEM(tmp,0);
672  const T *ptr2 = &VEC_ELEM(op2,0);
673  size_t iBlockMax = op2.vdim / 4;
674  for (size_t i = 0; i < iBlockMax; i++)
675  {
676  (*ptr1++) = op1 / (*ptr2++);
677  (*ptr1++) = op1 / (*ptr2++);
678  (*ptr1++) = op1 / (*ptr2++);
679  (*ptr1++) = op1 / (*ptr2++);
680  }
681  for (size_t i = iBlockMax * 4; i < op2.vdim; ++i)
682  (*ptr1++) = op1 / (*ptr2++);
683  return tmp;
684  }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define i
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator<<

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

Output to output stream.

Definition at line 1048 of file matrix1d.h.

1049  {
1050  if (v.vdim == 0)
1051  ostrm << "NULL Array\n";
1052  else
1053  ostrm << std::endl;
1054 
1055  double max_val = ABS(v.vdata[0]);
1056 
1057  for (size_t j = 0; j < v.vdim; j++)
1058  {
1059  max_val = XMIPP_MAX(max_val, v.vdata[j]);
1060  }
1061 
1062  int prec = bestPrecision(max_val, 10);
1063 
1064  if (v.row)
1065  for (size_t j = 0; j < v.vdim; j++)
1066  {
1067  ostrm << floatToString((double) v.vdata[j], 10, prec)
1068  << std::endl;
1069  }
1070  else
1071  for (size_t j = 0; j < v.vdim; j++)
1072  {
1073  ostrm << floatToString((double) v.vdata[j], 10, prec) << " ";
1074  }
1075 
1076  return ostrm;
1077  }
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
int bestPrecision(float F, int _width)
String floatToString(float F, int _width, int _prec)
#define ABS(x)
Definition: xmipp_macros.h:142
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267
#define j
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ operator>>

template<typename T>
std::istream& operator>> ( std::istream &  in,
Matrix1D< T > &  v 
)
friend

Input from input stream.

Actual size of the array is used to know how many values must be read.

v.resize(3);
std::cin >> v;

This function is not ported to Python.

Definition at line 1040 of file matrix1d.h.

1041  {
1042  for (int i = 0; i < VEC_XSIZE(v); i++)
1043  in >> VEC_ELEM(v,i);
1044  return in;
1045  }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
#define i
int in

Member Data Documentation

◆ destroyData

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

Destroy data.

Definition at line 261 of file matrix1d.h.

◆ row

template<typename T>
bool Matrix1D< T >::row

<0=column vector (default), 1=row vector

Definition at line 267 of file matrix1d.h.

◆ vdata

template<typename T>
T* Matrix1D< T >::vdata

The array itself.

Definition at line 258 of file matrix1d.h.

◆ vdim

template<typename T>
size_t Matrix1D< T >::vdim

Number of elements.

Definition at line 264 of file matrix1d.h.


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