Files
Flux/include/numerics/detail/max_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

74 lines
1.6 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 max_serial(const utils::Matrix<T>& A) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
T max = A(0,0);
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (max < A(i,j)){
max = A(i,j);
}
}
}
return max;
}
// ---------------- Vector -> Scalar ----------------
template <typename T>
T max_serial(const utils::Vector<T>& v) {
const uint64_t N = v.size();
T max = v[0];
for (uint64_t i = 0; i < N; ++i){
if (max < v[i]){
max = v[i];
}
}
return max;
}
// ---------------- Matrix -> Vector ----------------
template <typename T>
utils::Vector<T> max_rowwise_serial(const utils::Matrix<T>& A) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
utils::Vector<T> max(rows, T{0});
for (uint64_t i = 0; i < rows; ++i){
max[i] = A(i,0);
for (uint64_t j = 1; j < cols; ++j){
if (max[i] < A(i,j)){
max[i] = A(i,j);
}
}
}
return max;
}
template <typename T>
utils::Vector<T> max_colwise_serial(const utils::Matrix<T>& A) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
utils::Vector<T> max(cols, T{0});
for (uint64_t j = 0; j < cols; ++j){
max[j] = A(0, j);
for (uint64_t i = 1; i < rows; ++i){
if (max[j] < A(i,j)){
max[j] = A(i,j);
}
}
}
return max;
}
} // namespace numerics