77 lines
1.3 KiB
C++
77 lines
1.3 KiB
C++
#pragma once
|
|
|
|
|
|
#include "./utils/vector.h"
|
|
#include "./utils/matrix.h"
|
|
|
|
|
|
namespace numerics{
|
|
|
|
template <typename T>
|
|
T max(const T a, const T b){
|
|
|
|
if(a < b){
|
|
return b;
|
|
}else{
|
|
return a;
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
void inplace_max(utils::Matrix<T>& A, const T b){
|
|
|
|
const uint64_t rows = A.rows();
|
|
const uint64_t cols = A.cols();
|
|
|
|
for (uint64_t i = 0; i < rows; ++i){
|
|
for (uint64_t j = 0; j < cols; ++j){
|
|
|
|
if (b > A(i,j)){
|
|
//std::cout << A(i,j) << std::endl;
|
|
A(i,j) = b;
|
|
//std::cout << A(i,j) << std::endl;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
utils::Matrix<T> max(const utils::Matrix<T>& A, const T b){
|
|
|
|
utils::Matrix<T> B = A;
|
|
inplace_max(B, b);
|
|
return B;
|
|
}
|
|
|
|
template <typename T>
|
|
utils::Vector<T> max(const utils::Matrix<T>& A, std::string method){
|
|
|
|
utils::Vector<T> b;
|
|
|
|
if (method == "cols"){
|
|
b.resize(A.cols(), T{0});
|
|
for (uint64_t i = 0; i < A.cols(); ++i){
|
|
for (uint64_t j = 0; j < A.rows(); ++j){
|
|
b[i] = max(A(j, i), b[i]);
|
|
}
|
|
}
|
|
}else if (method == "rows"){
|
|
b.resize(A.rows(), T{0});
|
|
for (uint64_t i = 0; i < A.rows(); ++i){
|
|
for (uint64_t j = 0; j < A.cols(); ++j){
|
|
//std::cout << i << ":" << j << std::endl;
|
|
b[i] = max(A(i, j), b[i]);
|
|
}
|
|
}
|
|
}else{
|
|
throw std::runtime_error("max: choose 'rows or 'cols'");
|
|
}
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace numerics
|
|
|