Files
Flux-openbuild/include/numerics/matclip.h
2025-10-07 11:09:55 +00:00

77 lines
1.5 KiB
C++

#pragma once
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
void inplace_matclip_high(utils::Matrix<T>& A, T high){
uint64_t rows = A.rows();
uint64_t cols = A.cols();
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (A(i,j) > high){
A(i,j) = high;
}
}
}
}
template <typename T>
void inplace_matclip_low(utils::Matrix<T>& A, T low){
uint64_t rows = A.rows();
uint64_t cols = A.cols();
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (A(i,j) < low){
A(i,j) = low;
}
}
}
}
template <typename T>
void inplace_matclip(utils::Matrix<T>& A, T low, T high){
uint64_t rows = A.rows();
uint64_t cols = A.cols();
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (A(i,j) > high){
A(i,j) = high;
}else if (A(i,j) < low){
A(i,j) = low;
}
}
}
}
template <typename Td, typename Ti>
utils::Matrix<Td> matclip_high(const utils::Matrix<Ti>& A, Td high){
utils::Matrix<Td> B = A;
inplace_matclip_high(B, high);
return B;
}
template <typename Td, typename Ti>
utils::Matrix<Td> matclip_low(const utils::Matrix<Ti>& A, Td low){
utils::Matrix<Td> B = A;
inplace_matclip_low(B, low);
return B;
}
template <typename Td, typename Ti>
utils::Matrix<Td> matclip(const utils::Matrix<Ti>& A, Td low, Td high){
utils::Matrix<Td> B = A;
inplace_matclip(B, low, high);
return B;
}
} // namespace numerics