libqe 0.2.5
QE C++ library
Loading...
Searching...
No Matches
trajectory_distance.hpp
Go to the documentation of this file.
1
4#ifndef LIBQE_TRAJECTORY_DISTANCE_HPP
5#define LIBQE_TRAJECTORY_DISTANCE_HPP
6
7#include <armadillo>
8#include <vector>
9#include <cmath>
10#include <algorithm>
11#include "trajectory.hpp"
12#include "linalg_fallback.hpp"
13
14namespace qe {
15
17 arma::vec nodes;
18 arma::vec weights;
19};
20
23 static const GaussLegendreRule rule = []() {
24 int N = 32;
25 arma::mat J(N, N, arma::fill::zeros);
26 for (int k = 1; k < N; ++k) {
27 double b = static_cast<double>(k) / std::sqrt(4.0 * k * k - 1.0);
28 J(k, k - 1) = b;
29 J(k - 1, k) = b;
30 }
31 arma::vec evals;
32 arma::mat evecs;
33 qe::linalg::eig_sym(evals, evecs, J); // LAPACK-free in WASM builds
34 arma::vec w = 2.0 * arma::square(evecs.row(0).t());
35 return GaussLegendreRule{evals, w};
36 }();
37 return rule;
38}
39
55 const arma::vec& coeffs_ax,
56 const arma::vec& coeffs_ay,
57 const arma::vec& coeffs_bx,
58 const arma::vec& coeffs_by,
59 double t_start = 0.0,
60 double t_end = 1.0
61) {
62 if (t_end <= t_start) return 0.0;
63
64 const auto& rule = get_gl_rule_32();
65 double mid = 0.5 * (t_start + t_end);
66 double half_len = 0.5 * (t_end - t_start);
67 double total = 0.0;
68
69 for (size_t i = 0; i < rule.nodes.n_elem; ++i) {
70 double t_eval = mid + half_len * rule.nodes[i];
71 double xa = eval_poly_scalar(coeffs_ax, t_eval);
72 double ya = eval_poly_scalar(coeffs_ay, t_eval);
73 double xb = eval_poly_scalar(coeffs_bx, t_eval);
74 double yb = eval_poly_scalar(coeffs_by, t_eval);
75 double dx = xa - xb;
76 double dy = ya - yb;
77 double dist = std::sqrt(dx * dx + dy * dy);
78
79 total += rule.weights[i] * dist;
80 }
81
82 return half_len * total;
83}
84
87 const arma::vec& coeffs_fol_x,
88 const arma::vec& coeffs_fol_y,
89 const arma::vec& coeffs_ldr_x,
90 const arma::vec& coeffs_ldr_y,
91 double lag = 0.0
92) {
93 if (lag < 0.0 || lag >= 0.999) return 0.0;
94 double t_start = lag;
95 double t_end = 1.0;
96 const auto& rule = get_gl_rule_32();
97 double mid = 0.5 * (t_start + t_end);
98 double half_len = 0.5 * (t_end - t_start);
99 double total = 0.0;
100
101 for (size_t i = 0; i < rule.nodes.n_elem; ++i) {
102 double t_fol = mid + half_len * rule.nodes[i];
103 double t_ldr = t_fol - lag;
104
105 double x_fol = eval_poly_scalar(coeffs_fol_x, t_fol);
106 double y_fol = eval_poly_scalar(coeffs_fol_y, t_fol);
107 double x_ldr = eval_poly_scalar(coeffs_ldr_x, t_ldr);
108 double y_ldr = eval_poly_scalar(coeffs_ldr_y, t_ldr);
109
110 double dx = x_fol - x_ldr;
111 double dy = y_fol - y_ldr;
112 double dist = std::sqrt(dx * dx + dy * dy);
113
114 total += rule.weights[i] * dist;
115 }
116
117 double integral = half_len * total;
118 double interval = 1.0 - lag;
119 return (interval > 1e-6) ? (integral / interval) : 0.0;
120}
121
124 const std::vector<arma::vec>& all_coeffs_x,
125 const std::vector<arma::vec>& all_coeffs_y
126) {
127 size_t K = all_coeffs_x.size();
128 arma::mat D(K, K, arma::fill::zeros);
129
130 for (size_t i = 0; i < K; ++i) {
131 for (size_t j = i + 1; j < K; ++j) {
132 double dist = integrated_curve_distance(
133 all_coeffs_x[i], all_coeffs_y[i],
134 all_coeffs_x[j], all_coeffs_y[j]
135 );
136 D(i, j) = dist;
137 D(j, i) = dist;
138 }
139 }
140
141 return D;
142}
143
144} // namespace qe
145
146#endif // LIBQE_TRAJECTORY_DISTANCE_HPP
Thin LAPACK-free implementations of SVD, QR, and symmetric eigendecomposition for use in WASM / no-LA...
void eig_sym(arma::vec &eigval, arma::mat &eigvec, const arma::mat &S)
Eigendecomposition of a real symmetric matrix.
Definition linalg_fallback.hpp:129
Definition adjacency.hpp:16
double lagged_curve_distance(const arma::vec &coeffs_fol_x, const arma::vec &coeffs_fol_y, const arma::vec &coeffs_ldr_x, const arma::vec &coeffs_ldr_y, double lag=0.0)
Compute lagged curve distance: 1/(1-lag) * integral_{lag}^1 ||F_follower(t) - F_leader(t - lag)|| dt.
Definition trajectory_distance.hpp:86
double integrated_curve_distance(const arma::vec &coeffs_ax, const arma::vec &coeffs_ay, const arma::vec &coeffs_bx, const arma::vec &coeffs_by, double t_start=0.0, double t_end=1.0)
Compute integrated Euclidean distance between two 2D parametric polynomial curves.
Definition trajectory_distance.hpp:54
const GaussLegendreRule & get_gl_rule_32()
Obtain exact 32-point Gauss-Legendre quadrature nodes and weights on [-1, 1] via Golub-Welsch.
Definition trajectory_distance.hpp:22
arma::mat pairwise_trajectory_distance_matrix(const std::vector< arma::vec > &all_coeffs_x, const std::vector< arma::vec > &all_coeffs_y)
Compute pairwise integrated distance matrix across K polynomial trajectory curves.
Definition trajectory_distance.hpp:123
double eval_poly_scalar(const arma::vec &coeffs, double t)
Evaluate a 1D polynomial sum(c_k * t^k) at a single time t.
Definition trajectory.hpp:221
Definition trajectory_distance.hpp:16
arma::vec weights
Definition trajectory_distance.hpp:18
arma::vec nodes
Definition trajectory_distance.hpp:17
Parametric trajectory curve fitting, LOOCV-2D degree selection, and differential geometry (velocity,...