Skip to content

Instantly share code, notes, and snippets.

@inxanedev
Created August 29, 2020 20:27
Show Gist options
  • Save inxanedev/09cdb09905ebb52e0cfa7723c9c2910c to your computer and use it in GitHub Desktop.
Save inxanedev/09cdb09905ebb52e0cfa7723c9c2910c to your computer and use it in GitHub Desktop.
numpy.linspace from Python made in C++ - returns a list of x amount of evenly spaced numbers within a range
#include <iostream>
#include <vector>
std::vector<double> linearSpace(double a, double b, int amount) {
std::vector<double> result;
double step = (std::max(a, b) - std::min(a, b)) / amount;
for (double i = a; i <= b; i += step) {
result.push_back(i);
}
return result;
}
int main() {
std::vector<double> space = linearSpace(0, 10, 10);
for (double& val : space) {
std::cout << val << std::endl;
}
std::cin.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment