refactor
Sync public mirror / sync (push) Failing after 29s

almost complete. Need to doublecheck names for functions in *_serial.h
This commit is contained in:
2026-01-18 17:51:05 +01:00
parent 9709949a5b
commit 6b8a7ab582
15 changed files with 383 additions and 523 deletions
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include <cstdint> //uint64_t
#include <stdexcept> // std::runtime_error
#include "../utils/vector.h"
#include "../utils/matrix.h"
namespace numerics::detail{
// ---------------- Vector * Vector ----------------
template <typename T>
inline T dot_serial(const utils::Vector<T>& v, const utils::Vector<T>& p) {
const uint64_t N = v.size();
if (N != p.size()) {
throw std::runtime_error("dot_serial: dimension mismatch");
}
T dot_product = T{0};
for (uint64_t i = 0; i < N; ++i){
dot_product += v[i]*p[i];
}
return dot_product;
}
// ---------------- Matrix * Vector ----------------
template <typename T>
inline utils::Vector<T> dot_serial(const utils::Matrix<T>& A, const utils::Vector<T>& v) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
if (cols != v.size()) {
throw std::runtime_error("dot_serial: dimension mismatch");
}
utils::Vector<T> dot_product(rows, T{0});
for (uint64_t j = 0; j < cols; ++j){
for (uint64_t i = 0; i < rows; ++i){
dot_product[i] += A(i,j)*v[j];
}
}
return dot_product;
}
} // namespace numerics
+47
View File
@@ -0,0 +1,47 @@
#pragma once
#include <cstdint> //uint64_t
//#include <stdexcept> // std::runtime_error
#include "../utils/vector.h"
#include "../utils/matrix.h"
namespace numerics::detail{
// ---------------- Matrix ----------------
template <typename T>
inline bool equal_serial(const utils::Matrix<T>& A, const utils::Matrix<T> & B) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
if ((rows != B.rows()) || (cols != B.cols())){
return false;
}
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (A(i,j) != B(i,j)){
return false;
}
}
}
return true;
}
// ---------------- Vector ----------------
template <typename T>
inline bool equal_serial(const utils::Vector<T>& v, const utils::Vector<T>& p) {
const uint64_t N = v.size();
if (N != p.size()){
return false;
}
for (uint64_t i = 0; i < N; ++i){
if ((v[i] != p[i])){
return false;
}
}
return true;
}
} // namespace numerics
+49
View File
@@ -0,0 +1,49 @@
#pragma once
#include <cstdint> //uint64_t
//#include <stdexcept> // std::runtime_error
#include "../utils/vector.h"
#include "../utils/matrix.h"
#include <cmath> // std::abs
namespace numerics::detail{
// ---------------- Matrix ----------------
template <typename T>
inline bool isclose_serial(const utils::Matrix<T>& A, const utils::Matrix<T> & B, const T tol = T{1e-5}) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
if ((rows != B.rows()) || (cols != B.cols())){
return false;
}
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (static_cast<T>(std::abs(A(i,j)-B(i,j))) > tol){
return false;
}
}
}
return true;
}
// ---------------- Vector ----------------
template <typename T>
inline bool isclose_serial(const utils::Vector<T>& v, const utils::Vector<T>& p, const T tol = T{1e-5}) {
const uint64_t N = v.size();
if (N != p.size()){
return false;
}
for (uint64_t i = 0; i < N; ++i){
if (static_cast<T>(std::abs((v[i]-p[i]))) > tol){
return false;
}
}
return true;
}
} // namespace numerics
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include <cstdint> //uint64_t
#include <stdexcept> // std::runtime_error
#include "../utils/vector.h"
#include "../utils/matrix.h"
namespace numerics::detail{
// ---------------- Matrix * Matrix ----------------
template <typename T>
inline utils::Matrix<T> matmul_serial(const utils::Matrix<T>& A, const utils::Matrix<T>& B){
const uint64_t m = A.rows();
const uint64_t n = A.cols(); // also B.rows()
const uint64_t p = B.cols();
if(n != B.rows()){
throw std::runtime_error("matmul: dimension mismatch");
}
T tmp;
utils::Matrix<T> C(m, p, T{0});
for (uint64_t i = 0; i < m; ++i){
for (uint64_t j = 0; j < n; ++j){
tmp = A(i,j);
for (uint64_t k = 0; k < p; ++k){
C(i,k) += tmp * B(j,k);
}
}
}
return C;
}
} // namespace numerics
+63
View File
@@ -0,0 +1,63 @@
#pragma once
#include <cstdint> //uint64_t
//#include <stdexcept> // std::runtime_error
#include <random>
#include <type_traits>
#include "../utils/vector.h"
#include "../utils/matrix.h"
namespace numerics::detail{
// Shared engine
inline std::mt19937& rng() {
static std::random_device rd;
static std::mt19937 gen(rd());
return gen;
}
// Integral overload
template <
typename T,
typename std::enable_if<std::is_integral<T>::value, int>::type = 0
>
inline T random(const T low, const T high) {
std::uniform_int_distribution<T> dist(low, high);
return dist(rng());
}
// Floating-point overload
template <
typename T,
typename std::enable_if<std::is_floating_point<T>::value, int>::type = 0
>
inline T random(const T low, const T high) {
std::uniform_real_distribution<T> dist(low, high);
return dist(rng());
}
// ---------------- Matrix ----------------
template <typename T>
inline void inplace_random_serial(utils::Matrix<T>& A, const T low, const T high) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
A(i,j) = random(low, high);
}
}
}
// ---------------- Vector ----------------
template <typename T>
inline void inplace_random_serial(utils::Vector<T>& v, const T low, const T high) {
const uint64_t N = v.size();
for (uint64_t i = 0; i < N; ++i){
v[i] = random(low, high);
}
}
} // namespace numerics