IVSparse  v1.0
A sparse matrix compression library.
VCSC_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, bool columnMajor>
17 inline IVSparse::SparseMatrix<T, indexT, 2, columnMajor> SparseMatrix<T, indexT, 2, columnMajor>::scalarMultiply(T scalar) {
18  // Deep copy the matrix
20 
21  // If performance vectors are active use them for the scalar multiplication
22  #ifdef IVSPARSE_HAS_OPENMP
23  #pragma omp parallel for
24  #endif
25  for (int i = 0; i < outerDim; i++) {
26  for (int j = 0; j < valueSizes[i]; j++) {
27  newMatrix.values[i][j] *= scalar;
28  }
29  }
30  return newMatrix;
31 }
32 
33 // In Place Scalar Multiply
34 template <typename T, typename indexT, bool columnMajor>
35 inline void SparseMatrix<T, indexT, 2, columnMajor>::inPlaceScalarMultiply(T scalar) {
36  // if performance vectors are active use them for the scalar multiplication
37  #ifdef IVSPARSE_HAS_OPENMP
38  #pragma omp parallel for
39  #endif
40  for (int i = 0; i < outerDim; i++) {
41  for (int j = 0; j < valueSizes[i]; j++) {
42  values[i][j] *= scalar;
43  }
44  }
45 }
46 
47 //* BLAS Level 2 Routines *//
48 
49 // Matrix Vector Multiplication (IVSparse::SparseMatrix * Eigen::Vector)
50 template <typename T, typename indexT, bool columnMajor>
51 inline Eigen::Matrix<T, -1, 1> SparseMatrix<T, indexT, 2, columnMajor>::vectorMultiply(Eigen::Matrix<T, -1, 1> &vec) {
52 
53  #ifdef IVSPARSE_DEBUG
54  // check that the vector is the correct size
55  assert(vec.rows() == outerDim &&
56  "The vector must be the same size as the number of columns in the "
57  "matrix!");
58  #endif
59 
60  Eigen::Matrix<T, -1, 1> eigenTemp = Eigen::Matrix<T, -1, 1>::Zero(innerDim, 1);
61 
62  // iterate over the vector and multiply the corresponding row of the matrix by
63  // the vecIter value
64  for (uint32_t i = 0; i < outerDim; i++) {
65  for (typename SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator
66  matIter(*this, i);
67  matIter; ++matIter) {
68  eigenTemp(matIter.row()) += vec(matIter.col()) * matIter.value();
69  }
70  }
71  return eigenTemp;
72 }
73 
74 // Matrix Vector Multiplication (IVSparse::SparseMatrix *
75 // IVSparse::SparseMatrix::Vector)
76 template <typename T, typename indexT, bool columnMajor>
77 inline Eigen::Matrix<T, -1, 1> SparseMatrix<T, indexT, 2, columnMajor>::vectorMultiply(
78  typename SparseMatrix<T, indexT, 2, columnMajor>::Vector &vec) {
79 
80  #ifdef IVSPARSE_DEBUG
81  if (vec.length() != outerDim)
82  throw std::invalid_argument(
83  "The vector must be the same size as the number of columns in the "
84  "matrix!");
85  #endif
86 
87  Eigen::Matrix<T, -1, 1> newVector = Eigen::Matrix<T, -1, 1>::Zero(innerDim, 1);
88 
89  for (typename SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator vecIter(vec);
90  vecIter; ++vecIter) {
91  for (typename SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator
92  matIter(*this, vecIter.row());
93  matIter; ++matIter) {
94  newVector(matIter.row()) += matIter.value() * vecIter.value();
95  }
96  }
97  return newVector;
98 }
99 
100 //* BLAS Level 3 Routines *//
101 // Matrix multiplication has been moved to the VCSC_Operator.hpp file
102 
103 //* Other Matrix Calculations *//
104 
105 // Finds the Outer Sum of the Matrix
106 template <typename T, typename indexT, bool columnMajor>
108  std::vector<T> outerSum = std::vector<T>(outerDim);
109 
110  #ifdef IVSPARSE_HAS_OPENMP
111  #pragma omp parallel for
112  #endif
113  for (int i = 0; i < outerDim; i++) {
114  for (int j = 0; j < valueSizes[i]; j++) {
115  outerSum[i] += values[i][j] * counts[i][j];
116  }
117  }
118  return outerSum;
119 }
120 
121 // Finds the Inner Sum of the Matrix
122 template <typename T, typename indexT, bool columnMajor>
124  std::vector<T> innerSum = std::vector<T>(innerDim);
125 
126  #ifdef IVSPARSE_HAS_OPENMP
127  #pragma omp parallel for
128  #endif
129  for (int i = 0; i < outerDim; i++) {
131  *this, i);
132  it; ++it) {
133  innerSum[it.row()] += it.value();
134  }
135  }
136  return innerSum;
137 }
138 
139 // Finds the maximum value in each column
140 template <typename T, typename indexT, bool columnMajor>
142  std::vector<T> maxCoeff = std::vector<T>(innerDim);
143 
144  #ifdef IVSPARSE_HAS_OPENMP
145  #pragma omp parallel for
146  #endif
147  for (int i = 0; i < outerDim; i++) {
148  for (int j = 0; j < valueSizes[i]; j++) {
149  if (values[i][j] > maxCoeff[i]) {
150  maxCoeff[i] = values[i][j];
151  }
152  }
153  }
154  return maxCoeff;
155 }
156 
157 // Finds the maximum value in each row
158 template <typename T, typename indexT, bool columnMajor>
160  std::vector<T> maxCoeff = std::vector<T>(innerDim);
161 
162  #ifdef IVSPARSE_HAS_OPENMP
163  #pragma omp parallel for
164  #endif
165  for (int i = 0; i < outerDim; i++) {
167  *this, i);
168  it; ++it) {
169  if (it.value() > maxCoeff[it.row()]) {
170  maxCoeff[it.row()] = it.value();
171  }
172  }
173  }
174  return maxCoeff;
175 }
176 
177 // Finds the minimum value in each column
178 template <typename T, typename indexT, bool columnMajor>
180  std::vector<T> minCoeff = std::vector<T>(innerDim);
181 
182  #ifdef IVSPARSE_HAS_OPENMP
183  #pragma omp parallel for
184  #endif
185  for (int i = 0; i < outerDim; i++) {
186  for (int j = 0; j < valueSizes[i]; j++) {
187  if (values[i][j] < minCoeff[i]) {
188  minCoeff[i] = values[i][j];
189  }
190  }
191  }
192  return minCoeff;
193 }
194 
195 // Finds the minimum value in each row
196 template <typename T, typename indexT, bool columnMajor>
198  std::vector<T> minCoeff = std::vector<T>(innerDim);
199  memset(minCoeff.data(), 0xF, innerDim * sizeof(T));
200 
201  #ifdef IVSPARSE_HAS_OPENMP
202  #pragma omp parallel for
203  #endif
204  for (int i = 0; i < outerDim; i++) {
206  *this, i);
207  it; ++it) {
208  if (it.value() < minCoeff[it.row()]) {
209  minCoeff[it.row()] = it.value();
210  }
211  }
212  }
213  return minCoeff;
214 }
215 
216 // Calculates the trace of the matrix
217 template <typename T, typename indexT, 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 
226  #ifdef IVSPARSE_HAS_OPENMP
227  #pragma omp parallel for reduction(+ : trace)
228  #endif
229  for (int i = 0; i < outerDim; i++) {
231  *this, i);
232  it; ++it) {
233  if (it.row() == i) {
234  trace += it.value();
235  } else if (it.row() > i) {
236  continue;
237  }
238  }
239  }
240  return trace;
241 }
242 
243 // Calculates the sum of the matrix
244 template <typename T, typename indexT, bool columnMajor>
246  T sum = 0;
247  // std::vector<T> outerSum = this->outerSum();
248 
249  #ifdef IVSPARSE_HAS_OPENMP
250  #pragma omp parallel for reduction(+ : sum)
251  #endif
252  for (int i = 0; i < outerDim; i++) {
253  for (int j = 0; j < valueSizes[i]; j++) {
254  sum += values[i][j] * counts[i][j];
255  }
256  }
257  return sum;
258 }
259 
260 // Calculates the norm of the matrix
261 template <typename T, typename indexT, bool columnMajor>
263  double norm = 0;
264 
265  #ifdef IVSPARSE_HAS_OPENMP
266  #pragma omp parallel for reduction(+ : norm)
267  #endif
268  for (int i = 0; i < outerDim; i++) {
269  for (int j = 0; j < valueSizes[i]; j++) {
270  norm += values[i][j] * values[i][j] * counts[i][j];
271  }
272  }
273  return sqrt(norm);
274 }
275 
276 // Finds the length of a certain column
277 template <typename T, typename indexT, bool columnMajor>
279 
280  #ifdef IVSPARSE_DEBUG
281  assert(col < outerDim && col >= 0 && "Column index out of bounds!");
282  #endif
283 
284  double norm = 0;
285 
286  #ifdef IVSPARSE_HAS_OPENMP
287  #pragma omp parallel for reduction(+ : norm)
288  #endif
289  for (int i = 0; i < valueSizes[col]; i++) {
290  norm += values[col][i] * values[col][i] * counts[col][i];
291  }
292  return sqrt(norm);
293 }
294 
295 } // namespace IVSparse
Definition: IVCSC_Iterator.hpp:25
Definition: VCSC_SparseMatrix.hpp:21
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