132 lines
3.2 KiB
C++
132 lines
3.2 KiB
C++
#ifndef _matadd_n_
|
|
#define _matadd_n_
|
|
|
|
#include "./utils/vector.h"
|
|
#include "./utils/matrix.h"
|
|
#include "./core/omp_config.h"
|
|
|
|
namespace numerics{
|
|
|
|
template <typename T>
|
|
void inplace_matadd_mat(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_matadd: dimension mismatch");
|
|
}
|
|
|
|
for (uint64_t i = 0; i < cols; ++i) {
|
|
for (uint64_t j = 0; j < rows; ++j) {
|
|
A(j, i) += B(j, i);
|
|
}
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
utils::Matrix<T> matadd_mat(const utils::Matrix<T>& A, const utils::Matrix<T>& B) {
|
|
|
|
const uint64_t rows = A.rows();
|
|
const uint64_t cols = A.cols();
|
|
|
|
utils::Matrix<T> C = A;
|
|
|
|
if (rows != B.rows() || cols != B.cols()) {
|
|
throw std::runtime_error("inplace_matadd: dimension mismatch");
|
|
}
|
|
|
|
inplace_matadd_mat(C, B);
|
|
return C;
|
|
}
|
|
|
|
template <typename T>
|
|
void inplace_matadd_colvec(utils::Matrix<T>& A, const utils::Vector<T>& x) {
|
|
|
|
const uint64_t rows = A.rows();
|
|
const uint64_t cols = A.cols();
|
|
|
|
if (rows != x.size()) {
|
|
throw std::runtime_error("inplace_matadd_colvec: dimension mismatch");
|
|
}
|
|
|
|
for (uint64_t i = 0; i < cols; ++i) {
|
|
for (uint64_t j = 0; j < rows; ++j) {
|
|
A(j, i) += x[j];
|
|
}
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
void inplace_matadd_rowvec(utils::Matrix<T>& A, const utils::Vector<T>& x) {
|
|
|
|
const uint64_t rows = A.rows();
|
|
const uint64_t cols = A.cols();
|
|
|
|
if (cols != x.size()) {
|
|
throw std::runtime_error("inplace_matadd_rowvec: dimension mismatch");
|
|
}
|
|
|
|
for (uint64_t i = 0; i < cols; ++i) {
|
|
for (uint64_t j = 0; j < rows; ++j) {
|
|
A(j, i) += x[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
utils::Matrix<T> matadd_colvec(const utils::Matrix<T>& A, const utils::Vector<T>& x) {
|
|
|
|
//const uint64_t rows = A.rows();
|
|
//const uint64_t cols = A.cols();
|
|
|
|
utils::Matrix<T> B = A;
|
|
|
|
inplace_matadd_colvec(B, x);
|
|
|
|
return B;
|
|
}
|
|
|
|
template <typename T>
|
|
utils::Matrix<T> matadd_rowvec(const utils::Matrix<T>& A, const utils::Vector<T>& x) {
|
|
|
|
//const uint64_t rows = A.rows();
|
|
//const uint64_t cols = A.cols();
|
|
|
|
utils::Matrix<T> B = A;
|
|
|
|
inplace_matadd_rowvec(B, x);
|
|
|
|
return B;
|
|
}
|
|
|
|
template <typename T>
|
|
utils::Matrix<T> matadd(const utils::Matrix<T>& A, const utils::Vector<T>& x, std::string method = "auto"){
|
|
|
|
const uint64_t rows = A.rows();
|
|
const uint64_t cols = A.cols();
|
|
const uint64_t N = x.size();
|
|
|
|
if (method=="auto"){
|
|
|
|
if (rows==cols){
|
|
throw std::runtime_error("matadd: too many options for dimensions");
|
|
} else if (rows == N){
|
|
return matadd_rowvec(A, x);
|
|
} else if (cols == N){
|
|
return matadd_colvec(A, x);
|
|
}else{
|
|
throw std::runtime_error("matadd: undefined fault - auto");
|
|
}
|
|
}else if(method=="row"){
|
|
return matadd_rowvec(A, x);
|
|
} else if (method=="col"){
|
|
return matadd_colvec(A, x);
|
|
}else{
|
|
throw std::runtime_error("matadd: undefined fault - defined method");
|
|
}
|
|
}
|
|
|
|
} // namespace numerics
|
|
|
|
#endif // _matadd_n_
|