Created
August 29, 2020 20:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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