Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
Numerics.h
1// trax track library
2// AD 2019
3//
4// "Ein Drittel Heizoel, zwei Drittel Benzin."
5//
6// Casper
7//
8//
9// Copyright (c) 2025 Trend Redaktions- und Verlagsgesellschaft mbH
10// Copyright (c) 2019 Marc-Michael Horstmann
11// Use, modification and distribution are subject to the
12// Boost Software License, Version 1.0. (See accompanying file
13// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
14//
15// Permission is hereby granted to any person obtaining a copy of this software
16// and associated source code (the "Software"), to use, view, and study the
17// Software for personal or internal business purposes, subject to the following
18// conditions:
19//
20// 1. Redistribution, modification, sublicensing, or commercial use of the
21// Software is NOT permitted without prior written consent from the copyright
22// holder.
23//
24// 2. The Software is provided "AS IS", without warranty of any kind, express
25// or implied.
26//
27// 3. All copies of the Software must retain this license notice.
28//
29// For further information, please contact: horstmann.marc@trendverlag.de
30
31#pragma once
32
33#include <boost/version.hpp>
34
35//static_assert(BOOST_VERSION >= 107000, "Boost version too old");
36
37#if BOOST_VERSION < 107000
38# include <boost/math/tools/numerical_differentiation.hpp>
39#else
40# include <boost/math/differentiation/finite_difference.hpp>
41#endif // BOOST_VERSION < 107000
42
43
44namespace trax{
45
46template<class ResultType, class F, typename Valtype>
47inline ResultType finite_difference_derivative6( const F f, Valtype t )
48// Based on Boost:
49//
50// (C) Copyright Nick Thompson 2018.
51// Use, modification and distribution are subject to the
52// Boost Software License, Version 1.0. (See accompanying file
53// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
54{
55 using rtype = typename Valtype::real_type;
56 constexpr rtype eps = (std::numeric_limits<rtype>::epsilon)();
57 // Error bound ~eps^6/7
58 // Error: h^6f^(7)(x)/140 + 5|f(x)|eps/h
59 Valtype h{ std::pow( eps / rtype{168}, rtype{1} / rtype{7} ) };
60
61#if BOOST_VERSION < 107000
62 h = Valtype{ boost::math::tools::detail::make_xph_representable(t.Units(), h.Units()) };
63#else
64 h = Valtype{ boost::math::differentiation::detail::make_xph_representable(t.Units(), h.Units()) };
65#endif // BOOST_VERSION < 107000
66
67 const auto y1 = f(t + h) - f(t - h);
68 const auto y2 = f(t - 2 * h) - f(t + 2 * h);
69 const auto y3 = f(t + 3 * h) - f(t - 3 * h);
70
71 return (y3 + 9_1 * y2 + 45_1 * y1) / (60 * h);
72}
73
74template<class ResultType, class F, typename Valtype>
75inline ResultType finite_difference_derivative8( const F f, Valtype t )
76// Based on Boost:
77//
78// (C) Copyright Nick Thompson 2018.
79// Use, modification and distribution are subject to the
80// Boost Software License, Version 1.0. (See accompanying file
81// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
82{
83 using rtype = typename Valtype::real_type;
84
85 const rtype eps = (std::numeric_limits<rtype>::epsilon)();
86 // Error bound ~eps^8/9.
87 // In double precision, we only expect to lose two digits of precision while using this formula, at the cost of 8 function evaluations.
88 // Error: h^8|f^(9)(x)|/630 + 7|f(x)|eps/h assuming 7 unstabilized additions.
89 // Mathematica code to get the error:
90 // Series[(f[x+h]-f[x-h])*(4/5) + (1/5)*(f[x-2*h] - f[x+2*h]) + (4/105)*(f[x+3*h] - f[x-3*h]) + (1/280)*(f[x-4*h] - f[x+4*h]), {h, 0, 9}]
91 // If we used Kahan summation, we could get the max error down to h^8|f^(9)(x)|/630 + |f(x)|eps/h.
92 Valtype h{ std::pow( rtype{551.25f} * eps, rtype{1} / rtype{9} ) };
93
94#if BOOST_VERSION < 107000
95 h = Valtype{ boost::math::tools::detail::make_xph_representable(t.Units(), h.Units()) };
96#else
97 h = Valtype{ boost::math::differentiation::detail::make_xph_representable(t.Units(), h.Units()) };
98#endif // BOOST_VERSION < 107000
99
100 const auto y1 = f(t + h) - f(t - h);
101 const auto y2 = f(t - 2 * h) - f(t + 2 * h);
102 const auto y3 = f(t + 3 * h) - f(t - 3 * h);
103 const auto y4 = f(t - 4 * h) - f(t + 4 * h);
104
105 const auto tmp1 = 3 * y4 / 8 + 4 * y3;
106 const auto tmp2 = 21 * y2 + 84 * y1;
107
108 return (tmp1 + tmp2) / (105 * h);
109}
110
114template<class ContainerType,typename Valtype, typename IteratorToPositionConverterType >
116 ContainerType& data,
117 typename ContainerType::iterator start,
118 typename ContainerType::iterator end,
119 Valtype maxDeviation,
120 IteratorToPositionConverterType P ){
121
123 P(start),
124 spat::Normalize( P(end) - P(start) ).second };
125
126 Valtype maxDistance = Valtype{0};
127 typename ContainerType::iterator iterMax;
128 for( typename ContainerType::iterator iter = start + 1; iter < end; ++iter ){
129 spat::Vector<Valtype> O{ P(iter) - B.P };
130 O -= O * B.T * B.T;
131 const Valtype distance = O.Length();
132 if( maxDistance < distance ){
133 maxDistance = distance;
134 iterMax = iter;
135 }
136 }
137
138 if( maxDistance < maxDeviation )
139 data.erase( start+1, end );
140 else{
141 DouglasPeucker( data, iterMax, end, maxDeviation, P );
142 DouglasPeucker( data, start, iterMax, maxDeviation, P );
143 }
144}
145
146}
constexpr Real _1(One one) noexcept
Dimensionated Values conversion functions.
Definition DimensionedValues.h:1186
auto Normalize(const Frame< Valtype, ValtypeT > &f) noexcept -> std::pair< Vector< ValtypeT >, Frame< Valtype, decltype(ValtypeT{}/ValtypeT{})> >
Outer normalizing.
Definition Frame.h:1144
Namespace of all the trax track libraries classes and methods.
Definition Collection.h:17
void DouglasPeucker(ContainerType &data, typename ContainerType::iterator start, typename ContainerType::iterator end, Valtype maxDeviation, IteratorToPositionConverterType P)
Definition Numerics.h:115
Implements a Vector bundle.
Definition VectorBundle.h:42
Position< Valtype > P
Base space postion.
Definition VectorBundle.h:43
Vector< ValtypeT > T
Tangent vector or x-axis.
Definition VectorBundle.h:44
Implements a 3D - vector in cartesian coordinates.
Definition Vector.h:48
constexpr Valtype Length() const noexcept
Calculates the length of the vector.
Definition Vector.h:449