46 lines
1.9 KiB
C++
46 lines
1.9 KiB
C++
/// @file
|
|
/// @brief Task3: implementation
|
|
|
|
#include "task3.hpp" // sample_to_csv
|
|
#include "iue-io/csv.hpp" // iue::io::savetxt
|
|
#include "task2.hpp" // range|sample|numint|numdiff
|
|
|
|
/// @todo Include standard library headers as needed
|
|
#include <functional> // std::function
|
|
#include <vector> // std::vector
|
|
|
|
/// @brief This function
|
|
/// - samples a one-dimensional scalar function (f) in a provided interval and resolution,
|
|
// - approximates its derivative (df) and antiderivative (F) numerically, and
|
|
/// - produces a csv-file holding the discrete values in this form:
|
|
/// - csv-column layout: x, f, F, df
|
|
/// @param filepath
|
|
/// @param del Delimiter
|
|
/// @param comment Character designating a line as a comment
|
|
/// @param func Callable with a signature compatible with f(double) -> double
|
|
/// @param start Start of the interval
|
|
/// @param end End of the interval
|
|
/// @param N Number of values; assumption: N >= 2
|
|
void sample_to_csv(std::filesystem::path filepath, char del, char comments, std::function<double(double)> func,
|
|
double start, double end, unsigned int N) {
|
|
// Create a sequence of equidistant values in the interval [start, end]
|
|
std::vector<double> x = range(start, end, N);
|
|
// Sample the function 'func' at the locations 'x'
|
|
std::vector<double> f = sample(x, func);
|
|
// Approximate the derivative of 'f' at the locations 'x'
|
|
std::vector<double> df = numdiff(x, f);
|
|
// Approximate the antiderivative of 'f' at the locations 'x'
|
|
std::vector<double> F = numint(x, f, 0.0);
|
|
// Create a matrix holding the discrete values of 'x', 'f', 'F', and 'df'
|
|
std::vector<std::vector<double>> data(N, std::vector<double>(4));
|
|
for (unsigned int i = 0; i < N; ++i) {
|
|
data[i][0] = x[i];
|
|
data[i][1] = f[i];
|
|
data[i][2] = F[i];
|
|
data[i][3] = df[i];
|
|
}
|
|
// Save the matrix to a csv-file
|
|
iue::io::savetxt(filepath, data, del);
|
|
|
|
}
|