tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
ConstantMatrix.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CONSTANT_MATRIX_HPP
2#define TATAMI_CONSTANT_MATRIX_HPP
3
4#include "../base/Matrix.hpp"
7
8#include <algorithm>
9
15namespace tatami {
16
20namespace ConstantMatrix_internal {
21
22template<bool oracle_, typename Value_, typename Index_>
23class DenseFiller final : public DenseExtractor<oracle_, Value_, Index_> {
24public:
25 DenseFiller(const Index_ length, const Value_ value) : my_length(length), my_value(value) {}
26
27 const Value_* fetch(const Index_, Value_* const buffer) {
28 std::fill_n(buffer, my_length, my_value);
29 return buffer;
30 }
31private:
32 Index_ my_length;
33 Value_ my_value;
34};
35
36template<bool oracle_, typename Value_, typename Index_>
37class SparseFiller final : public SparseExtractor<oracle_, Value_, Index_> {
38public:
39 SparseFiller(const Options& opt) : my_needs_value(opt.sparse_extract_value), my_needs_index(opt.sparse_extract_index) {}
40
41 SparseRange<Value_, Index_> fetch(const Index_, Value_* const value_buffer, Index_* const index_buffer) {
42 return SparseRange<Value_, Index_>(0, (my_needs_value ? value_buffer : NULL), (my_needs_index ? index_buffer : NULL));
43 }
44private:
45 bool my_needs_value;
46 bool my_needs_index;
47};
48
49}
63template<typename Value_, typename Index_>
64class ConstantMatrix final : public Matrix<Value_, Index_> {
65public:
71 ConstantMatrix(const Index_ nrow, const Index_ ncol, const Value_ value) : my_nrow(nrow), my_ncol(ncol), my_value(value) {}
72
73private:
74 Index_ my_nrow, my_ncol;
75 Value_ my_value;
76
77public:
78 Index_ nrow() const {
79 return my_nrow;
80 }
81
82 Index_ ncol() const {
83 return my_ncol;
84 }
85
86 bool is_sparse() const {
87 return my_value == 0;
88 }
89
90 double is_sparse_proportion() const {
91 return static_cast<double>(my_value == 0);
92 }
93
94 bool prefer_rows() const {
95 return true; // pretty much arbitrary here.
96 }
97
98 double prefer_rows_proportion() const {
99 return 1; // pretty much arbitrary here.
100 }
101
102 bool uses_oracle(const bool) const {
103 return false;
104 }
105
106 /********************
107 *** Myopic dense ***
108 ********************/
109private:
110 template<bool oracle_>
111 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(
112 const bool row,
114 const Options&
115 ) const {
116 return std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(row ? my_ncol : my_nrow, my_value);
117 }
118
119 template<bool oracle_>
120 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(
121 const bool,
123 [[maybe_unused]] const Index_ block_start,
124 const Index_ block_length,
125 const Options&
126 ) const {
127 return std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(block_length, my_value);
128 }
129
130 template<bool oracle_>
131 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(
132 const bool,
134 VectorPtr<Index_> indices_ptr,
135 const Options&
136 ) const {
137 return std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(indices_ptr->size(), my_value);
138 }
139
140public:
141 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
142 const bool row,
143 const Options& opt
144 ) const {
145 return dense_internal<false>(row, false, opt);
146 }
147
148 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
149 const bool row,
150 const Index_ block_start,
151 const Index_ block_length,
152 const Options& opt
153 ) const {
154 return dense_internal<false>(row, false, block_start, block_length, opt);
155 }
156
157 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
158 const bool row,
159 VectorPtr<Index_> indices_ptr,
160 const Options& opt
161 ) const {
162 return dense_internal<false>(row, false, std::move(indices_ptr), opt);
163 }
164
165 /**********************
166 *** Oracular dense ***
167 **********************/
168public:
169 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
170 const bool row,
171 std::shared_ptr<const Oracle<Index_> > oracle,
172 const Options& opt
173 ) const {
174 return dense_internal<true>(row, std::move(oracle), opt);
175 }
176
177 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
178 const bool row,
179 std::shared_ptr<const Oracle<Index_> > oracle,
180 const Index_ block_start,
181 const Index_ block_length,
182 const Options& opt
183 ) const {
184 return dense_internal<true>(row, std::move(oracle), block_start, block_length, opt);
185 }
186
187 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
188 const bool row,
189 std::shared_ptr<const Oracle<Index_> > oracle,
190 VectorPtr<Index_> indices_ptr,
191 const Options& opt
192 ) const {
193 return dense_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
194 }
195
196 /*********************
197 *** Myopic sparse ***
198 *********************/
199private:
200 template<bool oracle_>
201 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(
202 const bool row,
204 const Options& opt
205 ) const {
206 if (my_value == 0) {
207 return std::make_unique<ConstantMatrix_internal::SparseFiller<oracle_, Value_, Index_> >(opt);
208 } else {
209 return std::make_unique<FullSparsifiedWrapper<oracle_, Value_, Index_> >(
210 dense_internal<oracle_>(row, std::move(oracle), opt),
211 (row ? my_ncol : my_nrow),
212 opt
213 );
214 }
215 }
216
217 template<bool oracle_>
218 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(
219 const bool row,
221 const Index_ block_start,
222 const Index_ block_length,
223 const Options& opt
224 ) const {
225 if (my_value == 0) {
226 return std::make_unique<ConstantMatrix_internal::SparseFiller<oracle_, Value_, Index_> >(opt);
227 } else {
228 return std::make_unique<BlockSparsifiedWrapper<oracle_, Value_, Index_> >(
229 dense_internal<oracle_>(row, std::move(oracle), block_start, block_length, opt),
230 block_start,
231 block_length,
232 opt
233 );
234 }
235 }
236
237 template<bool oracle_>
238 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(
239 const bool,
241 VectorPtr<Index_> indices_ptr,
242 const Options& opt
243 ) const {
244 if (my_value == 0) {
245 return std::make_unique<ConstantMatrix_internal::SparseFiller<oracle_, Value_, Index_> >(opt);
246 } else {
247 return std::make_unique<IndexSparsifiedWrapper<oracle_, Value_, Index_> >(
248 std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(indices_ptr->size(), my_value),
249 std::move(indices_ptr),
250 opt
251 );
252 }
253 }
254
255public:
256 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
257 const bool row,
258 const Options& opt
259 ) const {
260 return sparse_internal<false>(row, false, opt);
261 }
262
263 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
264 const bool row,
265 const Index_ block_start,
266 const Index_ block_length,
267 const Options& opt
268 ) const {
269 return sparse_internal<false>(row, false, block_start, block_length, opt);
270 }
271
272 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
273 const bool row,
274 VectorPtr<Index_> indices_ptr,
275 const Options& opt
276 ) const {
277 return sparse_internal<false>(row, false, std::move(indices_ptr), opt);
278 }
279
280 /**********************
281 *** Oracular sparse ***
282 **********************/
283public:
284 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
285 const bool row,
286 std::shared_ptr<const Oracle<Index_> > oracle,
287 const Options& opt
288 ) const {
289 return sparse_internal<true>(row, std::move(oracle), opt);
290 }
291
292 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
293 const bool row,
294 std::shared_ptr<const Oracle<Index_> > oracle,
295 const Index_ block_start,
296 const Index_ block_length,
297 const Options& opt
298 ) const {
299 return sparse_internal<true>(row, std::move(oracle), block_start, block_length, opt);
300 }
301
302 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
303 const bool row,
304 std::shared_ptr<const Oracle<Index_> > oracle,
305 VectorPtr<Index_> indices_ptr,
306 const Options& opt
307 ) const {
308 return sparse_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
309 }
310};
311
312}
313
314#endif
Virtual class for a matrix of some numeric type.
Wrapper classes for sparse extraction from a dense tatami::Matrix.
Matrix containing a constant value.
Definition ConstantMatrix.hpp:64
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition ConstantMatrix.hpp:256
bool is_sparse() const
Definition ConstantMatrix.hpp:86
ConstantMatrix(const Index_ nrow, const Index_ ncol, const Value_ value)
Definition ConstantMatrix.hpp:71
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition ConstantMatrix.hpp:272
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition ConstantMatrix.hpp:263
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition ConstantMatrix.hpp:169
double is_sparse_proportion() const
Definition ConstantMatrix.hpp:90
Index_ nrow() const
Definition ConstantMatrix.hpp:78
double prefer_rows_proportion() const
Definition ConstantMatrix.hpp:98
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 ConstantMatrix.hpp:292
Index_ ncol() const
Definition ConstantMatrix.hpp:82
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition ConstantMatrix.hpp:284
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition ConstantMatrix.hpp:141
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 ConstantMatrix.hpp:302
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition ConstantMatrix.hpp:157
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 ConstantMatrix.hpp:177
bool uses_oracle(const bool) const
Definition ConstantMatrix.hpp:102
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition ConstantMatrix.hpp:148
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 ConstantMatrix.hpp:187
bool prefer_rows() const
Definition ConstantMatrix.hpp:94
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
typename std::conditional< oracle_, std::shared_ptr< const Oracle< Index_ > >, bool >::type MaybeOracle
Definition new_extractor.hpp:20
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:273
Templated construction of a new extractor.
Options for accessing data from a Matrix instance.
Definition Options.hpp:30