Xmipp  v3.23.11-Nereus
Classes | Typedefs
Matrix1D Vectors
Collaboration diagram for Matrix1D Vectors:

Classes

class  Matrix1D< T >
 

Typedefs

typedef Matrix1D< double > DVector
 
typedef Matrix1D< int > IVector
 

Vectors speed up macros

This macros are defined to allow high speed in critical parts of your program. They shouldn't be used systematically as usually there is no checking on the correctness of the operation you are performing. Speed comes from three facts: first, they are macros and no function call is performed (although most of the critical functions are inline functions), there is no checking on the correctness of the operation (it could be wrong and you are not warned of it), and destination vectors are not returned saving time in the copy constructor and in the creation/destruction of temporary vectors.

#define MATRIX1D_ARRAY(v)   ((v).vdata)
 
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)   for (size_t i=0; i<v.vdim; i++)
 
#define VEC_XSIZE(m)   ((m).vdim)
 
#define XX(v)   (v).vdata[0]
 
#define YY(v)   (v).vdata[1]
 
#define ZZ(v)   (v).vdata[2]
 
#define VECTOR_R2(v, x, y)
 
#define VECTOR_R3(v, x, y, z)
 
#define V2_PLUS_V2(a, b, c)
 
#define V2_MINUS_V2(a, b, c)
 
#define V2_PLUS_CT(a, b, k)
 
#define V2_BY_CT(a, b, k)
 
#define V3_PLUS_V3(a, b, c)
 
#define V3_MINUS_V3(a, b, c)
 
#define V3_PLUS_CT(a, b, c)
 
#define V3_BY_CT(a, b, c)
 
#define VEC_ELEM(v, i)   ((v).vdata[(i)])
 
#define dMi(v, i)   ((v).vdata[(i)])
 
#define VEC_SWAP(v, i, j, aux)   {aux = dMi(v, i); dMi(v, i) = dMi(v, j); dMi(v, j) = aux; }
 

Vector Related functions

These functions are not methods of Matrix1D

Matrix1D< double > vectorR2 (double x, double y)
 
Matrix1D< double > vectorR3 (double x, double y, double z)
 
Matrix1D< int > vectorR3 (int x, int y, int z)
 
template<typename T >
dotProduct (const Matrix1D< T > &v1, const Matrix1D< T > &v2)
 
template<typename T >
Matrix1D< T > vectorProduct (const Matrix1D< T > &v1, const Matrix1D< T > &v2)
 
template<typename T >
void vectorProduct (const Matrix1D< T > &v1, const Matrix1D< T > &v2, Matrix1D< T > &result)
 
template<typename T >
void sortTwoVectors (Matrix1D< T > &v1, Matrix1D< T > &v2)
 
template<typename T1 , typename T2 >
void typeCast (const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
 
template<typename T1 >
void typeCast (const Matrix1D< T1 > &v1, Matrix1D< T1 > &v2)
 

Detailed Description

Macro Definition Documentation

◆ dMi

#define dMi (   v,
  i 
)    ((v).vdata[(i)])

Definition at line 246 of file matrix1d.h.

◆ FOR_ALL_ELEMENTS_IN_MATRIX1D

#define FOR_ALL_ELEMENTS_IN_MATRIX1D (   v)    for (size_t i=0; i<v.vdim; i++)

For all elements in the array This macro is used to generate loops for the vector in an easy manner. It defines an internal index 'i' which ranges the vector using its mathematical definition (ie, logical access).

{
std::cout << v(i) << " ";
}

Definition at line 72 of file matrix1d.h.

◆ MATRIX1D_ARRAY

#define MATRIX1D_ARRAY (   v)    ((v).vdata)

Array access. This macro gives you access to the array (T)

Definition at line 58 of file matrix1d.h.

◆ V2_BY_CT

#define V2_BY_CT (   a,
  b,
  k 
)
Value:
{ \
XX(a) = XX(b) * (k); \
YY(a) = YY(b) * (k); }
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
doublereal * b
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93
doublereal * a

Multiplying/dividing by a constant a R2 vector (a=b*k)

double k;
...;
double k;
...;
V2_BY_CT(a, b, 1/k);

Definition at line 179 of file matrix1d.h.

◆ V2_MINUS_V2

#define V2_MINUS_V2 (   a,
  b,
  c 
)
Value:
{ \
XX(a) = XX(b) - XX(c); \
YY(a) = YY(b) - YY(c); }
doublereal * c
doublereal * b
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93
doublereal * a

Subtracting two R2 vectors (a=b-c)

Definition at line 145 of file matrix1d.h.

◆ V2_PLUS_CT

#define V2_PLUS_CT (   a,
  b,
  k 
)
Value:
{ \
XX(a) = XX(b) + (k); \
YY(a) = YY(b) + (k); }
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
doublereal * b
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93
doublereal * a

Adding/subtracting a constant to a R2 vector (a=b-k).

double k;
...;
double k;
...;

Definition at line 162 of file matrix1d.h.

◆ V2_PLUS_V2

#define V2_PLUS_V2 (   a,
  b,
  c 
)
Value:
{ \
XX(a) = XX(b) + XX(c); \
YY(a) = YY(b) + YY(c); }
doublereal * c
doublereal * b
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93
doublereal * a

Adding two R2 vectors (a=b+c)

...;

Definition at line 134 of file matrix1d.h.

◆ V3_BY_CT

#define V3_BY_CT (   a,
  b,
  c 
)
Value:
{ \
XX(a) = XX(b) * (c); \
YY(a) = YY(b) * (c); \
ZZ(a) = ZZ(b) * (c); }
doublereal * c
doublereal * b
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93
#define ZZ(v)
Definition: matrix1d.h:101
doublereal * a

Multiplying/dividing by a constant a R3 vector (a=b*k)

double k;
...;
double k;
...;
V3_BY_CT(a, b, 1/k);

Definition at line 238 of file matrix1d.h.

◆ V3_MINUS_V3

#define V3_MINUS_V3 (   a,
  b,
  c 
)
Value:
{ \
XX(a) = XX(b) - XX(c); \
YY(a) = YY(b) - YY(c); \
ZZ(a) = ZZ(b) - ZZ(c); }
doublereal * c
doublereal * b
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93
#define ZZ(v)
Definition: matrix1d.h:101
doublereal * a

Subtracting two R3 vectors (a=b-c)

Definition at line 202 of file matrix1d.h.

◆ V3_PLUS_CT

#define V3_PLUS_CT (   a,
  b,
  c 
)
Value:
{ \
XX(a) = XX(b) + (c); \
YY(a) = YY(b) + (c); \
ZZ(a) = ZZ(b) + (c); }
doublereal * c
doublereal * b
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93
#define ZZ(v)
Definition: matrix1d.h:101
doublereal * a

Adding/subtracting a constant to a R3 vector (a=b-k)

double k;
...;
double k;
...;

Definition at line 220 of file matrix1d.h.

◆ V3_PLUS_V3

#define V3_PLUS_V3 (   a,
  b,
  c 
)
Value:
{ \
XX(a) = XX(b) + XX(c); \
YY(a) = YY(b) + YY(c); \
ZZ(a) = ZZ(b) + ZZ(c); }
doublereal * c
doublereal * b
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93
#define ZZ(v)
Definition: matrix1d.h:101
doublereal * a

Adding two R3 vectors (a=b+c)

...;

Definition at line 190 of file matrix1d.h.

◆ VEC_ELEM

#define VEC_ELEM (   v,
  i 
)    ((v).vdata[(i)])

Direct access to vector element

Definition at line 245 of file matrix1d.h.

◆ VEC_SWAP

#define VEC_SWAP (   v,
  i,
  j,
  aux 
)    {aux = dMi(v, i); dMi(v, i) = dMi(v, j); dMi(v, j) = aux; }

Definition at line 248 of file matrix1d.h.

◆ VEC_XSIZE

#define VEC_XSIZE (   m)    ((m).vdim)

X dimension of the matrix

Definition at line 77 of file matrix1d.h.

◆ VECTOR_R2

#define VECTOR_R2 (   v,
  x,
  y 
)
Value:
{ \
XX(v) = x; YY(v) = y; }
static double * y
doublereal * x
#define YY(v)
Definition: matrix1d.h:93

Creates vector in R2 The vector must be created beforehand to the correct size. After this macro the vector is (x, y) in R2.

Definition at line 112 of file matrix1d.h.

◆ VECTOR_R3

#define VECTOR_R3 (   v,
  x,
  y,
  z 
)
Value:
{ \
XX(v) = x; YY(v) = y; ZZ(v) = z;}
static double * y
doublereal * x
double z
#define YY(v)
Definition: matrix1d.h:93
#define ZZ(v)
Definition: matrix1d.h:101

Creates vector in R3 The vector must be created beforehand to the correct size. After this macro the vector is (x, y, z) in R3.

Definition at line 124 of file matrix1d.h.

◆ XX

#define XX (   v)    (v).vdata[0]

Access to X component

XX(v) = 1;
val = XX(v);

Definition at line 85 of file matrix1d.h.

◆ YY

#define YY (   v)    (v).vdata[1]

Access to Y component

YY(v) = 1;
val = YY(v);

Definition at line 93 of file matrix1d.h.

◆ ZZ

#define ZZ (   v)    (v).vdata[2]

Access to Z component

ZZ(v) = 1;
val = ZZ(v);

Definition at line 101 of file matrix1d.h.

Typedef Documentation

◆ DVector

typedef Matrix1D<double> DVector

Definition at line 1094 of file matrix1d.h.

◆ IVector

typedef Matrix1D<int> IVector

Definition at line 1095 of file matrix1d.h.

Function Documentation

◆ dotProduct()

template<typename T >
T dotProduct ( const Matrix1D< T > &  v1,
const Matrix1D< T > &  v2 
)

Dot product. Given any two vectors in Rn (n-dimensional vector), this function returns the dot product of both. If the vectors are not of the same size or shape then an exception is thrown. The dot product is defined as the sum of the component by component multiplication.

For the R3 vectors (V1x,V1y,V1z), (V2x, V2y, V2z) the result is V1x*V2x + V1y*V2y + V1z*V2z.

v1.init_random(0, 10, "gaussian");
std::cout << "The power of this vector should be 100 and is " <<
dotProduct(v1, v1) << std::endl;

Definition at line 1140 of file matrix1d.h.

1141 {
1142  if (!v1.sameShape(v2))
1144  "Dot product: vectors of different size or shape");
1145 
1146  T accumulate = 0;
1147  for (size_t j = 0; j < v1.vdim; j++)
1148  accumulate += v1.vdata[j] * v2.vdata[j];
1149  return accumulate;
1150 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
bool sameShape(const Matrix1D< T1 > &op) const
Definition: matrix1d.h:497
#define j
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ sortTwoVectors()

template<typename T >
void sortTwoVectors ( Matrix1D< T > &  v1,
Matrix1D< T > &  v2 
)

Sort two vectors. v1 and v2 must be of the same shape, if not an exception is thrown. After calling this function all components in v1 are the minimum between the corresponding components in v1 and v2, and all components in v2 are the maximum.

For instance, XX(v1)=MIN(XX(v1), XX(v2)), XX(v2)=MAX(XX(v1), XX(v2)). Notice that both vectors are modified. This function is very useful for sorting two corners. After calling it you can certainly perform a non-empty for (from corner1 to corner2) loop.

Definition at line 1206 of file matrix1d.h.

1207 {
1208  T temp;
1209  if (!v1.sameShape(v2))
1211  "sortTwoVectors: vectors are not of the same shape");
1212 
1213  for (size_t j = 0; j < v1.vdim; j++)
1214  {
1215  temp = XMIPP_MIN(v1.vdata[j], v2.vdata[j]);
1216  v2.vdata[j] = XMIPP_MAX(v1.vdata[j], v2.vdata[j]);
1217  v1.vdata[j] = temp;
1218  }
1219 }
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
bool sameShape(const Matrix1D< T1 > &op) const
Definition: matrix1d.h:497
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
#define j
T * vdata
The array itself.
Definition: matrix1d.h:258
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ typeCast() [1/2]

template<typename T1 , typename T2 >
void typeCast ( const Matrix1D< T1 > &  v1,
Matrix1D< T2 > &  v2 
)

Conversion from one type to another. If we have an integer array and we need a double one, we can use this function. The conversion is done through a type casting of each element If n >= 0, only the nth volumes will be converted, otherwise all NSIZE volumes

Definition at line 1227 of file matrix1d.h.

1228 {
1229  if (v1.vdim == 0)
1230  {
1231  v2.clear();
1232  return;
1233  }
1234 
1235  v2.resizeNoCopy(v1.vdim);
1236  for (size_t j = 0; j < v1.vdim; j++)
1237  v2.vdata[j] = static_cast<T2>(v1.vdata[j]);
1238 }
void clear()
Definition: matrix1d.cpp:67
#define j
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

◆ typeCast() [2/2]

template<typename T1 >
void typeCast ( const Matrix1D< T1 > &  v1,
Matrix1D< T1 > &  v2 
)

Conversion from one type to another. In some cases, the two types are the same. So a faster way is simply by assignment.

Definition at line 1244 of file matrix1d.h.

1245 {
1246  v2 = v1;
1247 }
double v1

◆ vectorProduct() [1/2]

template<typename T >
Matrix1D<T> vectorProduct ( const Matrix1D< T > &  v1,
const Matrix1D< T > &  v2 
)

Vector product in R3. This function takes two R3 vectors and compute their vectorial product. For two vectors (V1x,V1y,V1z), (V2x, V2y, V2z) the result is (V1y*V2z-V1z*v2y, V1z*V2x-V1x*V2z, V1x*V2y-V1y*V2x). Pay attention that this operator is not conmutative. An exception is thrown if the vectors are not of the same shape or they don't belong to R3.

Matrix1D< T > X = vectorR3(1, 0, 0), Y = vector_R3(0, 1, 0);
std::cout << "X*Y=Z=" << vectorProduct(X,Y).transpose() << std::endl;

Definition at line 1165 of file matrix1d.h.

1166 {
1167  if (v1.vdim != 3 || v2.vdim != 3)
1168  REPORT_ERROR(ERR_MATRIX_SIZE, "Vector_product: vectors are not in R3");
1169 
1170  if (v1.isRow() != v2.isRow())
1172  "Vector_product: vectors are of different shape");
1173 
1174  Matrix1D<T> result(3);
1175  vectorProduct(v1, v2, result);
1176  return result;
1177 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Problem with matrix size.
Definition: xmipp_error.h:152
Matrix1D< T > vectorProduct(const Matrix1D< T > &v1, const Matrix1D< T > &v2)
Definition: matrix1d.h:1165
int isRow() const
Definition: matrix1d.h:520
Definition: ctf.h:38
size_t vdim
Number of elements.
Definition: matrix1d.h:264

◆ vectorProduct() [2/2]

template<typename T >
void vectorProduct ( const Matrix1D< T > &  v1,
const Matrix1D< T > &  v2,
Matrix1D< T > &  result 
)

Vector product in R3. This function computes the vector product of two R3 vectors. No check is performed, it is assumed that the output vector is already resized

Definition at line 1186 of file matrix1d.h.

1188 {
1189  XX(result) = YY(v1) * ZZ(v2) - ZZ(v1) * YY(v2);
1190  YY(result) = ZZ(v1) * XX(v2) - XX(v1) * ZZ(v2);
1191  ZZ(result) = XX(v1) * YY(v2) - YY(v1) * XX(v2);
1192 }
#define XX(v)
Definition: matrix1d.h:85
#define YY(v)
Definition: matrix1d.h:93
#define ZZ(v)
Definition: matrix1d.h:101

◆ vectorR2()

Matrix1D<double> vectorR2 ( double  x,
double  y 
)

Creates vector in R2. After this function the vector is (x,y) in R2.

Definition at line 884 of file matrix1d.cpp.

885 {
886  Matrix1D<double> result(2);
887  VEC_ELEM(result, 0) = x;
888  VEC_ELEM(result, 1) = y;
889  return result;
890 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
static double * y
doublereal * x

◆ vectorR3() [1/2]

Matrix1D<double> vectorR3 ( double  x,
double  y,
double  z 
)

Creates vector in R3. After this function the vector is (x,y,z) in R3.

Definition at line 892 of file matrix1d.cpp.

893 {
894  Matrix1D<double> result(3);
895  VEC_ELEM(result, 0) = x;
896  VEC_ELEM(result, 1) = y;
897  VEC_ELEM(result, 2) = z;
898  return result;
899 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
static double * y
doublereal * x
double z

◆ vectorR3() [2/2]

Matrix1D<int> vectorR3 ( int  x,
int  y,
int  z 
)

Creates an integer vector in Z3.

Definition at line 901 of file matrix1d.cpp.

902 {
903  Matrix1D<int> result(3);
904  VEC_ELEM(result, 0) = x;
905  VEC_ELEM(result, 1) = y;
906  VEC_ELEM(result, 2) = z;
907  return result;
908 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
static double * y
doublereal * x
double z