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,73 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint> //uint64_t
|
||||
//#include <stdexcept> // std::runtime_error
|
||||
|
||||
#include "../utils/vector.h"
|
||||
#include "../utils/matrix.h"
|
||||
|
||||
namespace numerics::detail{
|
||||
|
||||
// ---------------- Matrix -> Scalar ----------------
|
||||
template <typename T>
|
||||
T min_serial(const utils::Matrix<T>& A) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
T min = A(0,0);
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (min > A(i,j)){
|
||||
min = A(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
// ---------------- Vector -> Scalar ----------------
|
||||
template <typename T>
|
||||
T min_serial(const utils::Vector<T>& v) {
|
||||
const uint64_t N = v.size();
|
||||
T min = v[0];
|
||||
for (uint64_t i = 0; i < N; ++i){
|
||||
if (min > v[i]){
|
||||
min = v[i];
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
// ---------------- Matrix -> Vector ----------------
|
||||
template <typename T>
|
||||
utils::Vector<T> min_rowwise_serial(const utils::Matrix<T>& A) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
utils::Vector<T> min(rows, T{0});
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
min[i] = A(i,0);
|
||||
for (uint64_t j = 1; j < cols; ++j){
|
||||
if (min[i] > A(i,j)){
|
||||
min[i] = A(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
utils::Vector<T> min_colwise_serial(const utils::Matrix<T>& A) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
utils::Vector<T> min(cols, T{0});
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
min[j] = A(0, j);
|
||||
for (uint64_t i = 1; i < rows; ++i){
|
||||
if (min[j] > A(i,j)){
|
||||
min[j] = A(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
} // namespace numerics
|
||||
|
||||
Reference in New Issue
Block a user