-
Notifications
You must be signed in to change notification settings - Fork 0
/
linspace.cpp
69 lines (58 loc) · 1.77 KB
/
linspace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <Eigen/Dense>
#include <iostream>
using Eigen::VectorXd;
/*
// Taken from https://stackoverflow.com/questions/27028226/python-linspace-in-c
template<typename T>
std::vector<double> linspace(T start_in, T end_in, int num_in)
{
std::vector<double> linspaced;
double start = static_cast<double>(start_in);
double end = static_cast<double>(end_in);
double num = static_cast<double>(num_in);
if (num == 0) { return linspaced; }
if (num == 1)
{
linspaced.push_back(start);
return linspaced;
}
double delta = (end - start) / (num - 1);
for(int i=0; i < num-1; ++i)
{
linspaced.push_back(start + delta * i);
}
linspaced.push_back(end); // I want to ensure that start and end
// are exactly the same as the input
return linspaced;
}
// -----
*/
// My linspace function
VectorXd linspace(const long double start_in, const long double end_in, const long num_in)
{
if (num_in == 0) {
std::cout << " num_in in linspace is 0. Cannot create a linspace vector. Returning -1." << std::endl;
VectorXd linspaced(1);
linspaced[0]=-1;
return linspaced;
}
VectorXd linspaced(num_in);
const long double delta = (end_in - start_in) / (num_in - 1);
for(long i=0 ; i< num_in ; i++){
linspaced[i]=start_in + delta*i;
}
/*std::cout << "start_in =" << start_in << std::endl;
std::cout << "end_in =" << end_in << std::endl;
std::cout << "num_in =" << num_in << std::endl;
std::cout << "delta =" << delta << std::endl;
std::cout << "linspaced.size() =" << linspaced.size() << std::endl;
for(long i=0; i<linspaced.size(); i++)
{
std::cout << linspaced[i] << std::endl;
}
std::cout << "linspace needs a thorough test" << std::endl;
std::cout << "exiting now"<< std::endl;
exit(EXIT_SUCCESS);
*/
return linspaced;
}