tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
arithmetic_helpers.hpp
Go to the documentation of this file.
1#ifndef TATAMI_ISOMETRIC_UNARY_ARITHMETIC_HELPERS_H
2#define TATAMI_ISOMETRIC_UNARY_ARITHMETIC_HELPERS_H
3
4#include "../arithmetic_utils.hpp"
5#include <vector>
6#include <limits>
7#include <type_traits>
8
15namespace tatami {
16
20template<ArithmeticOperation op_, bool right_, typename InputValue_, typename Index_, typename Scalar_, typename OutputValue_>
22 for (Index_ i = 0; i < length; ++i) {
23 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
24 auto& val = output[i];
26 } else {
28 }
29 }
30}
31
32// The '*_actual_sparse' and '*_zero' functions should be mirrors of each other;
33// we enforce this by putting their logic all in the same place.
34template<bool check_only_, ArithmeticOperation op_, bool right_, typename InputValue_, typename Scalar_>
37 if constexpr(right_) {
38 if (scalar) {
40 if constexpr(check_only_) {
41 return val == 0;
42 } else {
43 return val;
44 }
45 }
46 }
47
48 if constexpr(check_only_) {
49 return false;
50 } else {
51 throw std::runtime_error("division by zero is not supported");
53 }
54
55 } else {
57 if constexpr(check_only_) {
58 return val == 0;
59 } else {
60 return val;
61 }
62 }
63}
64
65template<ArithmeticOperation op_, bool right_, typename InputValue_, typename Scalar_>
68}
69
70template<ArithmeticOperation op_, bool right_, typename InputValue_, typename Scalar_>
73}
91template<ArithmeticOperation op_, bool right_, typename InputValue_, typename Scalar_>
93public:
100
101private:
102 Scalar_ my_scalar;
103 bool my_sparse;
104
105public:
109 static constexpr bool is_basic = false;
110
111 bool is_sparse() const {
112 return my_sparse;
113 }
118public:
122 template<typename Index_, typename OutputValue_>
123 void dense(bool, Index_, Index_, Index_ length, const InputValue_* input, OutputValue_* output) const {
125 }
126
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);
130 }
131
132 template<typename Index_, typename OutputValue_>
133 void sparse(bool, Index_, Index_ number, const InputValue_* input_value, const Index_*, OutputValue_* output_value) const {
135 }
136
137 template<typename OutputValue_, typename, typename Index_>
138 OutputValue_ fill(bool, Index_) const {
139 // We perform the operation with the InputValue_ before casting it to
140 // the OutputValue_, which is consistent with the behavior of all other
141 // methods. See ../arithmetic_utils.hpp for some comments about the
142 // safety of this cast when the value is known at compile time.
144 }
148};
149
163template<ArithmeticOperation op_, bool right_, typename InputValue_, typename Vector_>
165public:
174 for (auto x : my_vector) {
176 my_sparse = false;
177 break;
178 }
179 }
180 }
181
182private:
183 Vector_ my_vector;
184 bool my_by_row;
185 bool my_sparse = true;
186
187public:
191 static constexpr bool is_basic = false;
192
193 bool zero_depends_on_row() const {
194 return my_by_row;
195 }
196
197 bool zero_depends_on_column() const {
198 return !my_by_row;
199 }
200
201 bool non_zero_depends_on_row() const {
202 return my_by_row;
203 }
204
205 bool non_zero_depends_on_column() const {
206 return !my_by_row;
207 }
208
209 bool is_sparse() const {
210 return my_sparse;
211 }
216public:
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) {
224 } else {
225 for (Index_ i = 0; i < length; ++i) {
226 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
227 auto& val = output[i];
229 } else {
231 }
232 }
233 }
234 }
235
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);
240 } else {
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];
246 } else {
248 }
249 }
250 }
251 }
252
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) {
257 } else {
258 for (Index_ i = 0; i < number; ++i) {
259 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
260 auto& val = output_value[i];
262 } else {
264 }
265 }
266 }
267 }
268
269 template<typename OutputValue_, typename, typename Index_>
270 OutputValue_ fill(bool row, Index_ idx) const {
271 if (row == my_by_row) {
273 } else {
274 // We should only get to this point if it's sparse, otherwise no
275 // single fill value would work across the length of my_vector.
276 return 0;
277 }
278 }
282};
283
291template<typename InputValue_ = double, typename Scalar_>
295
304template<bool right_, typename InputValue_ = double, typename Scalar_>
308
316template<typename InputValue_ = double, typename Scalar_>
320
329template<bool right_, typename InputValue_ = double, typename Scalar_>
333
342template<bool right_, typename InputValue_ = double, typename Scalar_>
346
355template<bool right_, typename InputValue_ = double, typename Scalar_>
359
368template<bool right_, typename InputValue_ = double, typename Scalar_>
372
381template<typename InputValue_ = double, typename Vector_>
385
395template<bool right_, typename InputValue_ = double, typename Vector_>
399
408template<typename InputValue_ = double, typename Vector_>
412
422template<bool right_, typename InputValue_ = double, typename Vector_>
426
436template<bool right_, typename InputValue_ = double, typename Vector_>
440
450template<bool right_, typename InputValue_ = double, typename Vector_>
454
464template<bool right_, typename InputValue_ = double, typename Vector_>
468
469}
470
471#endif
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
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35
DelayedUnaryIsometricArithmeticScalar< ArithmeticOperation::SUBTRACT, right_, InputValue_, Scalar_ > make_DelayedUnaryIsometricSubtractScalar(Scalar_ scalar)
Definition arithmetic_helpers.hpp:305