Files
Flux/include/numerics/matmean.h
T
Bausager 48f329feef
Sync public mirror / sync (push) Failing after 27s
Regulaization
Started on regulaization in  Loss.h. I need to refactor the matsum.h since I need a total sum over the matrix. Also matmul needs a elementwise matmul function, which is the next this in the ragulaization
2026-01-03 22:10:50 +01:00

86 lines
1.7 KiB
C++

#pragma once
#include "./utils/vector.h"
#include "./utils/matrix.h"
#include "./core/omp_config.h"
namespace numerics{
template <typename T>
T matmean(const 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(const 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(const utils::Matrix<T>& A, utils::Vector<T>& b) {
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[i] /= static_cast<T>(cols);
}
}
template <typename T>
utils::Vector<T> matmean_row(const 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(const utils::Matrix<T>& A) {
utils::Vector<T> b(A.cols(), T{0});
inplace_matmean_cols(A, b);
return b;
}
} // namespace numerics