1#ifndef TATAMI_DENSE_MATRIX_H
2#define TATAMI_DENSE_MATRIX_H
8#include "../utils/Index_to_container.hpp"
16#include "sanisizer/sanisizer.hpp"
31namespace DenseMatrix_internals {
33template<
typename Value_,
typename Index_,
class Storage_>
34class PrimaryMyopicFullDense final :
public MyopicDenseExtractor<Value_, Index_> {
36 PrimaryMyopicFullDense(
const Storage_& storage,
const Index_ secondary) : my_storage(storage), my_secondary(secondary) {}
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 if constexpr(has_data<Value_, Storage_>::value) {
41 return my_storage.data() + offset;
43 std::copy_n(my_storage.begin() + offset, my_secondary, buffer);
49 const Storage_& my_storage;
53template<
typename Value_,
typename Index_,
class Storage_>
54class PrimaryMyopicBlockDense final :
public MyopicDenseExtractor<Value_, Index_> {
56 PrimaryMyopicBlockDense(
const Storage_& storage,
const Index_ secondary,
const Index_ block_start,
const Index_ block_length) :
57 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
59 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
60 const auto offset = sanisizer::nd_offset<I<
decltype(my_storage.size())> >(my_block_start, my_secondary, i);
61 if constexpr(has_data<Value_, Storage_>::value) {
62 return my_storage.data() + offset;
64 std::copy_n(my_storage.begin() + offset, my_block_length, buffer);
70 const Storage_& my_storage;
72 Index_ my_block_start, my_block_length;
75template<
typename Value_,
typename Index_,
class Storage_>
76class PrimaryMyopicIndexDense final :
public MyopicDenseExtractor<Value_, Index_> {
78 PrimaryMyopicIndexDense(
const Storage_& storage,
const Index_ secondary, VectorPtr<Index_> indices_ptr) :
79 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
81 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
82 const auto& indices = *my_indices_ptr;
83 const auto nindices = indices.size();
84 for (I<
decltype(nindices)> x = 0; x < nindices; ++x) {
85 buffer[x] = my_storage[sanisizer::nd_offset<I<
decltype(my_storage.size())> >(indices[x], my_secondary, i)];
91 const Storage_& my_storage;
93 VectorPtr<Index_> my_indices_ptr;
96template<
typename Value_,
typename Index_,
class Storage_>
97class SecondaryMyopicFullDense final :
public MyopicDenseExtractor<Value_, Index_> {
99 SecondaryMyopicFullDense(
const Storage_& storage,
const Index_ secondary,
const Index_ primary) :
100 my_storage(storage), my_secondary(secondary), my_primary(primary) {}
102 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
103 for (I<
decltype(my_primary)> x = 0; x < my_primary; ++x) {
104 buffer[x] = my_storage[sanisizer::nd_offset<I<
decltype(my_storage.size())> >(i, my_secondary, x)];
110 const Storage_& my_storage;
115template<
typename Value_,
typename Index_,
class Storage_>
116class SecondaryMyopicBlockDense final :
public MyopicDenseExtractor<Value_, Index_> {
118 SecondaryMyopicBlockDense(
const Storage_& storage,
const Index_ secondary,
const Index_ block_start,
const Index_ block_length) :
119 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
121 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
122 for (I<
decltype(my_block_length)> x = 0; x < my_block_length; ++x) {
123 buffer[x] = my_storage[sanisizer::nd_offset<I<
decltype(my_storage.size())> >(i, my_secondary, my_block_start + x)];
129 const Storage_& my_storage;
131 Index_ my_block_start;
132 Index_ my_block_length;
135template<
typename Value_,
typename Index_,
class Storage_>
136class SecondaryMyopicIndexDense final :
public MyopicDenseExtractor<Value_, Index_> {
138 SecondaryMyopicIndexDense(
const Storage_& storage,
const Index_ secondary, VectorPtr<Index_> indices_ptr) :
139 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
141 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
142 const auto& indices = *my_indices_ptr;
143 const auto nindices = indices.size();
144 for (I<
decltype(nindices)> x = 0; x < nindices; ++x) {
145 buffer[x] = my_storage[sanisizer::nd_offset<I<
decltype(my_storage.size())> >(i, my_secondary, indices[x])];
151 const Storage_& my_storage;
153 VectorPtr<Index_> my_indices_ptr;
171template<
typename Value_,
typename Index_,
class Storage_>
182 my_nrow(
nrow), my_ncol(
ncol), my_values(std::move(values)), my_row_major(row_major)
184 const auto nvalues = my_values.size();
187 throw std::runtime_error(
"length of 'values' should be equal to product of 'nrows' and 'ncols'");
189 }
else if (!safe_non_negative_equal(nvalues / my_nrow, my_ncol) || nvalues % my_nrow != 0) {
190 throw std::runtime_error(
"length of 'values' should be equal to product of 'nrows' and 'ncols'");
195 Index_ my_nrow, my_ncol;
200 Index_
nrow()
const {
return my_nrow; }
202 Index_
ncol()
const {
return my_ncol; }
219 Index_ primary()
const {
227 Index_ secondary()
const {
239 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
const bool row,
const Options&)
const {
240 if (my_row_major == row) {
241 return std::make_unique<DenseMatrix_internals::PrimaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary());
243 return std::make_unique<DenseMatrix_internals::SecondaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary(), primary());
247 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
const bool row,
const Index_ block_start,
const Index_ block_length,
const Options&)
const {
248 if (my_row_major == row) {
249 return std::make_unique<DenseMatrix_internals::PrimaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
251 return std::make_unique<DenseMatrix_internals::SecondaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
256 if (my_row_major == row) {
257 return std::make_unique<DenseMatrix_internals::PrimaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
259 return std::make_unique<DenseMatrix_internals::SecondaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
267 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
const bool row,
const Options& opt)
const {
268 return std::make_unique<FullSparsifiedWrapper<false, Value_, Index_> >(
dense(row, opt), (row ? my_ncol : my_nrow), opt);
271 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
const bool row,
const Index_ block_start,
const Index_ block_length,
const Options& opt)
const {
272 return std::make_unique<BlockSparsifiedWrapper<false, Value_, Index_> >(
dense(row, block_start, block_length, opt), block_start, block_length, opt);
276 auto ptr =
dense(row, indices_ptr, opt);
277 return std::make_unique<IndexSparsifiedWrapper<false, Value_, Index_> >(std::move(ptr), std::move(indices_ptr), opt);
284 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
const bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
285 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, opt));
288 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
291 const Index_ block_start,
292 const Index_ block_end,
295 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, block_start, block_end, opt));
299 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, std::move(indices_ptr), opt));
306 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
const bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
307 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, opt));
310 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
313 const Index_ block_start,
314 const Index_ block_end,
317 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, block_start, block_end, opt));
321 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, std::move(indices_ptr), opt));
330template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
346template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
Virtual class for a matrix of some numeric type.
Wrapper classes for sparse extraction from a dense tatami::Matrix.
Dense column-major matrix.
Definition DenseMatrix.hpp:331
DenseColumnMatrix(const Index_ nrow, const Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:338
Dense matrix representation.
Definition DenseMatrix.hpp:172
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:320
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:271
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:298
double prefer_rows_proportion() const
Definition DenseMatrix.hpp:212
bool prefer_rows() const
Definition DenseMatrix.hpp:204
bool is_sparse() const
Definition DenseMatrix.hpp:208
DenseMatrix(const Index_ nrow, const Index_ ncol, Storage_ values, const bool row_major)
Definition DenseMatrix.hpp:181
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:306
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DenseMatrix.hpp:267
bool uses_oracle(const bool) const
Definition DenseMatrix.hpp:206
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition DenseMatrix.hpp:255
double is_sparse_proportion() const
Definition DenseMatrix.hpp:210
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &) const
Definition DenseMatrix.hpp:239
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:275
Index_ nrow() const
Definition DenseMatrix.hpp:200
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &) const
Definition DenseMatrix.hpp:247
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_end, const Options &opt) const
Definition DenseMatrix.hpp:310
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:284
Index_ ncol() const
Definition DenseMatrix.hpp:202
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_end, const Options &opt) const
Definition DenseMatrix.hpp:288
Dense row-major matrix.
Definition DenseMatrix.hpp:347
DenseRowMatrix(const Index_ nrow, const Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:354
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.
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
Options for accessing data from a Matrix instance.
Definition Options.hpp:30