136 lines
2.7 KiB
C++
136 lines
2.7 KiB
C++
#ifndef _vector_n_
|
|
#define _vector_n_
|
|
|
|
#include "iostream"
|
|
#include <vector>
|
|
#include <random>
|
|
|
|
|
|
namespace utils{
|
|
//#######################################
|
|
//# VECTOR TYPE #
|
|
//#######################################
|
|
template<typename T>
|
|
struct Vector{
|
|
|
|
using value_type = T;
|
|
std::vector<T> v;
|
|
|
|
Vector() = default;
|
|
|
|
Vector(uint64_t size, T value = T()) {
|
|
v.resize(size, value);
|
|
}
|
|
|
|
|
|
void clear(){v.clear();}
|
|
void push_back(const T& val) { v.push_back(val); }
|
|
T& operator[](uint64_t idx) { return v[idx]; }
|
|
const T& operator[](uint64_t idx) const { return v[idx]; }
|
|
uint64_t size() const { return v.size(); }
|
|
|
|
void fill(const uint64_t size, const T num){
|
|
v.clear();
|
|
for (uint64_t i = 0; i < size; i++)
|
|
{
|
|
v.push_back(num);
|
|
}
|
|
}
|
|
|
|
void fill_RNG(const uint64_t size, const double min, const double max){
|
|
v.clear();
|
|
|
|
std::mt19937_64 rng{};
|
|
rng.seed( std::random_device{}());
|
|
|
|
for (uint64_t i = 0; i < size; i++){
|
|
|
|
v.push_back(std::uniform_real_distribution<>{min, max}(rng));
|
|
}
|
|
}
|
|
|
|
void disturbance(const double min, const double max){
|
|
|
|
uint64_t size = v.size();
|
|
|
|
std::mt19937_64 rng{};
|
|
rng.seed(std::random_device{}());
|
|
|
|
for (uint64_t i = 0; i < size; i++){
|
|
v[i] *= std::uniform_real_distribution<>{min, max}(rng);
|
|
}
|
|
}
|
|
|
|
double max(){
|
|
double max_value = v[0];
|
|
|
|
for (uint64_t i = 1; i < v.size(); i++){
|
|
max_value = MAX(max_value, v[i]);
|
|
}
|
|
return max_value;
|
|
}
|
|
|
|
double min(){
|
|
double min_value = v[0];
|
|
|
|
for (uint64_t i = 1; i < v.size(); i++){
|
|
min_value = MIN(min_value, v[i]);
|
|
}
|
|
return min_value;
|
|
}
|
|
|
|
double sum() const {
|
|
double sum_value = 0;
|
|
|
|
for (uint64_t i = 0; i < v.size(); i++){
|
|
sum_value += v[i];
|
|
}
|
|
return sum_value;
|
|
}
|
|
|
|
double mean(){
|
|
double mean_value = sum()/v.size();
|
|
return mean_value;
|
|
}
|
|
|
|
inline friend std::ostream& operator << (std::ostream& out, const Vector& vec){
|
|
out << "[";
|
|
for (uint64_t i = 0; i < vec.v.size(); i++){
|
|
out << vec.v[i];
|
|
if (i != vec.v.size() - 1) {
|
|
out << ", ";
|
|
}
|
|
}
|
|
out << "]";
|
|
return out;
|
|
}
|
|
|
|
void print() const{
|
|
std::cout << *this << std::endl;
|
|
}
|
|
|
|
void linspace(const T start, const T stop, const T num){
|
|
v.clear();
|
|
|
|
if (num > 1){
|
|
|
|
double delta = (stop - start)/(num - 1);
|
|
|
|
for (uint64_t i = 0; i < num; i++){
|
|
v.push_back(static_cast<T>(start + delta * i));
|
|
}
|
|
//v.push_back(static_cast<T>(stop));
|
|
}
|
|
|
|
|
|
}
|
|
};
|
|
|
|
typedef Vector<int> Vi;
|
|
typedef Vector<float> Vf;
|
|
typedef Vector<double> Vd;
|
|
|
|
|
|
} // namespace utils
|
|
|
|
#endif // _numerics_n_
|