Sync public subset from Flux
This commit is contained in:
88
include/numerics/matmean.h
Normal file
88
include/numerics/matmean.h
Normal file
@@ -0,0 +1,88 @@
|
||||
#ifndef _mean_n_
|
||||
#define _mean_n_
|
||||
|
||||
#include "./utils/vector.h"
|
||||
#include "./utils/matrix.h"
|
||||
#include "./core/omp_config.h"
|
||||
|
||||
namespace numerics{
|
||||
|
||||
template <typename T>
|
||||
T matmean(utils::Matrix<T>& A) {
|
||||
|
||||
T mean(T{0});
|
||||
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < cols; ++i) {
|
||||
for (uint64_t j = 0; j < rows; ++j) {
|
||||
mean += A(j, i);
|
||||
}
|
||||
}
|
||||
mean /= (static_cast<T>(rows)* static_cast<T>(cols));
|
||||
return mean;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
void inplace_matmean_row(utils::Matrix<T>& A, utils::Vector<T>& b) {
|
||||
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
if (b.size() != cols){
|
||||
b.resize(cols, T{0});
|
||||
}
|
||||
|
||||
for(uint64_t j = 0; j < cols; ++j){
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
b[j] += A(i, j);
|
||||
}
|
||||
b[j] =/ static_cast<T>(rows);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void inplace_matmean_cols(utils::Matrix<T>& A) {
|
||||
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
if (b.size() != rows){
|
||||
b.resize(rows, T{0});
|
||||
}
|
||||
|
||||
for(uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
b[i] += A(i, j);
|
||||
}
|
||||
b[j] =/ static_cast<T>(cols);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
utils::Vector<T> matmean_row(utils::Matrix<T>& A) {
|
||||
|
||||
utils:Vector<T> b(A.rows(), T{0});
|
||||
|
||||
inplace_matmean_row(A, b);
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
utils::Vector<T> matmean_col(utils::Matrix<T>& A) {
|
||||
|
||||
utils:Vector<T> b(A.cols(), T{0});
|
||||
|
||||
inplace_matmean_cols(A, b);
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
} // namespace numerics
|
||||
|
||||
#endif // _mean_n_
|
||||
Reference in New Issue
Block a user