Files
Flux/include/numerics/matsum.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

53 lines
983 B
C++

#ifndef _matsum_n_
#define _matsum_n_
#include "./utils/vector.h"
#include "./utils/matrix.h"
#include "./core/omp_config.h"
namespace numerics{
template <typename T>
T matsum_coeff(const utils::Matrix<T>& A) {
T b;
for (uint64_t i = 0; i < A.cols(); ++i){
for (uint64_t j = 0; j < A.rows(); ++j){
b += A(i, j);
}
}
return b;
}
template <typename T>
utils::Vector<T> matsum(const utils::Matrix<T>& A, std::string method) {
utils::Vector<T> b;
if (method == "row"){
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] += A(j, i);
}
}
}else if (method == "col"){
b.resize(A.rows(), T{0});
for (uint64_t i = 0; i < A.cols(); ++i){
for (uint64_t j = 0; j < A.rows(); ++j){
b[j] += A(j, i);
}
}
}else{
throw std::runtime_error("matsum: choose sum by: 'row' or 'col'");
}
return b;
}
} // namespace numerics
#endif // _matadd_n_