Next step is ramdom and equal. I also need to have a look at add and the serial naming of the functions.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#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_mul_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_mul_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_mul_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_mul_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_mul_elementwise_serial(utils::Vector<T>& v, const utils::Vector<T>& p) {
|
||||
if (v.size() != p.size()) {
|
||||
throw std::runtime_error("inplace_mul_elementwise_serial: dimension mismatch");
|
||||
}
|
||||
for (uint64_t i = 0; i < v.size(); ++i){
|
||||
v[i] *= p[i];
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- Rowwise ----------------
|
||||
template <typename T>
|
||||
void inplace_mul_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_mul_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_mul_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_mul_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
|
||||
|
||||
Reference in New Issue
Block a user