Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | Friends | List of all members
SparseMatrix2D Class Reference

#include <sparse_matrix2d.h>

Collaboration diagram for SparseMatrix2D:
Collaboration graph
[legend]

Public Member Functions

int nrows () const
 Y size of the matrix. More...
 
int ncols () const
 X size of the matrix. More...
 
 SparseMatrix2D (std::vector< SparseElement > &_elements, int _Nelements)
 
void sparseMatrix2DFromVector (std::vector< SparseElement > &_elements)
 Fill the sparse matrix A with the elements of the vector. More...
 
void multMv (double *x, double *y)
 
void multMM (const SparseMatrix2D &X, SparseMatrix2D &Y)
 Computes Y=this*X. More...
 
void multMMDiagonal (const MultidimArray< double > &D, SparseMatrix2D &Y)
 Computes y=SparseMatrixThis*SparseMatrix. More...
 
double getElemIJ (int row, int col) const
 
void loadMatrix (const FileName &fn)
 

Public Attributes

int N =0
 The matrix is of size NxN. More...
 
MultidimArray< int > iIdx
 List of i positions. More...
 
MultidimArray< int > jIdx
 List of j positions. More...
 
MultidimArray< double > values
 List of values. More...
 

Friends

std::ostream & operator<< (std::ostream &out, const SparseMatrix2D &X)
 Shows the dense Matrix associated. More...
 

Detailed Description

Square, sparse matrices.

To create a Sparse Matrix we need a vector with SparseElements that contains a value, its row position and its column position.

We store that information with a Compressed Row Storage (CRS). This method stores a vector with the non-zero values (values), their column position (jIdx) and another vector with the position on values's vector of the element which is the first non-zero elements of each row (iIdx). The value 0 on iIdx vector means that there aren't nonzero values in that row. iIdx and jIdx have the first element in position 1. Value 0 is for no elements on a row.

i.e.:

Vector values 4, 3, 1, 2, 1, 4, 3

Vector jIdX 1, 2, 1, 2, 2, 4, 4

Vector iIdx 1, 3, 5, 7

Matriz A: 4 3 0 0 1 2 0 0 0 1 0 4 0 0 0 3

Definition at line 80 of file sparse_matrix2d.h.

Constructor & Destructor Documentation

◆ SparseMatrix2D()

SparseMatrix2D::SparseMatrix2D ( std::vector< SparseElement > &  _elements,
int  _Nelements 
)

Constructor from a set of i,j indexes and their corresponding values. N is the total dimension of the square, sparse matrix.

Definition at line 32 of file sparse_matrix2d.cpp.

33 {
34  //First of all, we sort the elements by rows and then by columns
35  std::sort(_elements.begin(),_elements.end());
36 
37  size_t ln = _elements.size();
38  N = _Nelements;
39 
40  values.resizeNoCopy(ln);
41  jIdx.resizeNoCopy(ln);
43 
44  int actualRow = -1;
45  int i = 0; // Iterator for the vectors "values" and "jIdx"
46 
47  for (size_t k=0 ; k< ln ; ++k )// Iterator for vector of SparseElemets
48  {
49  if(_elements.at(k).value != 0.0) // Searching that there isn't any zero value
50  {
51  DIRECT_MULTIDIM_ELEM(values,i) = _elements.at(k).value;
52  DIRECT_MULTIDIM_ELEM(jIdx,i) = _elements.at(k).j +1;
53 
54  int rse = _elements.at(k).i;
55  while( rse > actualRow )
56  {
57  actualRow++;
58  if( rse == actualRow )
59  // The first element in row number "x" is in position iIdx(x-1)
60  DIRECT_MULTIDIM_ELEM(iIdx,actualRow) = i+1;
61 
62  else
63  // If there isn't any nonzero value on this row, the value in this vector is 0
64  DIRECT_MULTIDIM_ELEM(iIdx,actualRow) = 0;
65  }
66  ++i;
67  }
68  }
69 }
MultidimArray< double > values
List of values.
void resizeNoCopy(const MultidimArray< T1 > &v)
#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
MultidimArray< int > iIdx
List of i positions.
#define DIRECT_MULTIDIM_ELEM(v, n)
void sort(struct DCEL_T *dcel)
Definition: sorting.cpp:18
int N
The matrix is of size NxN.
MultidimArray< int > jIdx
List of j positions.

Member Function Documentation

◆ getElemIJ()

double SparseMatrix2D::getElemIJ ( int  row,
int  col 
) const

Returns the value on position (row,col). The matrix goes from 0 to N-1

Get element

Definition at line 157 of file sparse_matrix2d.cpp.

158 {
159  int rowBeg = DIRECT_MULTIDIM_ELEM(iIdx,row) -1;
160  int rowEnd;
161 
162  if( row != N-1 )
163  rowEnd = DIRECT_MULTIDIM_ELEM(iIdx,row+1) -1;
164  else
165  rowEnd = XSIZE(values);
166 
167  // If there is a non-zero element, the column is in jIdx
168  for(int i = rowBeg; i < rowEnd ; i++)
169  if( DIRECT_MULTIDIM_ELEM(jIdx,i)-1 == col )
170  return DIRECT_MULTIDIM_ELEM(values,i);
171  return 0.0;
172 }
MultidimArray< double > values
List of values.
#define i
#define XSIZE(v)
MultidimArray< int > iIdx
List of i positions.
#define DIRECT_MULTIDIM_ELEM(v, n)
int N
The matrix is of size NxN.
MultidimArray< int > jIdx
List of j positions.

◆ loadMatrix()

void SparseMatrix2D::loadMatrix ( const FileName fn)

Loads a sparse matrix from a file. Loads a SparseMatrix2D from a file which has the following format:

sizeOfMatrix 1 1 value 2 1 value . . . i j value . . . last_i last_j last_value

Definition at line 258 of file sparse_matrix2d.cpp.

259 {
260  std::ifstream fhIn;
261  fhIn.open(fn.c_str());
262  if (!fhIn)
264 
265  double dobVecSize, auxDob;
266  int vectorSize = 0;
267  fhIn >> dobVecSize;
268  vectorSize = (int)dobVecSize ;
269 
270  std::vector<SparseElement> elems(vectorSize);
271  for(int i =0; i< vectorSize; ++i){
272  fhIn >> auxDob;
273  elems.at(i).i = (int) auxDob -1;
274 
275  fhIn >> auxDob;
276  elems.at(i).j = (int) auxDob -1;
277 
278  fhIn >> elems.at(i).value;
279  }
281 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define i
void sparseMatrix2DFromVector(std::vector< SparseElement > &_elements)
Fill the sparse matrix A with the elements of the vector.
File or directory does not exist.
Definition: xmipp_error.h:136

◆ multMM()

void SparseMatrix2D::multMM ( const SparseMatrix2D X,
SparseMatrix2D Y 
)

Computes Y=this*X.

Computes y=SparseMatrixThis*SparseMatrixX.

Definition at line 175 of file sparse_matrix2d.cpp.

176 {
177  size_t nnz=XSIZE(X.values);
178 
179  Y.N = X.N;
180  Y.values.initZeros(nnz);
181  Y.jIdx.initZeros(nnz);
182  Y.iIdx.initZeros(N);
183 
184  size_t nnzY = 0;
185  int Nelems = X.N;
186  for(int row = 0; row < Nelems ; ++row){
187  int firstElemRow = 0;
188  for(int col = 0; col < Nelems ; ++col){
189  double aij = 0.0;
190  for(int k = 0; k < Nelems ; ++k ){
191  double val=getElemIJ(row, k);
192  if(val != 0.0){ // If GetElemIJ(row, k) is 0, the product is 0.
193  double val2=X.getElemIJ(k,col);
194  aij += val * val2;
195  }
196  }
197 
198  if(aij != 0.0){ // If there is a nonzero element in (row,col) position, we include that in the matrix
199  if(firstElemRow == 0)
200  firstElemRow = nnzY +1;
201  DIRECT_MULTIDIM_ELEM(Y.values,nnzY) = aij;
202  DIRECT_MULTIDIM_ELEM(Y.jIdx,nnzY) = col+1;
203  ++nnzY;
204  }
205  }
206  // Indicates where is the first element of the row
207  DIRECT_MULTIDIM_ELEM(Y.iIdx,row) = firstElemRow;
208  }
209  Y.values.resize(nnzY);
210  Y.jIdx.resize(nnzY);
211 }
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
MultidimArray< double > values
List of values.
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 XSIZE(v)
MultidimArray< int > iIdx
List of i positions.
#define DIRECT_MULTIDIM_ELEM(v, n)
int N
The matrix is of size NxN.
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< int > jIdx
List of j positions.
double getElemIJ(int row, int col) const

◆ multMMDiagonal()

void SparseMatrix2D::multMMDiagonal ( const MultidimArray< double > &  D,
SparseMatrix2D Y 
)

Computes y=SparseMatrixThis*SparseMatrix.

Computes Y=this*D where D is a diagonal matrix. It is assumed that the size of this sparse matrix is NxN and that the length of y is N.

Definition at line 227 of file sparse_matrix2d.cpp.

228 {
229  int actualRow = N-1;
230 
231  Y=*this;
232 
233  // We see where is the first element in the next row
234  int until = DIRECT_MULTIDIM_ELEM(iIdx,actualRow) -1;
235 
236  // Obtain the value that we have to multiply by the first row.
237  double dx = DIRECT_MULTIDIM_ELEM(D,actualRow);
238  size_t nnz=XSIZE(values);
239  for(int i = nnz-1; i >= 0 ; --i)
240  {
241  // Yij = Aij * Dii / Y = A => Yij = Yij * Dii
243 
244  if(until == i && i > 0){
245  --actualRow;
246  // Search for a row with nonzero elements
247  while( DIRECT_MULTIDIM_ELEM(iIdx,actualRow) == 0 )
248  --actualRow;
249 
250  until = DIRECT_MULTIDIM_ELEM(iIdx,actualRow) -1;
251  dx = DIRECT_MULTIDIM_ELEM(D,actualRow);
252  }
253  }
254 }
MultidimArray< double > values
List of values.
#define i
#define XSIZE(v)
MultidimArray< int > iIdx
List of i positions.
double dx
#define DIRECT_MULTIDIM_ELEM(v, n)
int N
The matrix is of size NxN.

◆ multMv()

void SparseMatrix2D::multMv ( double *  x,
double *  y 
)

Computes y=this*x y and x are vectors of size Nx1

It computes y <- this*x

Definition at line 113 of file sparse_matrix2d.cpp.

114 {
115  int col, rowEnd, rowBeg;
116  double val;
117  memset(y,0,N*sizeof(double));
118 
119  size_t nnz=XSIZE(values);
120  for(int i = 0; i< N; i++)
121  {
122  // We get the gap where are the elements of the row i in value's vector
123  rowBeg = DIRECT_MULTIDIM_ELEM(iIdx,i) -1;
124  if( i != N-1 )
125  rowEnd = DIRECT_MULTIDIM_ELEM(iIdx,i+1) -1;
126  else
127  rowEnd = nnz;
128 
129  val = 0.0;
130  for(int j = rowBeg; j < rowEnd ; j++)
131  {
132  col = DIRECT_MULTIDIM_ELEM(jIdx,j) -1;// Column with a nonzero element in this row of the matrix
133  val += DIRECT_MULTIDIM_ELEM(values,j) * x[col];
134  }
135  y[i] = val;
136  }
137 }
MultidimArray< double > values
List of values.
static double * y
doublereal * x
#define i
#define XSIZE(v)
MultidimArray< int > iIdx
List of i positions.
#define DIRECT_MULTIDIM_ELEM(v, n)
#define j
int N
The matrix is of size NxN.
MultidimArray< int > jIdx
List of j positions.

◆ ncols()

int SparseMatrix2D::ncols ( ) const
inline

X size of the matrix.

Definition at line 100 of file sparse_matrix2d.h.

101  {
102  return N;
103  }
int N
The matrix is of size NxN.

◆ nrows()

int SparseMatrix2D::nrows ( ) const
inline

Y size of the matrix.

Definition at line 94 of file sparse_matrix2d.h.

95  {
96  return N;
97  }
int N
The matrix is of size NxN.

◆ sparseMatrix2DFromVector()

void SparseMatrix2D::sparseMatrix2DFromVector ( std::vector< SparseElement > &  _elements)

Fill the sparse matrix A with the elements of the vector.

Definition at line 74 of file sparse_matrix2d.cpp.

75 {
76  std::sort(_elements.begin(),_elements.end());
77 
78  size_t ln = _elements.size();
79  N = _elements.at(ln-1).j;
80  values.resizeNoCopy(ln);
81  jIdx.resizeNoCopy(ln);
83 
84  int actualRow = -1;
85  int i = 0;
86 
87  for (size_t k=0 ; k< ln ; ++k )
88  {
89  if(_elements.at(k).value != 0.0) // Seaching that there isn't any zero value
90  {
91  DIRECT_MULTIDIM_ELEM(values,i) = _elements.at(k).value;
92  DIRECT_MULTIDIM_ELEM(jIdx,i) = _elements.at(k).j +1;
93 
94  int rse = _elements.at(k).i;
95  while( rse > actualRow )
96  {
97  actualRow++;
98  if( rse == actualRow )
99  DIRECT_MULTIDIM_ELEM(iIdx,actualRow) = i+1;
100 
101  else
102  DIRECT_MULTIDIM_ELEM(iIdx,actualRow) = 0;
103  }
104  ++i;
105  }
106  }
107  N = actualRow+1;
108 }
MultidimArray< double > values
List of values.
void resizeNoCopy(const MultidimArray< T1 > &v)
#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
MultidimArray< int > iIdx
List of i positions.
#define DIRECT_MULTIDIM_ELEM(v, n)
void sort(struct DCEL_T *dcel)
Definition: sorting.cpp:18
int N
The matrix is of size NxN.
MultidimArray< int > jIdx
List of j positions.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const SparseMatrix2D X 
)
friend

Shows the dense Matrix associated.

Definition at line 142 of file sparse_matrix2d.cpp.

143 {
144  int N=X.nrows();
145  for(int i =0 ; i< N ; i++)
146  {
147  for(int j =0; j<N ; j++)
148  out << X.getElemIJ(i, j) << "\t";
149  out << std::endl;
150  }
151  return out;
152 }
#define i
int nrows() const
Y size of the matrix.
#define j
int N
The matrix is of size NxN.
double getElemIJ(int row, int col) const

Member Data Documentation

◆ iIdx

MultidimArray<int> SparseMatrix2D::iIdx

List of i positions.

Definition at line 87 of file sparse_matrix2d.h.

◆ jIdx

MultidimArray<int> SparseMatrix2D::jIdx

List of j positions.

Definition at line 89 of file sparse_matrix2d.h.

◆ N

int SparseMatrix2D::N =0

The matrix is of size NxN.

Definition at line 84 of file sparse_matrix2d.h.

◆ values

MultidimArray<double> SparseMatrix2D::values

List of values.

Definition at line 91 of file sparse_matrix2d.h.


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