tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
DenseMatrix.hpp
Go to the documentation of this file.
1#ifndef TATAMI_DENSE_MATRIX_H
2#define TATAMI_DENSE_MATRIX_H
3
4#include "../base/Matrix.hpp"
6#include "../utils/has_data.hpp"
7#include "../utils/copy.hpp"
10
11#include <vector>
12#include <algorithm>
13#include <stdexcept>
14#include <utility>
15
16#include "sanisizer/sanisizer.hpp"
17
26namespace tatami {
27
31namespace DenseMatrix_internals {
32
33template<typename Value_, typename Index_, class Storage_>
34class PrimaryMyopicFullDense final : public MyopicDenseExtractor<Value_, Index_> {
35public:
36 PrimaryMyopicFullDense(const Storage_& storage, const Index_ secondary) : my_storage(storage), my_secondary(secondary) {}
37
38 const Value_* fetch(const Index_ i, Value_* const buffer) {
39 const auto offset = sanisizer::product_unsafe<I<decltype(my_storage.size())> >(my_secondary, i);
40#ifndef TATAMI_DEBUG_FORCE_COPY
41 if constexpr(has_data<Value_, Storage_>::value) {
42 return my_storage.data() + offset;
43 } else {
44#endif
45 std::copy_n(my_storage.begin() + offset, my_secondary, buffer);
46 return buffer;
47#ifndef TATAMI_DEBUG_FORCE_COPY
48 }
49#endif
50 }
51
52private:
53 const Storage_& my_storage;
54 Index_ my_secondary;
55};
56
57template<typename Value_, typename Index_, class Storage_>
58class PrimaryMyopicBlockDense final : public MyopicDenseExtractor<Value_, Index_> {
59public:
60 PrimaryMyopicBlockDense(const Storage_& storage, const Index_ secondary, const Index_ block_start, const Index_ block_length) :
61 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
62
63 const Value_* fetch(const Index_ i, Value_* const buffer) {
64 const auto offset = sanisizer::nd_offset<I<decltype(my_storage.size())> >(my_block_start, my_secondary, i);
65#ifndef TATAMI_DEBUG_FORCE_COPY
66 if constexpr(has_data<Value_, Storage_>::value) {
67 return my_storage.data() + offset;
68 } else {
69#endif
70 std::copy_n(my_storage.begin() + offset, my_block_length, buffer);
71 return buffer;
72#ifndef TATAMI_DEBUG_FORCE_COPY
73 }
74#endif
75 }
76
77private:
78 const Storage_& my_storage;
79 Index_ my_secondary;
80 Index_ my_block_start, my_block_length;
81};
82
83template<typename Value_, typename Index_, class Storage_>
84class PrimaryMyopicIndexDense final : public MyopicDenseExtractor<Value_, Index_> {
85public:
86 PrimaryMyopicIndexDense(const Storage_& storage, const Index_ secondary, VectorPtr<Index_> indices_ptr) :
87 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
88
89 const Value_* fetch(const Index_ i, Value_* const buffer) {
90 const auto& indices = *my_indices_ptr;
91 const auto nindices = indices.size();
92 for (I<decltype(nindices)> x = 0; x < nindices; ++x) {
93 buffer[x] = my_storage[sanisizer::nd_offset<I<decltype(my_storage.size())> >(indices[x], my_secondary, i)];
94 }
95 return buffer;
96 }
97
98private:
99 const Storage_& my_storage;
100 Index_ my_secondary;
101 VectorPtr<Index_> my_indices_ptr;
102};
103
104template<typename Value_, typename Index_, class Storage_>
105class SecondaryMyopicFullDense final : public MyopicDenseExtractor<Value_, Index_> {
106public:
107 SecondaryMyopicFullDense(const Storage_& storage, const Index_ secondary, const Index_ primary) :
108 my_storage(storage), my_secondary(secondary), my_primary(primary) {}
109
110 const Value_* fetch(const Index_ i, Value_* const buffer) {
111 for (I<decltype(my_primary)> x = 0; x < my_primary; ++x) {
112 buffer[x] = my_storage[sanisizer::nd_offset<I<decltype(my_storage.size())> >(i, my_secondary, x)];
113 }
114 return buffer;
115 }
116
117private:
118 const Storage_& my_storage;
119 Index_ my_secondary;
120 Index_ my_primary;
121};
122
123template<typename Value_, typename Index_, class Storage_>
124class SecondaryMyopicBlockDense final : public MyopicDenseExtractor<Value_, Index_> {
125public:
126 SecondaryMyopicBlockDense(const Storage_& storage, const Index_ secondary, const Index_ block_start, const Index_ block_length) :
127 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
128
129 const Value_* fetch(const Index_ i, Value_* const buffer) {
130 for (I<decltype(my_block_length)> x = 0; x < my_block_length; ++x) {
131 buffer[x] = my_storage[sanisizer::nd_offset<I<decltype(my_storage.size())> >(i, my_secondary, my_block_start + x)];
132 }
133 return buffer;
134 }
135
136private:
137 const Storage_& my_storage;
138 Index_ my_secondary;
139 Index_ my_block_start;
140 Index_ my_block_length;
141};
142
143template<typename Value_, typename Index_, class Storage_>
144class SecondaryMyopicIndexDense final : public MyopicDenseExtractor<Value_, Index_> {
145public:
146 SecondaryMyopicIndexDense(const Storage_& storage, const Index_ secondary, VectorPtr<Index_> indices_ptr) :
147 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
148
149 const Value_* fetch(const Index_ i, Value_* const buffer) {
150 const auto& indices = *my_indices_ptr;
151 const auto nindices = indices.size();
152 for (I<decltype(nindices)> x = 0; x < nindices; ++x) {
153 buffer[x] = my_storage[sanisizer::nd_offset<I<decltype(my_storage.size())> >(i, my_secondary, indices[x])];
154 }
155 return buffer;
156 }
157
158private:
159 const Storage_& my_storage;
160 Index_ my_secondary;
161 VectorPtr<Index_> my_indices_ptr;
162};
163
164}
179template<typename Value_, typename Index_, class Storage_>
180class DenseMatrix : public Matrix<Value_, Index_> {
181public:
189 DenseMatrix(const Index_ nrow, const Index_ ncol, Storage_ values, const bool row_major) :
190 my_nrow(nrow), my_ncol(ncol), my_values(std::move(values)), my_row_major(row_major)
191 {
192 const auto nvalues = my_values.size();
193 if (my_nrow == 0) {
194 if (nvalues != 0) {
195 throw std::runtime_error("length of 'values' should be equal to product of 'nrows' and 'ncols'");
196 }
197 } else if (!sanisizer::is_equal(nvalues / my_nrow, my_ncol) || nvalues % my_nrow != 0) { // using division instead of 'my_nrow * my_ncol == nvalues' as the product might overflow.
198 throw std::runtime_error("length of 'values' should be equal to product of 'nrows' and 'ncols'");
199 }
200 }
201
202private:
203 Index_ my_nrow, my_ncol;
204 Storage_ my_values;
205 bool my_row_major;
206
207public:
211 Index_ nrow() const { return my_nrow; }
212
216 Index_ ncol() const { return my_ncol; }
217
221 bool prefer_rows() const { return my_row_major; }
222
226 bool uses_oracle(const bool) const { return false; }
227
231 bool is_sparse() const { return false; }
232
236 double is_sparse_proportion() const { return 0; }
237
241 double prefer_rows_proportion() const { return static_cast<double>(my_row_major); }
242
243 using Matrix<Value_, Index_>::dense;
244
245 using Matrix<Value_, Index_>::sparse;
246
247private:
248 Index_ primary() const {
249 if (my_row_major) {
250 return my_nrow;
251 } else {
252 return my_ncol;
253 }
254 }
255
256 Index_ secondary() const {
257 if (my_row_major) {
258 return my_ncol;
259 } else {
260 return my_nrow;
261 }
262 }
263
264 /*****************************
265 ******* Dense myopic ********
266 *****************************/
267public:
271 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(const bool row, const Options&) const {
272 if (my_row_major == row) {
273 return std::make_unique<DenseMatrix_internals::PrimaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary());
274 } else {
275 return std::make_unique<DenseMatrix_internals::SecondaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary(), primary());
276 }
277 }
278
282 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options&) const {
283 if (my_row_major == row) {
284 return std::make_unique<DenseMatrix_internals::PrimaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
285 } else {
286 return std::make_unique<DenseMatrix_internals::SecondaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
287 }
288 }
289
293 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(const bool row, VectorPtr<Index_> indices_ptr, const Options&) const {
294 if (my_row_major == row) {
295 return std::make_unique<DenseMatrix_internals::PrimaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
296 } else {
297 return std::make_unique<DenseMatrix_internals::SecondaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
298 }
299 }
300
301 /******************************
302 ******* Sparse myopic ********
303 ******************************/
304public:
308 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(const bool row, const Options& opt) const {
309 return std::make_unique<FullSparsifiedWrapper<false, Value_, Index_> >(dense(row, opt), (row ? my_ncol : my_nrow), opt);
310 }
311
315 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options& opt) const {
316 return std::make_unique<BlockSparsifiedWrapper<false, Value_, Index_> >(dense(row, block_start, block_length, opt), block_start, block_length, opt);
317 }
318
322 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(const bool row, VectorPtr<Index_> indices_ptr, const Options& opt) const {
323 auto ptr = dense(row, indices_ptr, opt);
324 return std::make_unique<IndexSparsifiedWrapper<false, Value_, Index_> >(std::move(ptr), std::move(indices_ptr), opt);
325 }
326
327 /*******************************
328 ******* Dense oracular ********
329 *******************************/
330public:
334 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(const bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
335 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, opt));
336 }
337
341 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
342 const bool row,
343 std::shared_ptr<const Oracle<Index_> > oracle,
344 const Index_ block_start,
345 const Index_ block_length,
346 const Options& opt)
347 const {
348 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, block_start, block_length, opt));
349 }
350
354 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(const bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
355 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, std::move(indices_ptr), opt));
356 }
357
358 /********************************
359 ******* Sparse oracular ********
360 ********************************/
361public:
365 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(const bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
366 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, opt));
367 }
368
372 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
373 const bool row,
374 std::shared_ptr<const Oracle<Index_> > oracle,
375 const Index_ block_start,
376 const Index_ block_length,
377 const Options& opt)
378 const {
379 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, block_start, block_length, opt));
380 }
381
385 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(const bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
386 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, std::move(indices_ptr), opt));
387 }
388};
389
395template<typename Value_, typename Index_, class Storage_ = std::vector<Value_> >
396class DenseColumnMatrix final : public DenseMatrix<Value_, Index_, Storage_> {
397public:
403 DenseColumnMatrix(const Index_ nrow, const Index_ ncol, Storage_ values) : DenseMatrix<Value_, Index_, Storage_>(nrow, ncol, std::move(values), false) {}
404};
405
411template<typename Value_, typename Index_, class Storage_ = std::vector<Value_> >
412class DenseRowMatrix final : public DenseMatrix<Value_, Index_, Storage_> {
413public:
419 DenseRowMatrix(const Index_ nrow, const Index_ ncol, Storage_ values) : DenseMatrix<Value_, Index_, Storage_>(nrow, ncol, std::move(values), true) {}
420};
421
422}
423
424#endif
Convert index type to container size.
Virtual class for a matrix of some numeric type.
Mimic the oracle-aware extractor interface.
Wrapper classes for sparse extraction from a dense tatami::Matrix.
Dense column-major matrix.
Definition DenseMatrix.hpp:396
DenseColumnMatrix(const Index_ nrow, const Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:403
Dense matrix representation.
Definition DenseMatrix.hpp:180
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:385
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:315
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:354
double prefer_rows_proportion() const
Definition DenseMatrix.hpp:241
bool prefer_rows() const
Definition DenseMatrix.hpp:221
bool is_sparse() const
Definition DenseMatrix.hpp:231
DenseMatrix(const Index_ nrow, const Index_ ncol, Storage_ values, const bool row_major)
Definition DenseMatrix.hpp:189
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:365
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DenseMatrix.hpp:308
bool uses_oracle(const bool) const
Definition DenseMatrix.hpp:226
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition DenseMatrix.hpp:293
double is_sparse_proportion() const
Definition DenseMatrix.hpp:236
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &) const
Definition DenseMatrix.hpp:271
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:341
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:322
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:372
Index_ nrow() const
Definition DenseMatrix.hpp:211
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &) const
Definition DenseMatrix.hpp:282
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:334
Index_ ncol() const
Definition DenseMatrix.hpp:216
Dense row-major matrix.
Definition DenseMatrix.hpp:412
DenseRowMatrix(const Index_ nrow, const Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:419
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
Copy data from one buffer to another.
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
Options for accessing data from a Matrix instance.
Definition Options.hpp:30