cpp/functions.cpp

51 lines
1.6 KiB
C++
Raw Normal View History

2024-08-16 17:55:27 +00:00
//
// Created by dominik on 8/14/24.
//#
#include <vector>
#include <chrono>
#include <iostream>
int nearest_index(const std::vector<double> &x_ref, const double x, int start=0) {
while (x > x_ref[start+1]) {
start++;
}
return start;
}
double lerp(const std::vector<double>& x_ref, const std::vector<double>& y_ref, const double x, const int i) {
const double x_left = x_ref[i];
const double y_left = y_ref[i];
const double x_right = x_ref[i+1];
const double y_right = y_ref[i+1];
const double dydx = (y_right - y_left) / ( x_right - x_left );
return y_left + dydx * (x - x_left);
}
std::chrono::time_point<std::chrono::system_clock> printSteps(
const std::chrono::time_point<std::chrono::system_clock> last_print_out,
const std::chrono::time_point<std::chrono::system_clock> start,
const int total,
const int steps
) {
const auto now = std::chrono::high_resolution_clock::now();
if (const std::chrono::duration<float> duration = now - last_print_out; duration.count() < 10.) {
return last_print_out;
}
std::chrono::duration<float> duration = now - start;
const auto passed = duration.count();
std::cout << steps << " of " << total << " steps: " << passed << "s passed; ~" << passed * static_cast<float>(total-steps) / static_cast<float>(steps) << "s remaining\n";
return now;
// time_t end_time = std::chrono::system_clock::to_time_t(end);
// std::cout << "End tau = " << tau_i << "s : " << ctime(&end_time);
// std::cout << "Duration: " << duration.count() << "s\n" << std::endl;
}