43 lines
1.1 KiB
C++
43 lines
1.1 KiB
C++
#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
|
|
|