41 lines
982 B
C++
41 lines
982 B
C++
#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: A is " +
|
|
std::to_string(A.rows()) + "x" + std::to_string(A.cols()) +
|
|
", B is " +
|
|
std::to_string(B.rows()) + "x" + std::to_string(B.cols())
|
|
);
|
|
}
|
|
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
|
|
|