cpp/utils/ranges.cpp

60 lines
1.3 KiB
C++
Raw Permalink Normal View History

2024-08-16 17:55:27 +00:00
//
// Created by dominik on 8/14/24.
//
#include <vector>
#include <algorithm>
#include <cmath>
#include "ranges.h"
std::vector<double> arange(const int size, const double spacing=1.) {
std::vector<double> out(size);
std::generate(out.begin(), out.end(), [n = 0, spacing]() mutable { return n++ * spacing; });
return out;
}
std::vector<double> linspace(const double start, const double stop, const int steps) {
std::vector<double> range;
if (steps == 0) {
return range;
}
if (steps == 1) {
range.push_back(start);
return range;
}
const double stepsize = (stop-start) / (steps-1);
for (int i=0; i<steps; i++) {
range.push_back(start + stepsize * i);
}
return range;
}
std::vector<double> logspace(const double start, const double stop, const int steps) {
std::vector<double> range;
if (steps == 0) {
return range;
}
if (steps == 1) {
range.push_back(start);
return range;
}
2024-08-18 11:21:27 +00:00
const double logstart = std::log10(start);
const double logstop = std::log10(stop);
2024-08-16 17:55:27 +00:00
const double stepsize = (logstop-logstart) / (steps-1);
for (int i=0; i<steps; i++) {
range.push_back(pow(10, logstart + stepsize * i));
}
return range;
}