16 template <
typename T,
typename indexT,
bool columnMajor>
18 if (vals !=
nullptr) {
21 if (innerIdx !=
nullptr) {
27 template <
typename T,
typename indexT,
bool columnMajor>
33 assert((vec >= 0 && vec < mat.
outerSize()) &&
"Vector index out of bounds");
47 nnz = mat.outerPtr[vec + 1] - mat.outerPtr[vec];
50 vals = (T *)malloc(nnz *
sizeof(T));
51 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
52 }
catch (std::bad_alloc &e) {
53 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
56 memcpy(vals, mat.vals + mat.outerPtr[vec], nnz *
sizeof(T));
57 memcpy(innerIdx, mat.innerIdx + mat.outerPtr[vec], nnz *
sizeof(indexT));
67 template <
typename T,
typename indexT,
bool columnMajor>
76 vals = (T *)malloc(nnz *
sizeof(T));
77 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
78 }
catch (std::bad_alloc &e) {
79 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
82 memcpy(vals, vec.vals, nnz *
sizeof(T));
83 memcpy(innerIdx, vec.innerIdx, nnz *
sizeof(indexT));
93 template <
typename T,
typename indexT,
bool columnMajor>
95 assert(std::is_floating_point<indexT>::value ==
false &&
96 "The index type must be a non-floating point type");
97 assert((std::is_arithmetic<T>::value && std::is_arithmetic<indexT>::value) &&
98 "The value and index types must be numeric types");
99 assert((std::is_same<indexT, bool>::value ==
false) &&
100 "The index type must not be bool");
104 template <
typename T,
typename indexT,
bool columnMajor>
109 size +=
sizeof(T) * nnz;
110 size +=
sizeof(indexT) * nnz;
116 template <
typename T,
typename indexT,
bool columnMajor>
122 template <
typename T,
typename indexT,
bool columnMajor>
128 template <
typename T,
typename indexT,
bool columnMajor>
134 template <
typename T,
typename indexT,
bool columnMajor>
140 template <
typename T,
typename indexT,
bool columnMajor>
143 #ifdef IVSPARSE_DEBUG
144 assert((index >= 0 && index < length) &&
"Index out of bounds");
147 return (*
this)[index];
151 template <
typename T,
typename indexT,
bool columnMajor>
157 template <
typename T,
typename indexT,
bool columnMajor>
163 template <
typename T,
typename indexT,
bool columnMajor>
172 template <
typename T,
typename indexT,
bool columnMajor>
174 std::cout <<
"Vector: " << std::endl;
175 std::cout << std::endl;
178 for (uint32_t i = 0; i < std::min(length, (uint32_t)100); i++) {
179 std::cout << (*this)[i] <<
" ";
182 std::cout << std::endl;
188 template <
typename T,
typename indexT,
bool columnMajor>
198 if (vals !=
nullptr) {
201 if (innerIdx !=
nullptr) {
217 vals = (T *)malloc(nnz *
sizeof(T));
218 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
219 }
catch (std::bad_alloc &e) {
220 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
223 memcpy(vals, vec.vals, nnz *
sizeof(T));
224 memcpy(innerIdx, vec.innerIdx, nnz *
sizeof(indexT));
226 #ifdef IVSPARSE_DEBUG
234 template <
typename T,
typename indexT,
bool columnMajor>
235 bool SparseMatrix<T, indexT, 1, columnMajor>::Vector::operator==(
236 typename SparseMatrix<T, indexT, 1, columnMajor>::Vector &vec) {
239 if (length != vec.length || nnz != vec.nnz) {
244 for (uint32_t i = 0; i < nnz; i++) {
245 if (vals[i] != vec.vals[i] || innerIdx[i] != vec.innerIdx[i]) {
255 template <
typename T,
typename indexT,
bool columnMajor>
256 bool SparseMatrix<T, indexT, 1, columnMajor>::Vector::operator!=(
257 typename SparseMatrix<T, indexT, 1, columnMajor>::Vector &vec) {
259 return !(*
this == vec);
263 template <
typename T,
typename indexT,
bool columnMajor>
264 T SparseMatrix<T, indexT, 1, columnMajor>::Vector::operator[](uint32_t index) {
265 #ifdef IVSPARSE_DEBUG
266 assert((index >= 0 && index < length) &&
"Index out of bounds");
269 for (uint32_t i = 0; i < nnz; i++) {
270 if (innerIdx[i] == index) {
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
size_t byteSize()
Definition: IVCSC_Vector_Methods.hpp:183
Vector()
Definition: IVCSC_Vector.hpp:58
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
Definition: CSC_SparseMatrix.hpp:24
uint32_t innerSize() const
Definition: IVSparse_Base_Methods.hpp:33
size_t byteSize() const
Definition: IVSparse_Base_Methods.hpp:42
uint32_t outerSize() const
Definition: IVSparse_Base_Methods.hpp:36
Definition: IVCSC_SparseMatrix.hpp:29