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"
9
10#include <vector>
11#include <algorithm>
12#include <stdexcept>
13#include <utility>
14
23namespace tatami {
24
28namespace DenseMatrix_internals {
29
30template<typename Value_, typename Index_, class Storage_>
31class PrimaryMyopicFullDense final : public MyopicDenseExtractor<Value_, Index_> {
32public:
33 typedef decltype(std::declval<Storage_>().size()) Size;
34
35 PrimaryMyopicFullDense(const Storage_& storage, Size secondary) : my_storage(storage), my_secondary(secondary) {}
36
37 const Value_* fetch(Index_ i, Value_* buffer) {
38 Size offset = static_cast<Size>(i) * my_secondary; // cast to the container's size to avoid overflow.
39 if constexpr(has_data<Value_, Storage_>::value) {
40 return my_storage.data() + offset;
41 } else {
42 std::copy_n(my_storage.begin() + offset, my_secondary, buffer);
43 return buffer;
44 }
45 }
46
47private:
48 const Storage_& my_storage;
49 Size my_secondary;
50};
51
52template<typename Value_, typename Index_, class Storage_>
53class PrimaryMyopicBlockDense final : public MyopicDenseExtractor<Value_, Index_> {
54public:
55 typedef decltype(std::declval<Storage_>().size()) Size;
56
57 PrimaryMyopicBlockDense(const Storage_& storage, Size secondary, Index_ block_start, Index_ block_length) :
58 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
59
60 const Value_* fetch(Index_ i, Value_* buffer) {
61 Size offset = static_cast<Size>(i) * my_secondary + my_block_start; // cast to container size to avoid overflow.
62 if constexpr(has_data<Value_, Storage_>::value) {
63 return my_storage.data() + offset;
64 } else {
65 std::copy_n(my_storage.begin() + offset, my_block_length, buffer);
66 return buffer;
67 }
68 }
69
70private:
71 const Storage_& my_storage;
72 Size my_secondary;
73 Size my_block_start, my_block_length;
74};
75
76template<typename Value_, typename Index_, class Storage_>
77class PrimaryMyopicIndexDense final : public MyopicDenseExtractor<Value_, Index_> {
78public:
79 typedef decltype(std::declval<Storage_>().size()) Size;
80
81 PrimaryMyopicIndexDense(const Storage_& storage, Size secondary, VectorPtr<Index_> indices_ptr) :
82 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
83
84 const Value_* fetch(Index_ i, Value_* buffer) {
85 Size offset = static_cast<Size>(i) * my_secondary; // cast to container size to avoid overflow.
86 const auto& indices = *my_indices_ptr;
87 for (decltype(indices.size()) x = 0, end = indices.size(); x < end; ++x) {
88 buffer[x] = my_storage[offset + static_cast<Size>(indices[x])]; // more casting for overflow protection.
89 }
90 return buffer;
91 }
92
93private:
94 const Storage_& my_storage;
95 Size my_secondary;
96 VectorPtr<Index_> my_indices_ptr;
97};
98
99template<typename Value_, typename Index_, class Storage_>
100class SecondaryMyopicFullDense final : public MyopicDenseExtractor<Value_, Index_> {
101public:
102 typedef decltype(std::declval<Storage_>().size()) Size;
103
104 SecondaryMyopicFullDense(const Storage_& storage, Index_ secondary, Index_ primary) :
105 my_storage(storage), my_secondary(secondary), my_primary(primary) {}
106
107 const Value_* fetch(Index_ i, Value_* buffer) {
108 for (decltype(my_primary) x = 0; x < my_primary; ++x) {
109 buffer[x] = my_storage[x * my_secondary + static_cast<Size>(i)]; // cast to container size to avoid overflow.
110 }
111 return buffer;
112 }
113
114private:
115 const Storage_& my_storage;
116 Size my_secondary;
117 Size my_primary;
118};
119
120template<typename Value_, typename Index_, class Storage_>
121class SecondaryMyopicBlockDense final : public MyopicDenseExtractor<Value_, Index_> {
122public:
123 typedef decltype(std::declval<Storage_>().size()) Size;
124
125 SecondaryMyopicBlockDense(const Storage_& storage, Index_ secondary, Index_ block_start, Index_ block_length) :
126 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
127
128 const Value_* fetch(Index_ i, Value_* buffer) {
129 Size offset = my_block_start * my_secondary + static_cast<Size>(i); // cast to avoid overflow.
130 for (decltype(my_block_length) x = 0; x < my_block_length; ++x) {
131 buffer[x] = my_storage[x * my_secondary + offset]; // everything is already Size to avoid overflow.
132 }
133 return buffer;
134 }
135
136private:
137 const Storage_& my_storage;
138 Size my_secondary;
139 Size my_block_start;
140 Size my_block_length;
141};
142
143template<typename Value_, typename Index_, class Storage_>
144class SecondaryMyopicIndexDense final : public MyopicDenseExtractor<Value_, Index_> {
145public:
146 typedef decltype(std::declval<Storage_>().size()) Size;
147
148 SecondaryMyopicIndexDense(const Storage_& storage, Index_ secondary, VectorPtr<Index_> indices_ptr) :
149 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
150
151 const Value_* fetch(Index_ i, Value_* buffer) {
152 Size offset = i;
153 const auto& indices = *my_indices_ptr;
154 for (decltype(indices.size()) x = 0, end = indices.size(); x < end; ++x) {
155 buffer[x] = my_storage[static_cast<Size>(indices[x]) * my_secondary + offset]; // casting to avoid overflow.
156 }
157 return buffer;
158 }
159
160private:
161 const Storage_& my_storage;
162 Size my_secondary;
163 VectorPtr<Index_> my_indices_ptr;
164};
165
166}
181template<typename Value_, typename Index_, class Storage_>
182class DenseMatrix : public Matrix<Value_, Index_> {
183public:
191 DenseMatrix(Index_ nrow, Index_ ncol, Storage_ values, bool row_major) : my_nrow(nrow), my_ncol(ncol), my_values(std::move(values)), my_row_major(row_major) {
192 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 (!safe_non_negative_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:
208 Index_ nrow() const { return my_nrow; }
209
210 Index_ ncol() const { return my_ncol; }
211
212 bool prefer_rows() const { return my_row_major; }
213
214 bool uses_oracle(bool) const { return false; }
215
216 bool is_sparse() const { return false; }
217
218 double is_sparse_proportion() const { return 0; }
219
220 double prefer_rows_proportion() const { return static_cast<double>(my_row_major); }
221
222 using Matrix<Value_, Index_>::dense;
223
224 using Matrix<Value_, Index_>::sparse;
225
226private:
227 Index_ primary() const {
228 if (my_row_major) {
229 return my_nrow;
230 } else {
231 return my_ncol;
232 }
233 }
234
235 Index_ secondary() const {
236 if (my_row_major) {
237 return my_ncol;
238 } else {
239 return my_nrow;
240 }
241 }
242
243 /*****************************
244 ******* Dense myopic ********
245 *****************************/
246public:
247 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, const Options&) const {
248 if (my_row_major == row) {
249 return std::make_unique<DenseMatrix_internals::PrimaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary());
250 } else {
251 return std::make_unique<DenseMatrix_internals::SecondaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary(), primary());
252 }
253 }
254
255 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, Index_ block_start, Index_ block_length, const Options&) const {
256 if (my_row_major == row) {
257 return std::make_unique<DenseMatrix_internals::PrimaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
258 } else {
259 return std::make_unique<DenseMatrix_internals::SecondaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
260 }
261 }
262
263 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, VectorPtr<Index_> indices_ptr, const Options&) const {
264 if (my_row_major == row) {
265 return std::make_unique<DenseMatrix_internals::PrimaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
266 } else {
267 return std::make_unique<DenseMatrix_internals::SecondaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
268 }
269 }
270
271 /******************************
272 ******* Sparse myopic ********
273 ******************************/
274public:
275 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, const Options& opt) const {
276 return std::make_unique<FullSparsifiedWrapper<false, Value_, Index_> >(dense(row, opt), (row ? my_ncol : my_nrow), opt);
277 }
278
279 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, Index_ block_start, Index_ block_length, const Options& opt) const {
280 return std::make_unique<BlockSparsifiedWrapper<false, Value_, Index_> >(dense(row, block_start, block_length, opt), block_start, block_length, opt);
281 }
282
283 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, VectorPtr<Index_> indices_ptr, const Options& opt) const {
284 auto ptr = dense(row, indices_ptr, opt);
285 return std::make_unique<IndexSparsifiedWrapper<false, Value_, Index_> >(std::move(ptr), std::move(indices_ptr), opt);
286 }
287
288 /*******************************
289 ******* Dense oracular ********
290 *******************************/
291public:
292 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
293 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, opt));
294 }
295
296 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 {
297 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, block_start, block_end, opt));
298 }
299
300 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
301 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, std::move(indices_ptr), opt));
302 }
303
304 /********************************
305 ******* Sparse oracular ********
306 ********************************/
307public:
308 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
309 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, opt));
310 }
311
312 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 {
313 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, block_start, block_end, opt));
314 }
315
316 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
317 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, std::move(indices_ptr), opt));
318 }
319};
320
326template<typename Value_, typename Index_, class Storage_ = std::vector<Value_> >
327class DenseColumnMatrix final : public DenseMatrix<Value_, Index_, Storage_> {
328public:
334 DenseColumnMatrix(Index_ nrow, Index_ ncol, Storage_ values) : DenseMatrix<Value_, Index_, Storage_>(nrow, ncol, std::move(values), false) {}
335};
336
342template<typename Value_, typename Index_, class Storage_ = std::vector<Value_> >
343class DenseRowMatrix final : public DenseMatrix<Value_, Index_, Storage_> {
344public:
350 DenseRowMatrix(Index_ nrow, Index_ ncol, Storage_ values) : DenseMatrix<Value_, Index_, Storage_>(nrow, ncol, std::move(values), true) {}
351};
352
353}
354
355#endif
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:327
DenseColumnMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:334
Dense matrix representation.
Definition DenseMatrix.hpp:182
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &) const
Definition DenseMatrix.hpp:247
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 DenseMatrix.hpp:312
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DenseMatrix.hpp:275
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:292
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition DenseMatrix.hpp:263
DenseMatrix(Index_ nrow, Index_ ncol, Storage_ values, bool row_major)
Definition DenseMatrix.hpp:191
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:300
double prefer_rows_proportion() const
Definition DenseMatrix.hpp:220
bool prefer_rows() const
Definition DenseMatrix.hpp:212
bool is_sparse() const
Definition DenseMatrix.hpp:216
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:316
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:283
bool uses_oracle(bool) const
Definition DenseMatrix.hpp:214
double is_sparse_proportion() const
Definition DenseMatrix.hpp:218
Index_ nrow() const
Definition DenseMatrix.hpp:208
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &) const
Definition DenseMatrix.hpp:255
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:279
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 DenseMatrix.hpp:296
Index_ ncol() const
Definition DenseMatrix.hpp:210
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:308
Dense row-major matrix.
Definition DenseMatrix.hpp:343
DenseRowMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:350
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:23
Compile-time checks for the data() method.
Sign-aware integer comparisons.
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
bool safe_non_negative_equal(Left_ l, Right_ r)
Definition integer_comparisons.hpp:23
Options for accessing data from a Matrix instance.
Definition Options.hpp:30