ATSuite C++  v1.0
Scientific C++ routines originally developed by Alexis Tantet
atmatrix.hpp
Go to the documentation of this file.
1 #ifndef ATMATRIX_HPP
2 #define ATMATRIX_HPP
3 
4 #include <vector>
5 #include <gsl/gsl_vector.h>
6 #include <gsl/gsl_vector_int.h>
7 #include <gsl/gsl_matrix.h>
8 #include <Eigen/Dense>
9 #include <Eigen/Sparse>
10 
17 // Typedef definitions
19 typedef Eigen::SparseMatrix<double, Eigen::RowMajor> SpMatCSR;
21 typedef Eigen::SparseMatrix<int, Eigen::RowMajor> SpMatIntCSR;
23 typedef Eigen::SparseMatrix<double, Eigen::ColMajor> SpMatCSC;
25 typedef Eigen::SparseMatrix<bool, Eigen::ColMajor> SpMatCSCBool;
26 
27 // Declarations
28 gsl_vector* getRowSum(SpMatCSR *);
29 void getRowSum(SpMatCSR *, gsl_vector *);
30 gsl_vector_int* getRowSum(SpMatIntCSR *);
31 gsl_vector* getColSum(SpMatCSR *);
32 void getColSum(SpMatCSR *, gsl_vector *);
33 gsl_vector* getColSum(SpMatCSC *);
34 double getSum(SpMatCSR *);
35 double sumVectorElements(gsl_vector *);
36 void normalizeVector(gsl_vector *);
37 void normalizeRows(SpMatCSR *, gsl_vector *);
38 SpMatCSCBool * cwiseGT(SpMatCSC *, double);
39 SpMatCSCBool * cwiseLT(SpMatCSC *, double);
40 bool any(SpMatCSCBool *);
41 double max(SpMatCSC *);
42 double min(SpMatCSC *);
43 std::vector<int> * argmax(SpMatCSC *);
44 std::vector<int> * argmin(SpMatCSC *);
45 
46 
47 // Definitions
55 gsl_vector*
56 getRowSum(SpMatCSR *T)
57 {
58  int N = T->rows();
59  gsl_vector *rowSum = gsl_vector_calloc(N);
60 
61  // Calculate row sums
62  for (int i = 0; i < N; i++)
63  for (SpMatCSR::InnerIterator it(*T, i); it; ++it)
64  gsl_vector_set(rowSum, i, gsl_vector_get(rowSum, i)+it.value());
65 
66  return rowSum;
67 }
68 
77 void
78 getRowSum(SpMatCSR *T, gsl_vector *rowSum)
79 {
80  int N = T->rows();
81 
82  // Calculate row sums
83  gsl_vector_set_all(rowSum, 0.);
84  for (int i = 0; i < N; i++)
85  for (SpMatCSR::InnerIterator it(*T, i); it; ++it)
86  gsl_vector_set(rowSum, i, gsl_vector_get(rowSum, i)+it.value());
87 
88  return;
89 }
90 
98 gsl_vector_int *
100 {
101  int N = T->rows();
102  gsl_vector_int *rowSum = gsl_vector_int_calloc(N);
103  // Calculate row sums
104  for (int i = 0; i < N; i++)
105  for (SpMatIntCSR::InnerIterator it(*T, i); it; ++it)
106  gsl_vector_int_set(rowSum, i, gsl_vector_int_get(rowSum, i)+it.value());
107 
108  return rowSum;
109 }
110 
118 gsl_vector *
119 getColSum(SpMatCSR *T)
120 {
121  int N = T->rows();
122  gsl_vector *colSum = gsl_vector_calloc(N);
123 
124  // Calculate col sums
125  for (int irow = 0; irow < N; irow++)
126  for (SpMatCSR::InnerIterator it(*T, irow); it; ++it)
127  gsl_vector_set(colSum, it.col(),
128  gsl_vector_get(colSum, it.col()) + it.value());
129 
130  return colSum;
131 }
132 
141 void
142 getColSum(SpMatCSR *T, gsl_vector *colSum)
143 {
144  int N = T->rows();
145 
146  // Calculate col sums
147  gsl_vector_set_all(colSum, 0.);
148  for (int irow = 0; irow < N; irow++)
149  for (SpMatCSR::InnerIterator it(*T, irow); it; ++it)
150  gsl_vector_set(colSum, it.col(),
151  gsl_vector_get(colSum, it.col()) + it.value());
152 
153  return;
154 }
155 
163 gsl_vector *
165 {
166  int N = T->rows();
167  gsl_vector *colSum = gsl_vector_calloc(N);
168  // Calculate col sums
169  for (int icol = 0; icol < N; icol++)
170  for (SpMatCSC::InnerIterator it(*T, icol); it; ++it)
171  gsl_vector_set(colSum, icol, gsl_vector_get(colSum, icol) + it.value());
172 
173  return colSum;
174 }
175 
183 double
184 getSum(SpMatCSR *T)
185 {
186  int N = T->rows();
187  double sum = 0.;
188  // Calculate col sums
189  for (int irow = 0; irow < N; irow++)
190  for (SpMatCSR::InnerIterator it(*T, irow); it; ++it)
191  sum += it.value();
192 
193  return sum;
194 }
195 
196 
204 double
205 sumVectorElements(gsl_vector *v)
206 {
207  double sum = 0.;
208 
209  for (size_t i = 0; i < v->size; i++)
210  sum += gsl_vector_get(v, i);
211 
212  return sum;
213 }
214 
221 void
222 normalizeVector(gsl_vector *v)
223 {
224  double sum;
225 
226  sum = sumVectorElements(v);
227  gsl_vector_scale(v, 1. / sum);
228 
229  return;
230 }
231 
239 void
240 normalizeRows(SpMatCSR *T, gsl_vector *rowSum)
241 {
242  double rowSumi;
243  for (int i = 0; i < T->rows(); i++){
244  rowSumi = gsl_vector_get(rowSum, i);
245  for (SpMatCSR::InnerIterator it(*T, i); it; ++it)
246  if (rowSumi > 0.)
247  it.valueRef() /= rowSumi;
248  }
249  return ;
250 }
251 
260 SpMatCSCBool *
261 cwiseGT(SpMatCSC *T, double ref)
262 {
263  int j;
264  SpMatCSCBool *cmpT = new SpMatCSCBool(T->rows(), T->cols());
265  for (j = 0; j < T->cols(); j++)
266  for (SpMatCSC::InnerIterator it(*T, j); it; ++it)
267  cmpT->insert(it.row(), j) = it.value() > ref;
268 
269  return cmpT;
270 }
271 
280 SpMatCSCBool *
281 cwiseLT(SpMatCSC *T, double ref)
282 {
283  int j;
284  SpMatCSCBool *cmpT = new SpMatCSCBool(T->rows(), T->cols());
285  for (j = 0; j < T->cols(); j++)
286  for (SpMatCSC::InnerIterator it(*T, j); it; ++it)
287  cmpT->insert(it.row(), j) = it.value() < ref;
288 
289  return cmpT;
290 }
291 
299 bool
301 {
302  int j;
303  for (j = 0; j < T->cols(); j++)
304  for (SpMatCSCBool::InnerIterator it(*T, j); it; ++it)
305  if (it.value())
306  return true;
307 
308  return false;
309 }
310 
318 double
320 {
321  int j;
322  SpMatCSC::InnerIterator it(*T, 0);
323  double maxValue = it.value();
324  for (j = 0; j < T->cols(); j++)
325  for (SpMatCSC::InnerIterator it(*T, j); it; ++it)
326  if (it.value() > maxValue)
327  maxValue = it.value();
328 
329  return maxValue;
330 }
331 
332 
340 double
342 {
343  int j;
344  SpMatCSC::InnerIterator it(*T, 0);
345  double minValue = it.value();
346  for (j = 0; j < T->cols(); j++)
347  for (SpMatCSC::InnerIterator it(*T, j); it; ++it)
348  if (it.value() < minValue)
349  minValue = it.value();
350 
351  return minValue;
352 }
353 
354 
362 std::vector<int> *
364 {
365  int j;
366  std::vector<int> *argmax = new std::vector<int>(2);
367  SpMatCSC::InnerIterator it(*T, 0);
368  double maxValue = it.value();
369  for (j = 0; j < T->cols(); j++){
370  for (SpMatCSC::InnerIterator it(*T, j); it; ++it){
371  if (it.value() > maxValue){
372  argmax->at(0) = it.row();
373  argmax->at(1) = it.col();
374  maxValue = it.value();
375  }
376  }
377  }
378 
379  return argmax;
380 }
381 
389 std::vector<int> *
391 {
392  int j;
393  std::vector<int> *argmin = new std::vector<int>(2);
394  SpMatCSC::InnerIterator it(*T, 0);
395  double minValue = it.value();
396  for (j = 0; j < T->cols(); j++){
397  for (SpMatCSC::InnerIterator it(*T, j); it; ++it){
398  if (it.value() < minValue){
399  argmin->at(0) = it.row();
400  argmin->at(1) = it.col();
401  minValue = it.value();
402  }
403  }
404  }
405 
406  return argmin;
407 }
408 
409 
410 #endif
bool any(SpMatCSCBool *)
Perform an any operation on an Eigen CSC matrix of boolean type.
Definition: atmatrix.hpp:300
Eigen::SparseMatrix< double, Eigen::RowMajor > SpMatCSR
Eigen CSR matrix of double type.
Definition: atmatrix.hpp:19
SpMatCSCBool * cwiseGT(SpMatCSC *, double)
Perform an element-wise greater than test on an Eigen CSC matrix.
Definition: atmatrix.hpp:261
SparseMatrix< bool, ColMajor > SpMatCSCBool
Eigen CSC matrix of boolean type.
Definition: atmarkov.hpp:31
std::vector< int > * argmax(SpMatCSC *)
Finds the position of the maximum element of an Eigen CSC matrix.
Definition: atmatrix.hpp:363
double min(SpMatCSC *)
Finds the minimum element of an Eigen CSC matrix.
Definition: atmatrix.hpp:341
gsl_vector * getColSum(SpMatCSR *)
Get the sum of each column of an Eigen CSR matrix.
Definition: atmatrix.hpp:119
SpMatCSCBool * cwiseLT(SpMatCSC *, double)
Perform an element-wise lower than test on an Eigen CSC matrix.
Definition: atmatrix.hpp:281
gsl_vector * getRowSum(SpMatCSR *)
Get the sum of each row of an Eigen CSR matrix.
Definition: atmatrix.hpp:56
double sumVectorElements(gsl_vector *)
Returns a the sum of the elements of an GSL vector.
Definition: atmatrix.hpp:205
std::vector< int > * argmin(SpMatCSC *)
Finds the position of the minimum element of an Eigen CSC matrix.
Definition: atmatrix.hpp:390
double getSum(SpMatCSR *)
Returns a the sum of the elements of an Eigen CSR matrix.
Definition: atmatrix.hpp:184
SparseMatrix< int, RowMajor > SpMatIntCSR
Eigen CSR matrix of integer type.
Definition: atmarkov.hpp:27
Eigen::SparseMatrix< int, Eigen::RowMajor > SpMatIntCSR
Eigen CSR matrix of integer type.
Definition: atmatrix.hpp:21
double max(SpMatCSC *)
Finds the maximum element of an Eigen CSC matrix.
Definition: atmatrix.hpp:319
Eigen::SparseMatrix< bool, Eigen::ColMajor > SpMatCSCBool
Eigen CSC matrix of boolean type.
Definition: atmatrix.hpp:25
void normalizeVector(gsl_vector *)
Normalize a GSL vector by the sum of its elements.
Definition: atmatrix.hpp:222
Eigen::SparseMatrix< double, Eigen::ColMajor > SpMatCSC
Eigen CSC matrix of double type.
Definition: atmatrix.hpp:23
void normalizeRows(SpMatCSR *, gsl_vector *)
Normalize each row of an Eigen CSR matrix by a GSL vector.
Definition: atmatrix.hpp:240
Eigen::SparseMatrix< double, Eigen::ColMajor > SpMatCSC
Eigen sparse CSC matrix of double type.
Definition: atio.hpp:27