16 template <
typename T,
typename indexT,
bool columnMajor>
18 if (values !=
nullptr) {
21 if (counts !=
nullptr) {
24 if (indices !=
nullptr) {
30 template <
typename T,
typename indexT,
bool columnMajor>
36 assert((vec >= 0 && vec < mat.
outerSize()) &&
"Vector index out of bounds");
63 values = (T *)malloc(valuesSize *
sizeof(T));
64 counts = (indexT *)malloc(valuesSize *
sizeof(indexT));
65 indices = (indexT *)malloc(indexSize *
sizeof(indexT));
66 }
catch (std::bad_alloc &e) {
67 std::cerr << e.what() <<
'\n';
71 memcpy(values, mat.
getValues(vec), valuesSize *
sizeof(T));
72 memcpy(counts, mat.
getCounts(vec), valuesSize *
sizeof(indexT));
73 memcpy(indices, mat.
getIndices(vec), indexSize *
sizeof(indexT));
79 template <
typename T,
typename indexT,
bool columnMajor>
87 indexWidth = vec.indexWidth;
96 valuesSize = vec.valuesSize;
97 indexSize = vec.indexSize;
101 values = (T *)malloc(valuesSize *
sizeof(T));
102 counts = (indexT *)malloc(valuesSize *
sizeof(indexT));
103 indices = (indexT *)malloc(indexSize *
sizeof(indexT));
104 }
catch (std::bad_alloc &e) {
105 std::cerr << e.what() <<
'\n';
109 memcpy(values, vec.values, valuesSize *
sizeof(T));
110 memcpy(counts, vec.counts, valuesSize *
sizeof(indexT));
111 memcpy(indices, vec.indices, indexSize *
sizeof(indexT));
114 #ifdef IVSPARSE_DEBUG
122 template <
typename T,
typename indexT,
bool columnMajor>
124 assert(std::is_floating_point<indexT>::value ==
false &&
125 "The index type must be a non-floating point type");
126 assert((std::is_arithmetic<T>::value && std::is_arithmetic<indexT>::value) &&
127 "The value and index types must be numeric types");
128 assert((std::is_same<indexT, bool>::value ==
false) &&
129 "The index type must not be bool");
133 template <
typename T,
typename indexT,
bool columnMajor>
138 size +=
sizeof(T) * valuesSize;
139 size +=
sizeof(indexT) * valuesSize;
140 size +=
sizeof(indexT) * indexSize;
146 template <
typename T,
typename indexT,
bool columnMajor>
152 template <
typename T,
typename indexT,
bool columnMajor>
158 template <
typename T,
typename indexT,
bool columnMajor>
164 template <
typename T,
typename indexT,
bool columnMajor>
170 template <
typename T,
typename indexT,
bool columnMajor>
173 #ifdef IVSPARSE_DEBUG
175 assert(index < length && index >= 0 &&
"The index is out of bounds");
178 return (*
this)[index];
182 template <
typename T,
typename indexT,
bool columnMajor>
188 template <
typename T,
typename indexT,
bool columnMajor>
194 template <
typename T,
typename indexT,
bool columnMajor>
200 template <
typename T,
typename indexT,
bool columnMajor>
206 template <
typename T,
typename indexT,
bool columnMajor>
214 template <
typename T,
typename indexT,
bool columnMajor>
218 std::cout <<
"Vector is too large to print" << std::endl;
222 std::cout <<
"Vector: ";
223 std::cout << std::endl;
226 for (uint32_t i = 0; i < length; i++) {
227 std::cout << (*this)[i] <<
" ";
230 std::cout << std::endl;
236 template <
typename T,
typename indexT,
bool columnMajor>
239 #pragma omp parallel for schedule(dynamic)
240 for (
int i = 0; i < valueSizes; i++) {
241 norm += values[i] * values[i] * counts[i];
247 template <
typename T,
typename indexT,
bool columnMajor>
250 #pragma omp parallel for schedule(dynamic)
251 for (
int i = 0; i < valueSizes; i++) {
252 sum += values[i] * counts[i];
258 template <
typename T,
typename indexT,
bool columnMajor>
266 dot += it.value() * other.coeff(it.row());
273 template <
typename T,
typename indexT,
bool columnMajor>
280 dot += it.value() * other.coeff(it.row());
288 template <
typename T,
typename indexT,
bool columnMajor>
293 if (
this == &other) {
298 if (values !=
nullptr) {
301 if (counts !=
nullptr) {
304 if (indices !=
nullptr) {
309 length = other.length;
312 indexWidth = other.indexWidth;
321 valuesSize = other.valuesSize;
322 indexSize = other.indexSize;
326 values = (T *)malloc(valuesSize *
sizeof(T));
327 counts = (indexT *)malloc(valuesSize *
sizeof(indexT));
328 indices = (indexT *)malloc(indexSize *
sizeof(indexT));
329 }
catch (std::bad_alloc &e) {
330 std::cerr << e.what() <<
'\n';
334 memcpy(values, other.values, valuesSize *
sizeof(T));
335 memcpy(counts, other.counts, valuesSize *
sizeof(indexT));
336 memcpy(indices, other.indices, indexSize *
sizeof(indexT));
339 #ifdef IVSPARSE_DEBUG
348 template <
typename T,
typename indexT,
bool columnMajor>
349 bool SparseMatrix<T, indexT, 2, columnMajor>::Vector::operator==(
350 typename SparseMatrix<T, indexT, 2, columnMajor>::Vector &other) {
353 if (length != other.length) {
358 if (nnz != other.nnz) {
363 for (uint32_t i = 0; i < valuesSize; i++) {
364 if (values[i] != other.values[i]) {
370 for (uint32_t i = 0; i < valuesSize; i++) {
371 if (counts[i] != other.counts[i]) {
377 for (uint32_t i = 0; i < indexSize; i++) {
378 if (indices[i] != other.indices[i]) {
388 template <
typename T,
typename indexT,
bool columnMajor>
389 bool SparseMatrix<T, indexT, 2, columnMajor>::Vector::operator!=(
390 typename SparseMatrix<T, indexT, 2, columnMajor>::Vector &other) {
391 return !(*
this == other);
395 template <
typename T,
typename indexT,
bool columnMajor>
396 T SparseMatrix<T, indexT, 2, columnMajor>::Vector::operator[](uint32_t index) {
398 #ifdef IVSPARSE_DEBUG
400 assert(index < length &&
"The index is out of bounds");
408 if (it.getIndex() == (indexT)index) {
419 template <
typename T,
typename indexT,
bool columnMajor>
420 void SparseMatrix<T, indexT, 2, columnMajor>::Vector::operator*=(T scalar) {
421 #pragma omp parallel for
422 for (
int i = 0; i < valueSizes; i++) {
428 template <
typename T,
typename indexT,
bool columnMajor>
430 SparseMatrix<T, indexT, 2, columnMajor>::Vector::operator*(T scalar) {
434 #pragma omp parallel for
435 for (
int i = 0; i < outerDim; i++) {
436 for (
int j = 0; j < valueSizes; j++) {
437 newVector.values[i][j] *= scalar;
Definition: IVCSC_Iterator.hpp:25
Definition: IVCSC_Vector.hpp:25
uint32_t getLength()
Definition: IVCSC_Vector_Methods.hpp:200
T coeff(uint32_t index)
Definition: IVCSC_Vector_Methods.hpp:189
void print()
Definition: IVCSC_Vector_Methods.hpp:208
uint32_t innerSize()
Definition: IVCSC_Vector_Methods.hpp:153
T sum()
Definition: IVCSC_Vector_Methods.hpp:241
size_t byteSize()
Definition: IVCSC_Vector_Methods.hpp:183
Vector()
Definition: IVCSC_Vector.hpp:58
double norm()
Definition: IVCSC_Vector_Methods.hpp:230
uint32_t nonZeros()
Definition: IVCSC_Vector_Methods.hpp:165
uint32_t outerSize()
Definition: IVCSC_Vector_Methods.hpp:159
~Vector()
Definition: IVCSC_Vector_Methods.hpp:17
double dot(Eigen::Vector< T, -1 > &other)
Definition: IVCSC_Vector_Methods.hpp:252
Definition: VCSC_SparseMatrix.hpp:21
indexT getNumUniqueVals(uint32_t vec) const
Definition: VCSC_Methods.hpp:48
indexT * getCounts(uint32_t vec) const
Definition: VCSC_Methods.hpp:35
indexT getNumIndices(uint32_t vec) const
Definition: VCSC_Methods.hpp:58
indexT * getIndices(uint32_t vec) const
Definition: VCSC_Methods.hpp:41
T * getValues(uint32_t vec) const
Definition: VCSC_Methods.hpp:29
uint32_t innerSize() const
Definition: IVSparse_Base_Methods.hpp:33
uint32_t outerSize() const
Definition: IVSparse_Base_Methods.hpp:36
Definition: IVCSC_SparseMatrix.hpp:29