IVSparse  v1.0
A sparse matrix compression library.
IVCSC_BLAS.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 namespace IVSparse {
12 
13 //* BLAS Level 1 Routines *//
14 
15 // Scalar Multiply
16 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
17 inline IVSparse::SparseMatrix<T, indexT, compressionLevel, columnMajor> SparseMatrix<T, indexT, compressionLevel, columnMajor>::scalarMultiply(T scalar) {
18  // Deep copy the matrix
20 
21  // else use the iterator
22  #ifdef IVSPARSE_HAS_OPENMP
23  #pragma omp parallel for
24  #endif
25  for (uint32_t i = 0; i < this->outerDim; ++i) {
26  for (typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator it(newMatrix, i); it; ++it) {
27  if (it.isNewRun()) {
28  it.coeff(it.value() * scalar);
29  }
30  }
31  }
32  return newMatrix;
33 }
34 
35 // In Place Scalar Multiply
36 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
37 inline void SparseMatrix<T, indexT, compressionLevel, columnMajor>::inPlaceScalarMultiply(T scalar) {
38 
39  // else use the iterator
40  #ifdef IVSPARSE_HAS_OPENMP
41  #pragma omp parallel for
42  #endif
43  for (uint32_t i = 0; i < outerDim; ++i) {
44  for (typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator it(*this, i); it; ++it) {
45  if (it.isNewRun()) {
46  it.coeff(it.value() * scalar);
47  }
48  }
49  }
50 }
51 
52 //* BLAS Level 2 Routines *//
53 
54 // Matrix Vector Multiplication (Eigen::VectorXd * IVSparse::SparseMatrix)
55 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
56 inline Eigen::Matrix<T, -1, 1> SparseMatrix<T, indexT, compressionLevel, columnMajor>::vectorMultiply(Eigen::Matrix<T, -1, 1> vec) {
57 
58  #ifdef IVSPARSE_DEBUG
59  // check that the vector is the correct size
60  assert(vec.rows() == outerDim &&
61  "The vector must be the same size as the number of columns in the "
62  "matrix!");
63  #endif
64 
65  Eigen::Matrix<T, -1, 1> eigenTemp = Eigen::Matrix<T, -1, 1>::Zero(innerDim, 1);
66 
67  // iterate over the vector and multiply the corresponding row of the matrix by
68  // the vecIter value
69 
70  for (uint32_t i = 0; i < outerDim; i++) {
71  for (typename SparseMatrix<T, indexT, 3, columnMajor>::InnerIterator
72  matIter(*this, i);
73  matIter; ++matIter) {
74  eigenTemp(matIter.row()) += vec(matIter.col()) * matIter.value();
75  }
76  }
77  return eigenTemp;
78 }
79 
80 // Matrix Vector Multiplication (IVSparse::SparseMatrix::Vector *
81 // IVSparse::SparseMatrix)
82 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
83 inline Eigen::Matrix<T, -1, 1> SparseMatrix<T, indexT, compressionLevel, columnMajor>::vectorMultiply(
84  typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::Vector &vec) {
85 
86  #ifdef IVSPARSE_DEBUG
87  if (vec.length() != outerDim)
88  throw std::invalid_argument(
89  "The vector must be the same size as the number of columns in the "
90  "matrix!");
91  #endif
92 
93  Eigen::Matrix<T, -1, 1> eigenTemp = Eigen::Matrix<T, -1, 1>::Zero(innerDim, 1);
94 
95  for (typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator vecIter(vec);
96  vecIter; ++vecIter) {
97  for (typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator matIter(*this, vecIter.row());
98  matIter; ++matIter) {
99  eigenTemp(matIter.row()) += matIter.value() * vecIter.value();
100  }
101  }
102  return eigenTemp;
103 }
104 
105 //* BLAS Level 3 Routines *//
106 // Matrix multiplication has been moved to the IVCSC_Operator.hpp file
107 
108 //* Other Matrix Calculations *//
109 
110 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
112  std::vector<T> outerSum = std::vector<T>(outerDim);
113 
114  #ifdef IVSPARSE_HAS_OPENMP
115  #pragma omp parallel for
116  #endif
117  for (int i = 0; i < outerDim; i++) {
118  for (typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator it(*this, i); it; ++it) {
119  outerSum[i] += it.value();
120  }
121  }
122  return outerSum;
123 }
124 
125 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
127  std::vector<T> innerSum = std::vector<T>(innerDim);
128 
129  #ifdef IVSPARSE_HAS_OPENMP
130  #pragma omp parallel for
131  #endif
132  for (int i = 0; i < outerDim; i++) {
134  it; ++it) {
135  innerSum[it.row()] += it.value();
136  }
137  }
138  return innerSum;
139 }
140 
141 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
143 
144  std::vector<T> maxCoeff = std::vector<T>(innerDim);
145 
146  #ifdef IVSPARSE_HAS_OPENMP
147  #pragma omp parallel for
148  #endif
149  for (int i = 0; i < outerDim; i++) {
151  it; ++it) {
152  if (it.value() > maxCoeff[i]) {
153  maxCoeff[i] = it.value();
154  }
155  }
156  }
157  return maxCoeff;
158 }
159 
160 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
162 
163  std::vector<T> maxCoeff = std::vector<T>(innerDim);
164 
165  #ifdef IVSPARSE_HAS_OPENMP
166  #pragma omp parallel for
167  #endif
168  for (int i = 0; i < outerDim; i++) {
170  it; ++it) {
171  if (it.value() > maxCoeff[it.row()]) {
172  maxCoeff[it.row()] = it.value();
173  }
174  }
175  }
176  return maxCoeff;
177 }
178 
179 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
181 
182  std::vector<T> minCoeff = std::vector<T>(innerDim);
183 
184  #ifdef IVSPARSE_HAS_OPENMP
185  #pragma omp parallel for
186  #endif
187  for (int i = 0; i < outerDim; i++) {
189  it; ++it) {
190  if (it.value() < minCoeff[i]) {
191  minCoeff[i] = it.value();
192  }
193  }
194  }
195  return minCoeff;
196 }
197 
198 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
200  std::vector<T> minCoeff = std::vector<T>(innerDim);
201  memset(minCoeff.data(), 0xF, innerDim * sizeof(T));
202 
203  #ifdef IVSPARSE_HAS_OPENMP
204  #pragma omp parallel for
205  #endif
206  for (int i = 0; i < outerDim; i++) {
208  it; ++it) {
209  if (it.value() < minCoeff[it.row()]) {
210  minCoeff[it.row()] = it.value();
211  }
212  }
213  }
214  return minCoeff;
215 }
216 
217 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
219 
220  #ifdef IVSPARSE_DEBUG
221  assert(innerDim == outerDim && "Trace is only defined for square matrices!");
222  #endif
223 
224  T trace = 0;
225  for (int i = 0; i < outerDim; i++) {
227  it; ++it) {
228  if (it.row() == i) {
229  trace += it.value();
230  } else if (it.row() > i) {
231  continue;
232  }
233  }
234  }
235  return trace;
236 }
237 
238 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
240  T sum = 0;
241 
242  #ifdef IVSPARSE_HAS_OPENMP
243  #pragma omp parallel for reduction(+ : sum)
244  #endif
245  for (int i = 0; i < outerDim; i++) {
246  for (typename SparseMatrix<T, indexT, compressionLevel,
247  columnMajor>::InnerIterator it(*this, i);
248  it; ++it) {
249  sum += it.value();
250  }
251  }
252  return sum;
253 }
254 
255 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
257  double norm = 0;
258 
259  #ifdef IVSPARSE_HAS_OPENMP
260  #pragma omp parallel for reduction(+ : norm)
261  #endif
262  for (int i = 0; i < outerDim; i++) {
264  it; ++it) {
265  norm += it.value() * it.value();
266  }
267  }
268  return sqrt(norm);
269 }
270 
271 template <typename T, typename indexT, uint8_t compressionLevel, bool columnMajor>
273 
274  #ifdef IVSPARSE_DEBUG
275  assert(col < outerDim && col >= 0 && "The column index is out of bounds!");
276  #endif
277 
278  double norm = 0;
279 
281  it; ++it) {
282  norm += it.value() * it.value();
283  }
284  return sqrt(norm);
285 }
286 
287 } // namespace IVSparse
Definition: IVCSC_Iterator.hpp:25
Definition: IVCSC_SparseMatrix.hpp:29
std::vector< T > maxRowCoeff()
Definition: IVCSC_BLAS.hpp:161
std::vector< T > minRowCoeff()
Definition: IVCSC_BLAS.hpp:199
std::vector< T > innerSum()
Definition: IVCSC_BLAS.hpp:126
T sum()
Definition: IVCSC_BLAS.hpp:239
double norm()
Definition: IVCSC_BLAS.hpp:256
std::vector< T > minColCoeff()
Definition: IVCSC_BLAS.hpp:180
std::vector< T > outerSum()
Definition: IVCSC_BLAS.hpp:111
double vectorLength(uint32_t vec)
Definition: IVCSC_BLAS.hpp:272
std::vector< T > maxColCoeff()
Definition: IVCSC_BLAS.hpp:142
T trace()
Definition: IVCSC_BLAS.hpp:218