Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
UnitsHelper.h
1// trax track library
2// AD 2019
3//
4// "the resolution of all the fruitless searches"
5//
6// Peter Gabriel
7//
8// Copyright (c) 2025 Trend Redaktions- und Verlagsgesellschaft mbH
9// Copyright (c) 2019 Marc-Michael Horstmann
10//
11// Permission is hereby granted to any person obtaining a copy of this software
12// and associated source code (the "Software"), to use, view, and study the
13// Software for personal or internal business purposes, subject to the following
14// conditions:
15//
16// 1. Redistribution, modification, sublicensing, or commercial use of the
17// Software is NOT permitted without prior written consent from the copyright
18// holder.
19//
20// 2. The Software is provided "AS IS", without warranty of any kind, express
21// or implied.
22//
23// 3. All copies of the Software must retain this license notice.
24//
25// For further information, please contact: horstmann.marc@trendverlag.de
26
27#pragma once
28
29#include "Units.h"
30#include "common/Interval.h"
31
32//#define BOOST_MATH_INSTRUMENT
33#include <boost/math/tools/roots.hpp>
34
35namespace common{
36 template<typename> struct Interval;
37}
38
39namespace trax{
40
43 template<class F, typename DomainType/*, typename CoDomainType*/, typename Tol>
44 common::Interval<DomainType> bracket_and_solve_root(F f, const DomainType& guess, const One& factor, bool rising, Tol tol, boost::uintmax_t& max_iter){
45 const auto function = [&f]( Real s ) -> Real{
46 const auto result = f( DomainType{s} );
47 return result.Units();
48 };
49
50 const std::pair<Real,Real> bracket = boost::math::tools::bracket_and_solve_root(
51 function,
52 guess.Units(),
53 factor.Units(),
54 rising,
55 tol,
56 max_iter );
57
58 return { DomainType{bracket.first}, DomainType{bracket.second} };
59 }
60
61 template<class F, typename DomainType/*, typename CoDomainType*/>
62 DomainType newton_raphson_iterate( F f, DomainType guess, common::Interval<DomainType> limits, int digits, boost::uintmax_t& max_iter ){
63 const auto function = [&f]( Real s ) -> std::pair<Real,Real>{
64 const auto result = f( DomainType{s} );
65 return { result.first.Units(), result.second.Units() };
66 };
67
68 return DomainType{ boost::math::tools::newton_raphson_iterate(
69 function,
70 guess.Units(),
71 limits.Near().Units(),
72 limits.Far().Units(),
73 digits,
74 max_iter ) };
75 }
77
78}
Namespace of common utility classes and methods.
Definition Helpers.h:43
Value< Dimension< 0, 0, 0 > > One
Dimensionless value.
Definition DimensionedValues.h:319
float Real
Underlying floating point type to be used with the dim library.
Definition DimensionedValues.h:190
Namespace of all the trax track libraries classes and methods.
Definition Collection.h:17
An interval describes the area between two numbers. It is understood to contain the near one and exlu...
Definition Interval.h:42
constexpr Valtype Near() const noexcept
Definition Interval.h:381
constexpr Valtype Far() const noexcept
Definition Interval.h:386