Started Loss, done softmax, up to p.125

I've implemented alot of support functions that needs to be refactored, optimised and tested; mean.h, exponential.h, matdiv.h matsum.h matsubtract.h. Maybe we need to have a look at if matdiv/matmul should be in the same. Same with matadd/matsubtract and if some of it should be in matvec.h.
This commit is contained in:
2025-10-05 19:45:37 +02:00
parent 1b59713565
commit ea359f3b09
18 changed files with 407 additions and 56 deletions
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include <cmath>
#include "./utils/vector.h"
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
T exponential(const T a){
return std::exp(a);
}
template <typename T>
utils::Vector<T> exponential(const utils::Vector<T>& a){
utils::Vector<T> b = a;
for (uint64_t i = 0; i < a.size(); ++i){
b[i] = numerics::exponential(a[i]);
}
return b;
}
template <typename T>
utils::Matrix<T> exponential(const utils::Matrix<T>& A){
utils::Matrix<T> B = A;
for (uint64_t i = 0; i < A.rows(); ++i){
for (uint64_t j = 0; j < A.cols(); ++j){
B(i,j) = numerics::exponential(A(i,j));
}
}
return B;
}
} // namespace numerics
+1 -4
View File
@@ -1,15 +1,12 @@
#ifndef _matadd_n_
#define _matadd_n_
#include "./utils/vector.h"
#include "./utils/matrix.h"
#include "./core/omp_config.h"
namespace numerics{
// =================================================
// y = A * x (MatrixVector product)
// =================================================
template <typename T>
void inplace_matadd_colvec(utils::Matrix<T>& A, const utils::Vector<T>& x) {
+38
View File
@@ -0,0 +1,38 @@
#ifndef _matdiv_n_
#define _matdiv_n_
#include "./utils/matrix.h"
#include "./core/omp_config.h"
namespace numerics{
// ---------------- Serial baseline ----------------
template <typename T>
utils::Matrix<T> matdiv(const utils::Matrix<T>& A, const utils::Vector<T>& b, std::string method){
utils::Matrix<T> C = A;
if (method == "row"){
for (uint64_t i = 0; i < A.rows(); ++i){
for (uint64_t j = 0; j < A.cols(); ++j){
C(i,j) /= b[j];
}
}
}else if (method == "col"){
for (uint64_t i = 0; i < A.rows(); ++i){
for (uint64_t j = 0; j < A.cols(); ++j){
C(i,j) /= b[i];
}
}
}else{
throw std::runtime_error("matdiv: choose div by: 'row' or 'col'");
}
return C;
}
} // namespace numerics
#endif // _matdiv_n_
+102
View File
@@ -0,0 +1,102 @@
#ifndef _matsubtract_n_
#define _matsubtract_n_
#include "./utils/vector.h"
#include "./utils/matrix.h"
#include "./core/omp_config.h"
namespace numerics{
template <typename T>
void inplace_matsubtract_colvec(utils::Matrix<T>& A, const utils::Vector<T>& x) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
if (rows != x.size()) {
throw std::runtime_error("inplace_matsubtract_colvec: dimension mismatch");
}
for (uint64_t i = 0; i < cols; ++i) {
for (uint64_t j = 0; j < rows; ++j) {
A(j, i) -= x[j];
}
}
}
template <typename T>
void inplace_matsubtract_rowvec(utils::Matrix<T>& A, const utils::Vector<T>& x) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
if (cols != x.size()) {
throw std::runtime_error("inplace_matsubtract_rowvec: dimension mismatch");
}
for (uint64_t i = 0; i < cols; ++i) {
for (uint64_t j = 0; j < rows; ++j) {
A(j, i) -= x[i];
}
}
}
template <typename T>
utils::Matrix<T> matsubtract_colvec(const utils::Matrix<T>& A, const utils::Vector<T>& x) {
//const uint64_t rows = A.rows();
//const uint64_t cols = A.cols();
utils::Matrix<T> B = A;
inplace_matsubtract_colvec(B, x);
return B;
}
template <typename T>
utils::Matrix<T> matsubtract_rowvec(const utils::Matrix<T>& A, const utils::Vector<T>& x) {
//const uint64_t rows = A.rows();
//const uint64_t cols = A.cols();
utils::Matrix<T> B = A;
inplace_matsubtract_rowvec(B, x);
return B;
}
template <typename T>
utils::Matrix<T> matsubtract(const utils::Matrix<T>& A, const utils::Vector<T>& x, std::string method = "auto"){
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
const uint64_t N = x.size();
if (method=="auto"){
if (rows==cols){
throw std::runtime_error("matsubtract: too many options for dimensions");
} else if (rows == N){
return matsubtract_rowvec(A, x);
} else if (cols == N){
return matsubtract_colvec(A, x);
}else{
throw std::runtime_error("matsubtract: undefined fault - auto");
}
}else if(method=="row"){
return matsubtract_rowvec(A, x);
} else if (method=="col"){
return matsubtract_colvec(A, x);
}else{
throw std::runtime_error("matsubtract: undefined fault - defined method");
}
}
} // namespace numerics
#endif // _matsubtract_n_
+39
View File
@@ -0,0 +1,39 @@
#ifndef _matsum_n_
#define _matsum_n_
#include "./utils/vector.h"
#include "./utils/matrix.h"
#include "./core/omp_config.h"
namespace numerics{
template <typename T>
utils::Vector<T> matsum(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_
+27 -3
View File
@@ -39,13 +39,37 @@ namespace numerics{
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
+31
View File
@@ -0,0 +1,31 @@
#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 mean(utils::Vector<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;
}
} // namespace numerics
#endif // _mean_n_
+5
View File
@@ -6,11 +6,16 @@
#include "./numerics/transpose.h"
#include "./numerics/inverse.h"
#include "./numerics/matmul.h"
#include "./numerics/matdiv.h"
#include "./numerics/matvec.h"
#include "./numerics/matadd.h"
#include "./numerics/matsubtract.h"
#include "./numerics/matsum.h"
#include "./numerics/min.h"
#include "./numerics/max.h"
#include "./numerics/abs.h"
#include "./numerics/mean.h"
#include "./numerics/exponential.h"
#include "./numerics/interpolation1d.h" // base