Files

84 lines
1.8 KiB
C++

#pragma once
#include <cstdint> //uint64_t
//#include <stdexcept> // std::runtime_error
#include "utils/vector.h"
#include "utils/matrix.h"
namespace numerics::detail{
// ---------------- (Scalar, Scalar) -> Scalar ----------------
template <typename T>
T max_serial(const T a, const T b) {
if (a >= b){
return a;
}
return b;
}
// ---------------- 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