@@ -316,22 +316,26 @@ template <typename T> |
316 | 316 | } |
317 | 317 |
|
318 | 318 | template <typename T> |
319 | | -[[nodiscard]] Line<T> linear_fit_medians_gate_slopes(const std::vector<numerical::Vector<2, T>>& points, const T gate) |
| 319 | +[[nodiscard]] std::vector<T> linear_fit_piecewise( |
| 320 | + const std::vector<numerical::Vector<2, T>>& points, |
| 321 | + const std::vector<T>& breakpoints) |
320 | 322 | { |
321 | 323 | static_assert(std::is_floating_point_v<T>); |
322 | 324 | namespace impl = implementation; |
323 | 325 |
|
324 | | - impl::check_point_count(points); |
| 326 | + impl::check_breakpoints(breakpoints); |
325 | 327 |
|
326 | | - const std::vector<T> slopes = impl::compute_slope_medians(points); |
| 328 | + std::vector<std::vector<T>> a = impl::least_squares_a(points, breakpoints); |
| 329 | + std::vector<T> b = impl::least_squares_b(points); |
327 | 330 |
|
328 | | - const std::vector<numerical::Vector<2, T>> filtered_points = impl::filter_points(points, slopes, gate); |
| 331 | + std::vector<T> s = numerical::qr_solve(a, b); |
| 332 | + ASSERT(s.size() == breakpoints.size() + 1); |
329 | 333 |
|
330 | | - return linear_fit_medians(filtered_points); |
| 334 | + return s; |
331 | 335 | } |
332 | 336 |
|
333 | 337 | template <typename T> |
334 | | -[[nodiscard]] Line<T> linear_fit_medians_gate_residuals( |
| 338 | +[[nodiscard]] std::vector<numerical::Vector<2, T>> filter_by_slopes( |
335 | 339 | const std::vector<numerical::Vector<2, T>>& points, |
336 | 340 | const T gate) |
337 | 341 | { |
@@ -340,36 +344,30 @@ template <typename T> |
340 | 344 |
|
341 | 345 | impl::check_point_count(points); |
342 | 346 |
|
343 | | - const Line l = linear_fit_medians(points); |
344 | | - |
345 | | - const std::vector<T> residuals = impl::residuals(points, l.intercept, l.slope); |
346 | | - |
347 | | - const std::vector<numerical::Vector<2, T>> filtered_points = impl::filter_points(points, residuals, gate); |
| 347 | + const std::vector<T> slopes = impl::compute_slope_medians(points); |
348 | 348 |
|
349 | | - return linear_fit_medians(filtered_points); |
| 349 | + return impl::filter_points(points, slopes, gate); |
350 | 350 | } |
351 | 351 |
|
352 | 352 | template <typename T> |
353 | | -[[nodiscard]] std::vector<T> linear_fit_piecewise( |
| 353 | +[[nodiscard]] std::vector<numerical::Vector<2, T>> filter_by_residuals( |
354 | 354 | const std::vector<numerical::Vector<2, T>>& points, |
355 | | - const std::vector<T>& breakpoints) |
| 355 | + const T gate) |
356 | 356 | { |
357 | 357 | static_assert(std::is_floating_point_v<T>); |
358 | 358 | namespace impl = implementation; |
359 | 359 |
|
360 | | - impl::check_breakpoints(breakpoints); |
| 360 | + impl::check_point_count(points); |
361 | 361 |
|
362 | | - std::vector<std::vector<T>> a = impl::least_squares_a(points, breakpoints); |
363 | | - std::vector<T> b = impl::least_squares_b(points); |
| 362 | + const Line l = linear_fit_medians(points); |
364 | 363 |
|
365 | | - std::vector<T> s = numerical::qr_solve(a, b); |
366 | | - ASSERT(s.size() == breakpoints.size() + 1); |
| 364 | + const std::vector<T> residuals = impl::residuals(points, l.intercept, l.slope); |
367 | 365 |
|
368 | | - return s; |
| 366 | + return impl::filter_points(points, residuals, gate); |
369 | 367 | } |
370 | 368 |
|
371 | 369 | template <typename T> |
372 | | -[[nodiscard]] std::vector<T> linear_fit_piecewise_gate( |
| 370 | +[[nodiscard]] std::vector<numerical::Vector<2, T>> filter_by_residuals( |
373 | 371 | const std::vector<numerical::Vector<2, T>>& points, |
374 | 372 | const std::vector<T>& breakpoints, |
375 | 373 | const T gate) |
@@ -379,27 +377,11 @@ template <typename T> |
379 | 377 |
|
380 | 378 | impl::check_breakpoints(breakpoints); |
381 | 379 |
|
382 | | - const std::vector<std::vector<numerical::Vector<2, T>>> segments = |
383 | | - impl::breakpoint_segments(points, breakpoints); |
384 | | - |
385 | | - std::vector<numerical::Vector<2, T>> filtered_points; |
386 | | - for (const std::vector<numerical::Vector<2, T>>& segment : segments) |
| 380 | + std::vector<numerical::Vector<2, T>> res; |
| 381 | + for (const std::vector<numerical::Vector<2, T>>& segment : impl::breakpoint_segments(points, breakpoints)) |
387 | 382 | { |
388 | | - impl::check_point_count(segment); |
389 | | - |
390 | | - const Line l = linear_fit_medians(segment); |
391 | | - |
392 | | - const std::vector<T> residuals = impl::residuals(segment, l.intercept, l.slope); |
393 | | - |
394 | | - filtered_points.insert_range(filtered_points.end(), impl::filter_points(segment, residuals, gate)); |
| 383 | + res.insert_range(res.end(), filter_by_residuals(segment, gate)); |
395 | 384 | } |
396 | | - |
397 | | - std::vector<std::vector<T>> a = impl::least_squares_a(filtered_points, breakpoints); |
398 | | - std::vector<T> b = impl::least_squares_b(filtered_points); |
399 | | - |
400 | | - std::vector<T> s = numerical::qr_solve(a, b); |
401 | | - ASSERT(s.size() == breakpoints.size() + 1); |
402 | | - |
403 | | - return s; |
| 385 | + return res; |
404 | 386 | } |
405 | 387 | } |