#ifndef _matvec_n_ #define _matvec_n_ #include "./utils/matrix.h" namespace numerics{ // y = A * x, where A is (m×n) and x is length n and y is length m template utils::Vector matvec(const utils::Matrix& A, const utils::Vector& x) { if (A.cols() != x.size()) { throw std::runtime_error("matvec: dimension mismatch"); } const uint64_t m = A.rows(); const uint64_t n = A.cols(); utils::Vector y(m, T{0}); for (uint64_t i = 0; i < m; ++i) { T acc = T{0}; for (uint64_t j = 0; j < n; ++j) { acc += A(i, j) * x[j]; } y[i] = acc; } return y; } // y = x * A, where x is length m and A is (m×n) -> y is length n template utils::Vector vecmat(const utils::Vector& x, const utils::Matrix& A) { if (x.size() != A.rows()) { throw std::runtime_error("vecmat: dimension mismatch"); } const uint64_t m = A.rows(); const uint64_t n = A.cols(); utils::Vector y(n, T{0}); for (uint64_t j = 0; j < n; ++j) { T acc = T{0}; for (uint64_t i = 0; i < m; ++i) { acc += x[i] * A(i, j); } y[j] = acc; } return y; } } // namespace numerics #endif // _matvec_n_