tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
CompressedSparseMatrix.hpp
Go to the documentation of this file.
1#ifndef TATAMI_COMPRESSED_SPARSE_MATRIX_H
2#define TATAMI_COMPRESSED_SPARSE_MATRIX_H
3
4#include "../base/Matrix.hpp"
5#include "primary_extraction.hpp"
6#include "secondary_extraction.hpp"
7
11
12#include <vector>
13#include <algorithm>
14#include <memory>
15#include <utility>
16#include <stdexcept>
17
24namespace tatami {
25
29namespace CompressedSparseMatrix_internal {
30
31/********************
32 *** Primary full ***
33 ********************/
34
35template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
36class PrimaryMyopicFullDense : public MyopicDenseExtractor<Value_, Index_> {
37public:
38 PrimaryMyopicFullDense(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary) :
39 my_values(values),
40 my_indices(indices),
41 my_pointers(pointers),
42 my_secondary(secondary)
43 {}
44
45 const Value_* fetch(Index_ i, Value_* buffer) {
46 auto offset = my_pointers[i];
47 size_t delta = my_pointers[i+1] - my_pointers[i];
48 std::fill_n(buffer, my_secondary, static_cast<Value_>(0));
49 for (size_t x = 0; x < delta; ++x) {
50 auto cur_offset = offset + x;
51 buffer[my_indices[cur_offset]] = my_values[cur_offset];
52 }
53 return buffer;
54 }
55
56private:
57 const ValueStorage_& my_values;
58 const IndexStorage_& my_indices;
59 const PointerStorage_& my_pointers;
60 Index_ my_secondary;
61};
62
63template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
64class PrimaryMyopicFullSparse : public MyopicSparseExtractor<Value_, Index_> {
65public:
66 PrimaryMyopicFullSparse(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, const Options& opt) :
67 my_values(values),
68 my_indices(indices),
69 my_pointers(pointers),
70 my_secondary(secondary),
71 my_needs_value(opt.sparse_extract_value),
72 my_needs_index(opt.sparse_extract_index)
73 {}
74
75 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
76 auto offset = my_pointers[i];
77 auto delta = my_pointers[i+1] - my_pointers[i];
78
79 SparseRange<Value_, Index_> output(delta, NULL, NULL);
80 if (my_needs_value) {
81 output.value = sparse_utils::extract_primary_vector(my_values, offset, delta, value_buffer);
82 }
83 if (my_needs_index) {
84 output.index = sparse_utils::extract_primary_vector(my_indices, offset, delta, index_buffer);
85 }
86 return output;
87 }
88
89private:
90 const ValueStorage_& my_values;
91 const IndexStorage_& my_indices;
92 const PointerStorage_& my_pointers;
93 Index_ my_secondary;
94 bool my_needs_value, my_needs_index;
95};
96
97/*********************
98 *** Primary block ***
99 *********************/
100
101template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
102class PrimaryMyopicBlockDense : public MyopicDenseExtractor<Value_, Index_> {
103public:
104 PrimaryMyopicBlockDense(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, Index_ block_start, Index_ block_length) :
105 my_values(values),
106 my_indices(indices),
107 my_pointers(pointers),
108 my_secondary(secondary),
109 my_block_start(block_start),
110 my_block_length(block_length)
111 {}
112
113 const Value_* fetch(Index_ i, Value_* buffer) {
114 auto iStart = my_indices.begin() + my_pointers[i];
115 auto iEnd = my_indices.begin() + my_pointers[i + 1];
116 sparse_utils::refine_primary_block_limits(iStart, iEnd, my_secondary, my_block_start, my_block_length);
117 size_t offset = (iStart - my_indices.begin());
118 size_t number = iEnd - iStart;
119
120 std::fill_n(buffer, my_block_length, static_cast<Value_>(0));
121 for (size_t i = 0; i < number; ++i) {
122 auto cur_offset = offset + i;
123 buffer[my_indices[cur_offset] - my_block_start] = my_values[cur_offset];
124 }
125 return buffer;
126 }
127
128private:
129 const ValueStorage_& my_values;
130 const IndexStorage_& my_indices;
131 const PointerStorage_& my_pointers;
132 Index_ my_secondary;
133 Index_ my_block_start, my_block_length;
134};
135
136template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
137class PrimaryMyopicBlockSparse : public MyopicSparseExtractor<Value_, Index_> {
138public:
139 PrimaryMyopicBlockSparse(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, Index_ block_start, Index_ block_length, const Options& opt) :
140 my_values(values),
141 my_indices(indices),
142 my_pointers(pointers),
143 my_secondary(secondary),
144 my_block_start(block_start),
145 my_block_length(block_length),
146 my_needs_value(opt.sparse_extract_value),
147 my_needs_index(opt.sparse_extract_index)
148 {}
149
150 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
151 auto iStart = my_indices.begin() + my_pointers[i];
152 auto iEnd = my_indices.begin() + my_pointers[i + 1];
153 sparse_utils::refine_primary_block_limits(iStart, iEnd, my_secondary, my_block_start, my_block_length);
154 size_t offset = iStart - my_indices.begin();
155 size_t delta = iEnd - iStart;
156
157 SparseRange<Value_, Index_> output(delta, NULL, NULL);
158 if (my_needs_value) {
159 output.value = sparse_utils::extract_primary_vector(my_values, offset, delta, value_buffer);
160 }
161 if (my_needs_index) {
162 output.index = sparse_utils::extract_primary_vector(my_indices, offset, delta, index_buffer);
163 }
164 return output;
165 }
166
167private:
168 const ValueStorage_& my_values;
169 const IndexStorage_& my_indices;
170 const PointerStorage_& my_pointers;
171 Index_ my_secondary;
172 Index_ my_block_start, my_block_length;
173 bool my_needs_value, my_needs_index;
174};
175
176/***********************
177 *** Primary indexed ***
178 ***********************/
179
180template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
181class PrimaryMyopicIndexDense : public MyopicDenseExtractor<Value_, Index_> {
182public:
183 PrimaryMyopicIndexDense(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, const VectorPtr<Index_>& indices_ptr) :
184 my_values(values),
185 my_indices(indices),
186 my_pointers(pointers),
187 my_retriever(*indices_ptr, secondary),
188 my_num_indices(indices_ptr->size())
189 {}
190
191 const Value_* fetch(Index_ i, Value_* buffer) {
192 std::fill_n(buffer, my_num_indices, static_cast<Value_>(0));
193 auto vIt = my_values.begin() + my_pointers[i];
194 my_retriever.populate(
195 my_indices.begin() + my_pointers[i],
196 my_indices.begin() + my_pointers[i+1],
197 [&](size_t s, size_t offset) -> void {
198 buffer[s] = *(vIt + offset);
199 }
200 );
201 return buffer;
202 }
203
204private:
205 const ValueStorage_& my_values;
206 const IndexStorage_& my_indices;
207 const PointerStorage_& my_pointers;
208 sparse_utils::RetrievePrimarySubsetDense<Index_> my_retriever;
209 size_t my_num_indices;
210};
211
212template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
213class PrimaryMyopicIndexSparse : public MyopicSparseExtractor<Value_, Index_> {
214public:
215 PrimaryMyopicIndexSparse(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, const VectorPtr<Index_>& indices_ptr, const Options& opt) :
216 my_values(values),
217 my_indices(indices),
218 my_pointers(pointers),
219 my_retriever(*indices_ptr, secondary),
220 my_needs_value(opt.sparse_extract_value),
221 my_needs_index(opt.sparse_extract_index) {}
222
223 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
224 Index_ count = 0;
225 auto vcopy = value_buffer;
226 auto icopy = index_buffer;
227
228 auto vIt = my_values.begin() + my_pointers[i];
229 my_retriever.populate(
230 my_indices.begin() + my_pointers[i],
231 my_indices.begin() + my_pointers[i+1],
232 [&](size_t offset, Index_ ix) -> void {
233 ++count;
234 if (my_needs_value) {
235 *vcopy = *(vIt + offset);
236 ++vcopy;
237 }
238 if (my_needs_index) {
239 *icopy = ix;
240 ++icopy;
241 }
242 }
243 );
244
245 return SparseRange<Value_, Index_>(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
246 }
247
248private:
249 const ValueStorage_& my_values;
250 const IndexStorage_& my_indices;
251 const PointerStorage_& my_pointers;
252 sparse_utils::RetrievePrimarySubsetSparse<Index_> my_retriever;
253 bool my_needs_value, my_needs_index;
254};
255
256/**********************
257 *** Secondary full ***
258 **********************/
259
260template<typename Index_, class IndexStorage_, class PointerStorage_>
261class ServeIndices {
262public:
263 ServeIndices(const IndexStorage_& i, const PointerStorage_& p) : my_indices(i), my_pointers(p) {}
264
265private:
266 const IndexStorage_& my_indices;
267 const PointerStorage_& my_pointers;
268
269public:
270 typedef ElementType<PointerStorage_> pointer_type;
271
272 pointer_type start_offset(Index_ primary) const {
273 return my_pointers[primary];
274 }
275
276 pointer_type end_offset(Index_ primary) const {
277 return my_pointers[primary + 1];
278 }
279
280 auto raw(Index_) const {
281 return my_indices.begin();
282 }
283};
284
285template<typename Index_, class IndexStorage_, class PointerStorage_>
286auto make_ServeIndices(const IndexStorage_& i, const PointerStorage_& p) {
287 return ServeIndices<Index_, IndexStorage_, PointerStorage_>(i, p);
288}
289
290template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
291class SecondaryMyopicFullDense : public MyopicDenseExtractor<Value_, Index_> {
292public:
293 SecondaryMyopicFullDense(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary) :
294 my_values(values),
295 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, pointers.size() - 1)
296 {}
297
298 const Value_* fetch(Index_ i, Value_* buffer) {
299 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
300 my_cache.search(
301 i,
302 [&](Index_, Index_ index_primary, ElementType<PointerStorage_> ptr) -> void {
303 buffer[index_primary] = my_values[ptr];
304 }
305 );
306 return buffer;
307 }
308
309private:
310 const ValueStorage_& my_values;
311 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
312};
313
314template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
315class SecondaryMyopicFullSparse : public MyopicSparseExtractor<Value_, Index_> {
316public:
317 SecondaryMyopicFullSparse(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, const Options& opt) :
318 my_values(values),
319 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, pointers.size() - 1),
320 my_needs_value(opt.sparse_extract_value),
321 my_needs_index(opt.sparse_extract_index)
322 {}
323
324 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
325 Index_ count = 0;
326 my_cache.search(i, [&](Index_ primary, Index_, ElementType<PointerStorage_> ptr) -> void {
327 if (my_needs_value) {
328 value_buffer[count] = my_values[ptr];
329 }
330 if (my_needs_index) {
331 index_buffer[count] = primary;
332 }
333 ++count;
334 });
335 return SparseRange<Value_, Index_>(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
336 }
337
338private:
339 const ValueStorage_& my_values;
340 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
341 bool my_needs_value, my_needs_index;
342};
343
344/***********************
345 *** Secondary block ***
346 ***********************/
347
348template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
349class SecondaryMyopicBlockDense : public MyopicDenseExtractor<Value_, Index_> {
350public:
351 SecondaryMyopicBlockDense(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, Index_ block_start, Index_ block_length) :
352 my_values(values),
353 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, block_start, block_length)
354 {}
355
356 const Value_* fetch(Index_ i, Value_* buffer) {
357 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
358 my_cache.search(
359 i,
360 [&](Index_, Index_ index_primary, ElementType<PointerStorage_> ptr) -> void {
361 buffer[index_primary] = my_values[ptr];
362 }
363 );
364 return buffer;
365 }
366
367private:
368 const ValueStorage_& my_values;
369 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
370};
371
372template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
373class SecondaryMyopicBlockSparse : public MyopicSparseExtractor<Value_, Index_> {
374public:
375 SecondaryMyopicBlockSparse(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, Index_ block_start, Index_ block_length, const Options& opt) :
376 my_values(values),
377 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, block_start, block_length),
378 my_needs_value(opt.sparse_extract_value),
379 my_needs_index(opt.sparse_extract_index)
380 {}
381
382 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
383 Index_ count = 0;
384 my_cache.search(
385 i,
386 [&](Index_ primary, Index_, ElementType<PointerStorage_> ptr) -> void {
387 if (my_needs_value) {
388 value_buffer[count] = my_values[ptr];
389 }
390 if (my_needs_index) {
391 index_buffer[count] = primary;
392 }
393 ++count;
394 }
395 );
396 return SparseRange<Value_, Index_>(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
397 }
398
399private:
400 const ValueStorage_& my_values;
401 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
402 bool my_needs_value, my_needs_index;
403};
404
405/***********************
406 *** Secondary index ***
407 ***********************/
408
409template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
410class SecondaryMyopicIndexDense : public MyopicDenseExtractor<Value_, Index_> {
411public:
412 SecondaryMyopicIndexDense(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, VectorPtr<Index_> sub_ptr) :
413 my_values(values), my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, std::move(sub_ptr)) {}
414
415 const Value_* fetch(Index_ i, Value_* buffer) {
416 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
417 my_cache.search(
418 i,
419 [&](Index_, Index_ index_primary, ElementType<PointerStorage_> ptr) -> void {
420 buffer[index_primary] = my_values[ptr];
421 }
422 );
423 return buffer;
424 }
425
426private:
427 const ValueStorage_& my_values;
428 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
429};
430
431template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
432class SecondaryMyopicIndexSparse : public MyopicSparseExtractor<Value_, Index_> {
433public:
434 SecondaryMyopicIndexSparse(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, Index_ secondary, VectorPtr<Index_> sub_ptr, const Options& opt) :
435 my_values(values),
436 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, std::move(sub_ptr)),
437 my_needs_value(opt.sparse_extract_value),
438 my_needs_index(opt.sparse_extract_index)
439 {}
440
441 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
442 Index_ count = 0;
443 my_cache.search(
444 i,
445 [&](Index_ primary, Index_, ElementType<PointerStorage_> ptr) -> void {
446 if (my_needs_value) {
447 value_buffer[count] = my_values[ptr];
448 }
449 if (my_needs_index) {
450 index_buffer[count] = primary;
451 }
452 ++count;
453 }
454 );
455 return SparseRange<Value_, Index_>(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
456 }
457
458private:
459 const ValueStorage_& my_values;
460 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
461 bool my_needs_value, my_needs_index;
462};
463
464}
485template<
486 typename Value_,
487 typename Index_,
488 class ValueStorage_ = std::vector<Value_>,
489 class IndexStorage_ = std::vector<Index_>,
490 class PointerStorage_ = std::vector<size_t>
491>
492class CompressedSparseMatrix : public Matrix<Value_, Index_> {
493public:
509 CompressedSparseMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool csr, bool check = true) :
510 my_nrow(nrow), my_ncols(ncol), my_values(std::move(values)), my_indices(std::move(indices)), my_pointers(std::move(pointers)), my_csr(csr)
511 {
512 if (check) {
513 size_t nnzero = my_values.size();
514 if (nnzero != my_indices.size()) {
515 throw std::runtime_error("'my_values' and 'my_indices' should be of the same length");
516 }
517
518 size_t npointers = my_pointers.size();
519 if (my_csr) {
520 if (npointers != static_cast<size_t>(my_nrow) + 1){
521 throw std::runtime_error("length of 'pointers' should be equal to 'nrow + 1'");
522 }
523 } else {
524 if (npointers != static_cast<size_t>(my_ncols) + 1){
525 throw std::runtime_error("length of 'pointers' should be equal to 'ncols + 1'");
526 }
527 }
528
529 if (my_pointers[0] != 0) {
530 throw std::runtime_error("first element of 'pointers' should be zero");
531 }
532
533 auto last = my_pointers[npointers - 1]; // don't use back() as this is not guaranteed to be available for arbitrary PointerStorage_.
534 if (static_cast<size_t>(last) != nnzero) {
535 throw std::runtime_error("last element of 'pointers' should be equal to length of 'indices'");
536 }
537
538 ElementType<IndexStorage_> max_index = (my_csr ? my_ncols : my_nrow);
539 for (size_t i = 1; i < npointers; ++i) {
540 auto start = my_pointers[i- 1], end = my_pointers[i];
541 if (end < start || end > last) {
542 throw std::runtime_error("'pointers' should be in non-decreasing order");
543 }
544
545 for (auto x = start; x < end; ++x) {
546 if (my_indices[x] < 0 || my_indices[x] >= max_index) {
547 throw std::runtime_error("'indices' should contain non-negative integers less than the number of " + (my_csr ? std::string("columns") : std::string("rows")));
548 }
549 }
550
551 for (decltype(start) j = start + 1; j < end; ++j) {
552 if (my_indices[j] <= my_indices[j - 1]) {
553 throw std::runtime_error("'indices' should be strictly increasing within each " + (my_csr ? std::string("row") : std::string("column")));
554 }
555 }
556 }
557 }
558 }
559
560private:
561 Index_ my_nrow, my_ncols;
562 ValueStorage_ my_values;
563 IndexStorage_ my_indices;
564 PointerStorage_ my_pointers;
565 bool my_csr;
566
567public:
568 Index_ nrow() const { return my_nrow; }
569
570 Index_ ncol() const { return my_ncols; }
571
572 bool is_sparse() const { return true; }
573
574 double is_sparse_proportion() const { return 1; }
575
576 bool prefer_rows() const { return my_csr; }
577
578 double prefer_rows_proportion() const { return static_cast<double>(my_csr); }
579
580 bool uses_oracle(bool) const { return false; }
581
582 using Matrix<Value_, Index_>::dense_row;
583
584 using Matrix<Value_, Index_>::dense_column;
585
586 using Matrix<Value_, Index_>::sparse_row;
587
588 using Matrix<Value_, Index_>::sparse_column;
589
590private:
591 Index_ secondary() const {
592 if (my_csr) {
593 return my_ncols;
594 } else {
595 return my_nrow;
596 }
597 }
598
599 /*****************************
600 ******* Dense myopic ********
601 *****************************/
602public:
603 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, const Options&) const {
604 if (my_csr == row) {
605 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicFullDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
606 my_values, my_indices, my_pointers, secondary()
607 );
608 } else {
609 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicFullDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
610 my_values, my_indices, my_pointers, secondary()
611 );
612 }
613 }
614
615 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, Index_ block_start, Index_ block_end, const Options&) const {
616 if (my_csr == row) {
617 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicBlockDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
618 my_values, my_indices, my_pointers, secondary(), block_start, block_end
619 );
620 } else {
621 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicBlockDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
622 my_values, my_indices, my_pointers, secondary(), block_start, block_end
623 );
624 }
625 }
626
627 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, VectorPtr<Index_> indices_ptr, const Options&) const {
628 if (my_csr == row) {
629 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicIndexDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
630 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr)
631 );
632 } else {
633 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicIndexDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
634 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr)
635 );
636 }
637 }
638
639 /******************************
640 ******* Sparse myopic ********
641 ******************************/
642public:
643 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, const Options& opt) const {
644 if (my_csr == row) {
645 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicFullSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
646 my_values, my_indices, my_pointers, secondary(), opt
647 );
648 } else {
649 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicFullSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
650 my_values, my_indices, my_pointers, secondary(), opt
651 );
652 }
653 }
654
655 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, Index_ block_start, Index_ block_end, const Options& opt) const {
656 if (my_csr == row) {
657 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicBlockSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
658 my_values, my_indices, my_pointers, secondary(), block_start, block_end, opt
659 );
660 } else {
661 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicBlockSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
662 my_values, my_indices, my_pointers, secondary(), block_start, block_end, opt
663 );
664 }
665 }
666
667 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, VectorPtr<Index_> indices_ptr, const Options& opt) const {
668 if (my_csr == row) {
669 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicIndexSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
670 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr), opt
671 );
672 } else {
673 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicIndexSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
674 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr), opt
675 );
676 }
677 }
678
679 /*******************************
680 ******* Dense oracular ********
681 *******************************/
682public:
683 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
684 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, opt));
685 }
686
687 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, Index_ block_start, Index_ block_end, const Options& opt) const {
688 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, block_start, block_end, opt));
689 }
690
691 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> my_indices_ptr, const Options& opt) const {
692 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, std::move(my_indices_ptr), opt));
693 }
694
695 /********************************
696 ******* Sparse oracular ********
697 ********************************/
698public:
699 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
700 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, opt));
701 }
702
703 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, Index_ block_start, Index_ block_end, const Options& opt) const {
704 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, block_start, block_end, opt));
705 }
706
707 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> my_indices_ptr, const Options& opt) const {
708 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, std::move(my_indices_ptr), opt));
709 }
710};
711
717template<typename Value_, typename Index_, class ValueStorage_ = std::vector<Value_>, class IndexStorage_ = std::vector<Index_>, class PointerStorage_ = std::vector<size_t> >
718class CompressedSparseColumnMatrix : public CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> {
719public:
728 CompressedSparseColumnMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check = true) :
729 CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_>(nrow, ncol, std::move(values), std::move(indices), std::move(pointers), false, check) {}
730};
731
737template<typename Value_, typename Index_, class ValueStorage_ = std::vector<Value_>, class IndexStorage_ = std::vector<Index_>, class PointerStorage_ = std::vector<size_t> >
738class CompressedSparseRowMatrix : public CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> {
739public:
748 CompressedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check = true) :
749 CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_>(nrow, ncol, std::move(values), std::move(indices), std::move(pointers), true, check) {}
750};
751
752}
753
754#endif
Get type of elements in an array.
Virtual class for a matrix of some numeric type.
Mimic the oracle-aware extractor interface.
Compressed sparse column matrix.
Definition CompressedSparseMatrix.hpp:718
CompressedSparseColumnMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check=true)
Definition CompressedSparseMatrix.hpp:728
Compressed sparse matrix representation.
Definition CompressedSparseMatrix.hpp:492
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition CompressedSparseMatrix.hpp:699
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition CompressedSparseMatrix.hpp:643
Index_ nrow() const
Definition CompressedSparseMatrix.hpp:568
bool prefer_rows() const
Definition CompressedSparseMatrix.hpp:576
double prefer_rows_proportion() const
Definition CompressedSparseMatrix.hpp:578
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_end, const Options &opt) const
Definition CompressedSparseMatrix.hpp:655
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > my_indices_ptr, const Options &opt) const
Definition CompressedSparseMatrix.hpp:707
bool uses_oracle(bool) const
Definition CompressedSparseMatrix.hpp:580
CompressedSparseMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool csr, bool check=true)
Definition CompressedSparseMatrix.hpp:509
double is_sparse_proportion() const
Definition CompressedSparseMatrix.hpp:574
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_end, const Options &) const
Definition CompressedSparseMatrix.hpp:615
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition CompressedSparseMatrix.hpp:683
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_end, const Options &opt) const
Definition CompressedSparseMatrix.hpp:703
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_end, const Options &opt) const
Definition CompressedSparseMatrix.hpp:687
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > my_indices_ptr, const Options &opt) const
Definition CompressedSparseMatrix.hpp:691
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition CompressedSparseMatrix.hpp:627
bool is_sparse() const
Definition CompressedSparseMatrix.hpp:572
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition CompressedSparseMatrix.hpp:667
Index_ ncol() const
Definition CompressedSparseMatrix.hpp:570
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &) const
Definition CompressedSparseMatrix.hpp:603
Compressed sparse row matrix.
Definition CompressedSparseMatrix.hpp:738
CompressedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check=true)
Definition CompressedSparseMatrix.hpp:748
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:21
Compile-time checks for the data() method.
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
typename std::remove_cv< typename std::remove_reference< decltype(std::declval< Array_ >()[0])>::type >::type ElementType
Definition ElementType.hpp:17
Options for accessing data from a Matrix instance.
Definition Options.hpp:30