1#ifndef TATAMI_ISOMETRIC_UNARY_SUBSTITUTE_HELPERS_H
2#define TATAMI_ISOMETRIC_UNARY_SUBSTITUTE_HELPERS_H
20template<CompareOperation op_,
typename InputValue_,
typename Scalar_,
typename OutputValue_>
21bool delayed_substitute_is_sparse(Scalar_ compared, OutputValue_ substitute) {
22 return !delayed_compare<op_, InputValue_>(0, compared) || substitute == 0;
25template<CompareOperation op_,
typename InputValue_,
typename Scalar_,
typename OutputValue_>
26OutputValue_ delayed_substitute_run(InputValue_ val, Scalar_ compared, OutputValue_ substitute) {
27 if (delayed_compare<op_>(val, compared)) {
34template<CompareOperation op_,
typename InputValue_,
typename Index_,
typename Scalar_,
typename OutputValue_>
35void delayed_substitute_run_simple(
const InputValue_* input, Index_ length, Scalar_ compared, OutputValue_* output, OutputValue_ substitute) {
36 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
39 for (Index_ i = 0; i < length; ++i) {
40 output[i] = delayed_substitute_run<op_>(input[i], compared, substitute);
59template<CompareOperation op_,
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Scalar_>
68 my_sparse = delayed_substitute_is_sparse<op_, InputValue_>(my_compared, my_substitute);
73 OutputValue_ my_substitute;
77 std::optional<Index_>
nrow()
const {
77 std::optional<Index_>
nrow()
const {
…}
81 std::optional<Index_>
ncol()
const {
81 std::optional<Index_>
ncol()
const {
…}
103 void dense(
bool, Index_, Index_, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
104 delayed_substitute_run_simple<op_>(input, length, my_compared, output, my_substitute);
103 void dense(
bool, Index_, Index_, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
…}
107 void dense(
bool, Index_,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
108 delayed_substitute_run_simple<op_>(input,
static_cast<Index_
>(indices.size()), my_compared, output, my_substitute);
107 void dense(
bool, Index_,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
…}
116 void sparse(
bool, Index_, Index_ number,
const InputValue_* input_value,
const Index_*, OutputValue_* output_value)
const {
117 delayed_substitute_run_simple<op_>(input_value, number, my_compared, output_value, my_substitute);
116 void sparse(
bool, Index_, Index_ number,
const InputValue_* input_value,
const Index_*, OutputValue_* output_value)
const {
…}
120 OutputValue_
fill(
bool, Index_)
const {
124 return my_substitute;
120 OutputValue_
fill(
bool, Index_)
const {
…}
137template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Scalar_>
148template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Scalar_>
159template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Scalar_>
170template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Scalar_>
181template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Scalar_>
192template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Scalar_>
203template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Scalar_>
210template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Scalar_>
211std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteEqualScalar(Scalar_ compared, OutputValue_ substitute) {
212 return std::make_shared<DelayedUnaryIsometricSubstituteEqualScalarHelper<OutputValue_, InputValue_, Index_, Scalar_> >(compared, substitute);
215template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Scalar_>
216std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteGreaterThanScalar(Scalar_ compared, OutputValue_ substitute) {
217 return std::make_shared<DelayedUnaryIsometricSubstituteGreaterThanScalarHelper<OutputValue_, InputValue_, Index_, Scalar_> >(compared, substitute);
220template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Scalar_>
221std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteLessThanScalar(Scalar_ compared, OutputValue_ substitute) {
222 return std::make_shared<DelayedUnaryIsometricSubstituteLessThanScalarHelper<OutputValue_, InputValue_, Index_, Scalar_> >(compared, substitute);
225template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Scalar_>
226std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteGreaterThanOrEqualScalar(Scalar_ compared, OutputValue_ substitute) {
227 return std::make_shared<DelayedUnaryIsometricSubstituteGreaterThanOrEqualScalarHelper<OutputValue_, InputValue_, Index_, Scalar_> >(compared, substitute);
230template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Scalar_>
231std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteLessThanOrEqualScalar(Scalar_ compared, OutputValue_ substitute) {
232 return std::make_shared<DelayedUnaryIsometricSubstituteLessThanOrEqualScalarHelper<OutputValue_, InputValue_, Index_, Scalar_> >(compared, substitute);
235template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Scalar_>
236std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteNotEqualScalar(Scalar_ compared, OutputValue_ substitute) {
237 return std::make_shared<DelayedUnaryIsometricSubstituteNotEqualScalarHelper<OutputValue_, InputValue_, Index_, Scalar_> >(compared, substitute);
259template<CompareOperation op_,
typename OutputValue_,
typename InputValue_,
typename Index_,
typename ComparedVector_,
typename SubstituteVector_>
273 my_compared(std::move(compared)),
274 my_substitute(std::move(substitute)),
277 if (
static_cast<size_t>(my_compared.size()) !=
static_cast<size_t>(my_substitute.size())) {
278 throw std::runtime_error(
"'compared' and 'substitute' should have the same length");
280 for (
size_t i = 0, end = my_compared.size(); i < end; ++i) {
281 if (!delayed_substitute_is_sparse<op_, InputValue_>(my_compared[i], my_substitute[i])) {
289 ComparedVector_ my_compared;
290 SubstituteVector_ my_substitute;
292 bool my_sparse =
true;
295 std::optional<Index_>
nrow()
const {
297 return my_compared.size();
295 std::optional<Index_>
nrow()
const {
…}
303 std::optional<Index_>
ncol()
const {
307 return my_compared.size();
303 std::optional<Index_>
ncol()
const {
…}
329 void dense(
bool row, Index_ idx, Index_ start, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
330 if (row == my_by_row) {
331 delayed_substitute_run_simple<op_>(input, length, my_compared[idx], output, my_substitute[idx]);
333 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
336 for (Index_ i = 0; i < length; ++i) {
337 Index_ is = i + start;
338 output[i] = delayed_substitute_run<op_>(input[i], my_compared[is], my_substitute[is]);
329 void dense(
bool row, Index_ idx, Index_ start, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
…}
343 void dense(
bool row, Index_ idx,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
344 if (row == my_by_row) {
345 delayed_substitute_run_simple<op_>(input,
static_cast<Index_
>(indices.size()), my_compared[idx], output, my_substitute[idx]);
347 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
350 Index_ length = indices.size();
351 for (Index_ i = 0; i < length; ++i) {
352 auto ii = indices[i];
353 output[i] = delayed_substitute_run<op_>(input[i], my_compared[ii], my_substitute[ii]);
343 void dense(
bool row, Index_ idx,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
…}
363 void sparse(
bool row, Index_ idx, Index_ number,
const InputValue_* input_value,
const Index_* indices, OutputValue_* output_value)
const {
364 if (row == my_by_row) {
365 delayed_substitute_run_simple<op_>(input_value, number, my_compared[idx], output_value, my_substitute[idx]);
367 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
368 input_value = output_value;
370 for (Index_ i = 0; i < number; ++i) {
371 auto ii = indices[i];
372 output_value[i] = delayed_substitute_run<op_>(input_value[i], my_compared[ii], my_substitute[ii]);
363 void sparse(
bool row, Index_ idx, Index_ number,
const InputValue_* input_value,
const Index_* indices, OutputValue_* output_value)
const {
…}
377 OutputValue_
fill(
bool row, Index_ idx)
const {
378 if (row == my_by_row) {
379 auto sub = my_substitute[idx];
380 if (!delayed_compare<op_, InputValue_>(0, my_compared[idx])) {
377 OutputValue_
fill(
bool row, Index_ idx)
const {
…}
402template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename ComparedVector_,
typename SubstituteVector_>
414template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename ComparedVector_,
typename SubstituteVector_>
426template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename ComparedVector_,
typename SubstituteVector_>
438template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename ComparedVector_,
typename SubstituteVector_>
450template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename ComparedVector_,
typename SubstituteVector_>
462template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename ComparedVector_,
typename SubstituteVector_>
474template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename ComparedVector_,
typename SubstituteVector_>
481template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename ComparedVector_,
typename SubstituteVector_>
482std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteEqualVector(ComparedVector_ compared, SubstituteVector_ substitute,
bool by_row) {
483 return std::make_shared<DelayedUnaryIsometricSubstituteEqualVectorHelper<OutputValue_, InputValue_, Index_, ComparedVector_, SubstituteVector_> >(std::move(compared), std::move(substitute), by_row);
486template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename ComparedVector_,
typename SubstituteVector_>
487std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteGreaterThanVector(ComparedVector_ compared, SubstituteVector_ substitute,
bool by_row) {
488 return std::make_shared<DelayedUnaryIsometricSubstituteGreaterThanVectorHelper<OutputValue_, InputValue_, Index_, ComparedVector_, SubstituteVector_> >(std::move(compared), std::move(substitute), by_row);
491template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename ComparedVector_,
typename SubstituteVector_>
492std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteLessThanVector(ComparedVector_ compared, SubstituteVector_ substitute,
bool by_row) {
493 return std::make_shared<DelayedUnaryIsometricSubstituteLessThanVectorHelper<OutputValue_, InputValue_, Index_, ComparedVector_, SubstituteVector_> >(std::move(compared), std::move(substitute), by_row);
496template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename ComparedVector_,
typename SubstituteVector_>
497std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteGreaterThanOrEqualVector(ComparedVector_ compared, SubstituteVector_ substitute,
bool by_row) {
498 return std::make_shared<DelayedUnaryIsometricSubstituteGreaterThanOrEqualVectorHelper<OutputValue_, InputValue_, Index_, ComparedVector_, SubstituteVector_> >(std::move(compared), std::move(substitute), by_row);
501template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename ComparedVector_,
typename SubstituteVector_>
502std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricLessSubstituteThanOrEqualVector(ComparedVector_ compared, SubstituteVector_ substitute,
bool by_row) {
503 return std::make_shared<DelayedUnaryIsometricSubstituteLessThanOrEqualVectorHelper<OutputValue_, InputValue_, Index_, ComparedVector_, SubstituteVector_> >(std::move(compared), std::move(substitute), by_row);
506template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename ComparedVector_,
typename SubstituteVector_>
507std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteNotEqualVector(ComparedVector_ compared, SubstituteVector_ substitute,
bool by_row) {
508 return std::make_shared<DelayedUnaryIsometricSubstituteNotEqualVectorHelper<OutputValue_, InputValue_, Index_, ComparedVector_, SubstituteVector_> >(std::move(compared), std::move(substitute), by_row);
517template<SpecialCompareOperation op_,
bool pass_,
typename InputValue_,
typename OutputValue_>
518bool delayed_special_substitute_is_sparse(OutputValue_ substitute) {
519 return !delayed_special_compare<op_, pass_, InputValue_>(0) || substitute == 0;
522template<SpecialCompareOperation op_,
bool pass_,
typename InputValue_,
typename OutputValue_>
523OutputValue_ delayed_special_substitute_run(InputValue_ val, OutputValue_ substitute) {
524 if (delayed_special_compare<op_, pass_>(val)) {
531template<SpecialCompareOperation op_,
bool pass_,
typename InputValue_,
typename Index_,
typename OutputValue_>
532void delayed_special_substitute_run_simple(
const InputValue_* input, Index_ length, OutputValue_ substitute, OutputValue_* output) {
533 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
536 for (Index_ i = 0; i < length; ++i) {
537 output[i] = delayed_special_substitute_run<op_, pass_>(input[i], substitute);
557template<SpecialCompareOperation op_,
bool pass_,
typename OutputValue_,
typename InputValue_,
typename Index_>
564 my_sparse = delayed_special_substitute_is_sparse<op_, pass_, InputValue_>(my_substitute);
568 OutputValue_ my_substitute;
572 std::optional<Index_>
nrow()
const {
572 std::optional<Index_>
nrow()
const {
…}
576 std::optional<Index_>
ncol()
const {
576 std::optional<Index_>
ncol()
const {
…}
598 void dense(
bool, Index_, Index_, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
599 delayed_special_substitute_run_simple<op_, pass_>(input, length, my_substitute, output);
598 void dense(
bool, Index_, Index_, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
…}
602 void dense(
bool, Index_,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
603 delayed_special_substitute_run_simple<op_, pass_>(input,
static_cast<Index_
>(indices.size()), my_substitute, output);
602 void dense(
bool, Index_,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
…}
611 void sparse(
bool, Index_, Index_ number,
const InputValue_* input_value,
const Index_*, OutputValue_* output_value)
const {
612 delayed_special_substitute_run_simple<op_, pass_>(input_value, number, my_substitute, output_value);
611 void sparse(
bool, Index_, Index_ number,
const InputValue_* input_value,
const Index_*, OutputValue_* output_value)
const {
…}
615 OutputValue_
fill(
bool, Index_)
const {
619 return my_substitute;
615 OutputValue_
fill(
bool, Index_)
const {
…}
632template<
bool pass_,
typename OutputValue_,
typename InputValue_,
typename Index_>
643template<
bool pass_,
typename OutputValue_,
typename InputValue_,
typename Index_>
654template<
bool pass_,
typename OutputValue_,
typename InputValue_,
typename Index_>
661template<
bool pass_ = true,
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int>
662std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteIsnan(OutputValue_ substitute) {
663 return std::make_shared<DelayedUnaryIsometricSubstituteIsnanHelper<pass_, OutputValue_, InputValue_, Index_> >(substitute);
666template<
bool pass_ = true,
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int>
667std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteIsinf(OutputValue_ substitute) {
668 return std::make_shared<DelayedUnaryIsometricSubstituteIsinfHelper<pass_, OutputValue_, InputValue_, Index_> >(substitute);
671template<
bool pass_ = true,
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int>
672std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricSubstituteIsfinite(OutputValue_ substitute) {
673 return std::make_shared<DelayedUnaryIsometricSubstituteIsfiniteHelper<pass_, OutputValue_, InputValue_, Index_> >(substitute);
Helper operation interface for DelayedUnaryIsometricOperation.
Definition helper_interface.hpp:27
Delayed special value substitution.
Definition substitute_helpers.hpp:558
std::optional< Index_ > ncol() const
Definition substitute_helpers.hpp:576
void dense(bool, Index_, Index_, Index_ length, const InputValue_ *input, OutputValue_ *output) const
Definition substitute_helpers.hpp:598
bool zero_depends_on_column() const
Definition substitute_helpers.hpp:585
bool zero_depends_on_row() const
Definition substitute_helpers.hpp:581
DelayedUnaryIsometricSpecialSubstituteHelper(OutputValue_ substitute)
Definition substitute_helpers.hpp:563
bool non_zero_depends_on_column() const
Definition substitute_helpers.hpp:593
std::optional< Index_ > nrow() const
Definition substitute_helpers.hpp:572
void sparse(bool, Index_, Index_ number, const InputValue_ *input_value, const Index_ *, OutputValue_ *output_value) const
Definition substitute_helpers.hpp:611
bool is_sparse() const
Definition substitute_helpers.hpp:607
void dense(bool, Index_, const std::vector< Index_ > &indices, const InputValue_ *input, OutputValue_ *output) const
Definition substitute_helpers.hpp:602
bool non_zero_depends_on_row() const
Definition substitute_helpers.hpp:589
OutputValue_ fill(bool, Index_) const
Definition substitute_helpers.hpp:615
Helper for delayed scalar substitution.
Definition substitute_helpers.hpp:60
bool non_zero_depends_on_column() const
Definition substitute_helpers.hpp:98
bool is_sparse() const
Definition substitute_helpers.hpp:112
DelayedUnaryIsometricSubstituteScalarHelper(Scalar_ compared, OutputValue_ substitute)
Definition substitute_helpers.hpp:67
std::optional< Index_ > nrow() const
Definition substitute_helpers.hpp:77
void sparse(bool, Index_, Index_ number, const InputValue_ *input_value, const Index_ *, OutputValue_ *output_value) const
Definition substitute_helpers.hpp:116
void dense(bool, Index_, const std::vector< Index_ > &indices, const InputValue_ *input, OutputValue_ *output) const
Definition substitute_helpers.hpp:107
OutputValue_ fill(bool, Index_) const
Definition substitute_helpers.hpp:120
bool non_zero_depends_on_row() const
Definition substitute_helpers.hpp:94
bool zero_depends_on_column() const
Definition substitute_helpers.hpp:90
void dense(bool, Index_, Index_, Index_ length, const InputValue_ *input, OutputValue_ *output) const
Definition substitute_helpers.hpp:103
std::optional< Index_ > ncol() const
Definition substitute_helpers.hpp:81
bool zero_depends_on_row() const
Definition substitute_helpers.hpp:86
Delayed vector comparisons.
Definition substitute_helpers.hpp:260
std::optional< Index_ > nrow() const
Definition substitute_helpers.hpp:295
bool is_sparse() const
Definition substitute_helpers.hpp:359
bool zero_depends_on_row() const
Definition substitute_helpers.hpp:312
DelayedUnaryIsometricSubstituteVectorHelper(ComparedVector_ compared, SubstituteVector_ substitute, bool by_row)
Definition substitute_helpers.hpp:272
void dense(bool row, Index_ idx, const std::vector< Index_ > &indices, const InputValue_ *input, OutputValue_ *output) const
Definition substitute_helpers.hpp:343
std::optional< Index_ > ncol() const
Definition substitute_helpers.hpp:303
void dense(bool row, Index_ idx, Index_ start, Index_ length, const InputValue_ *input, OutputValue_ *output) const
Definition substitute_helpers.hpp:329
OutputValue_ fill(bool row, Index_ idx) const
Definition substitute_helpers.hpp:377
void sparse(bool row, Index_ idx, Index_ number, const InputValue_ *input_value, const Index_ *indices, OutputValue_ *output_value) const
Definition substitute_helpers.hpp:363
bool non_zero_depends_on_row() const
Definition substitute_helpers.hpp:320
bool zero_depends_on_column() const
Definition substitute_helpers.hpp:316
bool non_zero_depends_on_column() const
Definition substitute_helpers.hpp:324
Utilities for delayed comparison operations.
Flexible representations for matrix data.
Definition Extractor.hpp:15
Interface for tatami::DelayedUnaryIsometricOperation helpers.