Files
Flux/include/numerics/detail/sum_serial.h
T
Bausager 9709949a5b
Sync public mirror / sync (push) Failing after 27s
Refactor progress
Next step is ramdom and equal. I also need to have a look at add and the serial naming of the functions.
2026-01-13 20:07:27 +01:00

64 lines
1.5 KiB
C++

#pragma once
#include <cstdint> //uint64_t
//#include <stdexcept> // std::runtime_error
#include "../utils/vector.h"
#include "../utils/matrix.h"
namespace numerics::detail{
// ---------------- Matrix -> Scalar ----------------
template <typename T>
T sum_serial(const utils::Matrix<T>& A) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
T sum = T{0};
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
sum += A(i,j);
}
}
return sum;
}
// ---------------- Vector -> Scalar ----------------
template <typename T>
T sum_serial(const utils::Vector<T>& v) {
const uint64_t N = v.size();
T sum = T{0};
for (uint64_t i = 0; i < N; ++i){
sum += v[i];
}
return sum;
}
// ---------------- Matrix -> Vector ----------------
template <typename T>
utils::Vector<T> sum_rowwise_serial(const utils::Matrix<T>& A) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
utils::Vector<T> sum(cols, T{0});
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
sum[j] += A(i,j);
}
}
return sum;
}
template <typename T>
utils::Vector<T> sum_colwise_serial(const utils::Matrix<T>& A) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
utils::Vector<T> sum(rows, T{0});
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
sum[i] += A(i,j);
}
}
return sum;
}
} // namespace numerics