Files
Flux/include/numerics/detail/add_serial.h
T
Bausager 9709949a5b
Sync public mirror / sync (push) Failing after 27s
Refactor progress
Next step is ramdom and equal. I also need to have a look at add and the serial naming of the functions.
2026-01-13 20:07:27 +01:00

86 lines
2.4 KiB
C++

#pragma once
#include <cstdint> //uint64_t
#include <stdexcept> // std::runtime_error
#include "../utils/vector.h"
#include "../utils/matrix.h"
namespace numerics::detail{
// ---------------- Scalar ----------------
template <typename T>
void inplace_add_scalar_serial(utils::Matrix<T>& A, const T c) {
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) += c;
}
}
}
template <typename T>
void inplace_add_scalar_serial(utils::Vector<T>& v, const T c) {
for (uint64_t i = 0; i < v.size(); ++i){
v[i] += c;
}
}
// ---------------- Elemenwise ----------------
template <typename T>
void inplace_add_elementwise_serial(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()) {
throw std::runtime_error("inplace_add_elementwise_serial: dimension mismatch");
}
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
A(i,j) += B(i,j);
}
}
}
template <typename T>
void inplace_add_elementwise_serial(utils::Vector<T>& v, const utils::Vector<T>& p) {
if (v.size() != p.size()) {
throw std::runtime_error("inplace_add_elementwise_serial: dimension mismatch");
}
for (uint64_t i = 0; i < v.size(); ++i){
v[i] += p[i];
}
}
// ---------------- Rowwise ----------------
template <typename T>
void inplace_add_rowwise_serial(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("inplace_add_rowwise_serial: dimension mismatch");
}
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
A(i,j) += v[j];
}
}
}
// ---------------- Colwise ----------------
template <typename T>
void inplace_add_colwise_serial(utils::Matrix<T>& A, const utils::Vector<T>& v) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
if (rows != v.size()) {
throw std::runtime_error("inplace_add_colwise_serial: dimension mismatch");
}
for (uint64_t i = 0; i < rows; ++i){
const T vi = v[i];
for (uint64_t j = 0; j < cols; ++j){
A(i,j) += vi;
}
}
}
} // namespace numerics