1#ifndef TATAMI_ISOMETRIC_UNARY_ARITHMETIC_HELPERS_H
2#define TATAMI_ISOMETRIC_UNARY_ARITHMETIC_HELPERS_H
20template<ArithmeticOperation op_,
bool right_,
typename InputValue_,
typename Index_,
typename Scalar_,
typename OutputValue_>
21void delayed_arithmetic_run_simple(
const InputValue_* input, Index_ length, Scalar_ scalar, OutputValue_* output) {
22 for (Index_ i = 0; i < length; ++i) {
23 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
24 auto& val = output[i];
25 val = delayed_arithmetic<op_, right_>(val, scalar);
27 output[i] = delayed_arithmetic<op_, right_>(input[i], scalar);
34template<
bool check_only_, ArithmeticOperation op_,
bool right_,
typename InputValue_,
typename Scalar_>
35auto delayed_arithmetic_zeroish(Scalar_ scalar) {
36 if constexpr(has_unsafe_divide_by_zero<op_, right_, InputValue_, Scalar_>()) {
37 if constexpr(right_) {
39 auto val = delayed_arithmetic<op_, right_, InputValue_>(0, scalar);
40 if constexpr(check_only_) {
48 if constexpr(check_only_) {
51 throw std::runtime_error(
"division by zero is not supported");
52 return static_cast<decltype(delayed_arithmetic<op_, right_, InputValue_>(0, scalar))
>(1);
56 auto val = delayed_arithmetic<op_, right_, InputValue_>(0, scalar);
57 if constexpr(check_only_) {
65template<ArithmeticOperation op_,
bool right_,
typename InputValue_,
typename Scalar_>
66bool delayed_arithmetic_actual_sparse(Scalar_ scalar) {
67 return delayed_arithmetic_zeroish<true, op_, right_, InputValue_, Scalar_>(scalar);
70template<ArithmeticOperation op_,
bool right_,
typename InputValue_,
typename Scalar_>
71auto delayed_arithmetic_zero(Scalar_ scalar) {
72 return delayed_arithmetic_zeroish<false, op_, right_, InputValue_, Scalar_>(scalar);
91template<ArithmeticOperation op_,
bool right_,
typename InputValue_,
typename Scalar_>
98 my_sparse = delayed_arithmetic_actual_sparse<op_, right_, InputValue_>(scalar);
109 static constexpr bool is_basic =
false;
111 bool is_sparse()
const {
122 template<
typename Index_,
typename OutputValue_>
123 void dense(
bool, Index_, Index_, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
124 delayed_arithmetic_run_simple<op_, right_>(input, length, my_scalar, output);
127 template<
typename Index_,
typename OutputValue_>
128 void dense(
bool, Index_,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
129 delayed_arithmetic_run_simple<op_, right_>(input,
static_cast<Index_
>(indices.size()), my_scalar, output);
132 template<
typename Index_,
typename OutputValue_>
133 void sparse(
bool, Index_, Index_ number,
const InputValue_* input_value,
const Index_*, OutputValue_* output_value)
const {
134 delayed_arithmetic_run_simple<op_, right_>(input_value, number, my_scalar, output_value);
137 template<
typename OutputValue_,
typename,
typename Index_>
138 OutputValue_ fill(
bool, Index_)
const {
143 return delayed_arithmetic_zero<op_, right_, InputValue_>(my_scalar);
163template<ArithmeticOperation op_,
bool right_,
typename InputValue_,
typename Vector_>
174 for (
auto x : my_vector) {
175 if (!delayed_arithmetic_actual_sparse<op_, right_, InputValue_>(x)) {
185 bool my_sparse =
true;
191 static constexpr bool is_basic =
false;
193 bool zero_depends_on_row()
const {
197 bool zero_depends_on_column()
const {
201 bool non_zero_depends_on_row()
const {
205 bool non_zero_depends_on_column()
const {
209 bool is_sparse()
const {
220 template<
typename Index_,
typename OutputValue_>
221 void dense(
bool row, Index_ idx, Index_ start, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
222 if (row == my_by_row) {
223 delayed_arithmetic_run_simple<op_, right_>(input, length, my_vector[idx], output);
225 for (Index_ i = 0; i < length; ++i) {
226 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
227 auto& val = output[i];
228 val = delayed_arithmetic<op_, right_>(val, my_vector[i + start]);
230 output[i] = delayed_arithmetic<op_, right_>(input[i], my_vector[i + start]);
236 template<
typename Index_,
typename OutputValue_>
237 void dense(
bool row, Index_ idx,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
238 if (row == my_by_row) {
239 delayed_arithmetic_run_simple<op_, right_>(input,
static_cast<Index_
>(indices.size()), my_vector[idx], output);
241 Index_ length = indices.size();
242 for (Index_ i = 0; i < length; ++i) {
243 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
244 auto& val = output[i];
245 val = delayed_arithmetic<op_, right_>(val, my_vector[indices[i]]);
247 output[i] = delayed_arithmetic<op_, right_>(input[i], my_vector[indices[i]]);
253 template<
typename Index_,
typename OutputValue_>
254 void sparse(
bool row, Index_ idx, Index_ number,
const InputValue_* input_value,
const Index_* indices, OutputValue_* output_value)
const {
255 if (row == my_by_row) {
256 delayed_arithmetic_run_simple<op_, right_>(input_value, number, my_vector[idx], output_value);
258 for (Index_ i = 0; i < number; ++i) {
259 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
260 auto& val = output_value[i];
261 val = delayed_arithmetic<op_, right_>(val, my_vector[indices[i]]);
263 output_value[i] = delayed_arithmetic<op_, right_>(input_value[i], my_vector[indices[i]]);
269 template<
typename OutputValue_,
typename,
typename Index_>
270 OutputValue_ fill(
bool row, Index_ idx)
const {
271 if (row == my_by_row) {
272 return delayed_arithmetic_zero<op_, right_, InputValue_>(my_vector[idx]);
291template<
typename InputValue_ =
double,
typename Scalar_>
304template<
bool right_,
typename InputValue_ =
double,
typename Scalar_>
316template<
typename InputValue_ =
double,
typename Scalar_>
329template<
bool right_,
typename InputValue_ =
double,
typename Scalar_>
342template<
bool right_,
typename InputValue_ =
double,
typename Scalar_>
355template<
bool right_,
typename InputValue_ =
double,
typename Scalar_>
368template<
bool right_,
typename InputValue_ =
double,
typename Scalar_>
381template<
typename InputValue_ =
double,
typename Vector_>
395template<
bool right_,
typename InputValue_ =
double,
typename Vector_>
408template<
typename InputValue_ =
double,
typename Vector_>
422template<
bool right_,
typename InputValue_ =
double,
typename Vector_>
436template<
bool right_,
typename InputValue_ =
double,
typename Vector_>
450template<
bool right_,
typename InputValue_ =
double,
typename Vector_>
464template<
bool right_,
typename InputValue_ =
double,
typename Vector_>
Utilities for delayed arithmetic operations.
Delayed unary isometric scalar arithmetic.
Definition arithmetic_helpers.hpp:92
DelayedUnaryIsometricArithmeticScalar(Scalar_ scalar)
Definition arithmetic_helpers.hpp:97
Delayed unary isometric vector arithmetic.
Definition arithmetic_helpers.hpp:164
DelayedUnaryIsometricArithmeticVector(Vector_ vector, bool by_row)
Definition arithmetic_helpers.hpp:173
Flexible representations for matrix data.
Definition Extractor.hpp:15
DelayedUnaryIsometricArithmeticVector< ArithmeticOperation::DIVIDE, right_, InputValue_, Vector_ > make_DelayedUnaryIsometricDivideVector(Vector_ vector, bool by_row)
Definition arithmetic_helpers.hpp:423
DelayedUnaryIsometricArithmeticVector< ArithmeticOperation::POWER, right_, InputValue_, Vector_ > make_DelayedUnaryIsometricPowerVector(Vector_ vector, bool by_row)
Definition arithmetic_helpers.hpp:437
DelayedUnaryIsometricArithmeticScalar< ArithmeticOperation::DIVIDE, right_, InputValue_, Scalar_ > make_DelayedUnaryIsometricDivideScalar(Scalar_ scalar)
Definition arithmetic_helpers.hpp:330
DelayedUnaryIsometricArithmeticVector< ArithmeticOperation::ADD, true, InputValue_, Vector_ > make_DelayedUnaryIsometricAddVector(Vector_ vector, bool by_row)
Definition arithmetic_helpers.hpp:382
DelayedUnaryIsometricArithmeticScalar< ArithmeticOperation::MODULO, right_, InputValue_, Scalar_ > make_DelayedUnaryIsometricModuloScalar(Scalar_ scalar)
Definition arithmetic_helpers.hpp:356
DelayedUnaryIsometricArithmeticScalar< ArithmeticOperation::INTEGER_DIVIDE, right_, InputValue_, Scalar_ > make_DelayedUnaryIsometricIntegerDivideScalar(Scalar_ scalar)
Definition arithmetic_helpers.hpp:369
DelayedUnaryIsometricArithmeticVector< ArithmeticOperation::MODULO, right_, InputValue_, Vector_ > make_DelayedUnaryIsometricModuloVector(Vector_ vector, bool by_row)
Definition arithmetic_helpers.hpp:451
DelayedUnaryIsometricArithmeticScalar< ArithmeticOperation::MULTIPLY, true, InputValue_, Scalar_ > make_DelayedUnaryIsometricMultiplyScalar(Scalar_ scalar)
Definition arithmetic_helpers.hpp:317
DelayedUnaryIsometricArithmeticVector< ArithmeticOperation::INTEGER_DIVIDE, right_, InputValue_, Vector_ > make_DelayedUnaryIsometricIntegerDivideVector(Vector_ vector, bool by_row)
Definition arithmetic_helpers.hpp:465
DelayedUnaryIsometricArithmeticScalar< ArithmeticOperation::POWER, right_, InputValue_, Scalar_ > make_DelayedUnaryIsometricPowerScalar(Scalar_ scalar)
Definition arithmetic_helpers.hpp:343
DelayedUnaryIsometricArithmeticScalar< ArithmeticOperation::ADD, true, InputValue_, Scalar_ > make_DelayedUnaryIsometricAddScalar(Scalar_ scalar)
Definition arithmetic_helpers.hpp:292
DelayedUnaryIsometricArithmeticVector< ArithmeticOperation::MULTIPLY, true, InputValue_, Vector_ > make_DelayedUnaryIsometricMultiplyVector(Vector_ vector, bool by_row)
Definition arithmetic_helpers.hpp:409
DelayedUnaryIsometricArithmeticVector< ArithmeticOperation::SUBTRACT, right_, InputValue_, Vector_ > make_DelayedUnaryIsometricSubtractVector(Vector_ vector, bool by_row)
Definition arithmetic_helpers.hpp:396
DelayedUnaryIsometricArithmeticScalar< ArithmeticOperation::SUBTRACT, right_, InputValue_, Scalar_ > make_DelayedUnaryIsometricSubtractScalar(Scalar_ scalar)
Definition arithmetic_helpers.hpp:305