limes 3.1.0
Composable Calculus Expressions for C++20
Loading...
Searching...
No Matches
result.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <optional>
5#include <vector>
6#include "../concepts/concepts.hpp"
7
8namespace limes::algorithms {
9
10// Integration result with comprehensive metadata
11template<concepts::Field T>
13 using value_type = T;
14
15 T value_{};
16 T error_{};
17 std::size_t iterations_{};
18 std::size_t evaluations_{};
19 bool converged_{true};
20
21 // Optional additional information
22 std::optional<T> variance_{};
23 std::optional<std::vector<T>> intermediate_values_{};
24
25 constexpr integration_result() noexcept = default;
26
27 constexpr integration_result(T val, T err, std::size_t iters) noexcept
28 : value_{val}, error_{err}, iterations_{iters}, evaluations_{iters} {}
29
30 constexpr integration_result(T val, T err, std::size_t iters, std::size_t evals) noexcept
31 : value_{val}, error_{err}, iterations_{iters}, evaluations_{evals} {}
32
33 // Accessors
34 constexpr T value() const noexcept { return value_; }
35 constexpr T error() const noexcept { return error_; }
36 constexpr std::size_t iterations() const noexcept { return iterations_; }
37 constexpr std::size_t evaluations() const noexcept { return evaluations_; }
38 constexpr bool converged() const noexcept { return converged_; }
39
40 // Conversion operators
41 constexpr operator T() const noexcept { return value_; }
42 constexpr explicit operator bool() const noexcept { return converged_; }
43
44 // Relative error
45 constexpr T relative_error() const noexcept {
46 if (value_ == T{}) return error_;
47 return error_ / std::abs(value_);
48 }
49
50 // Combine results (for composite integration)
51 constexpr integration_result& operator+=(const integration_result& other) noexcept {
52 value_ += other.value_;
53 error_ += other.error_;
54 iterations_ += other.iterations_;
55 evaluations_ += other.evaluations_;
56 converged_ = converged_ && other.converged_;
57 return *this;
58 }
59
60 constexpr integration_result operator+(const integration_result& other) const noexcept {
61 integration_result result = *this;
62 result += other;
63 return result;
64 }
65
66 // Scale result
67 constexpr integration_result& operator*=(T scale) noexcept {
68 value_ *= scale;
69 error_ *= std::abs(scale);
70 return *this;
71 }
72
73 constexpr integration_result operator*(T scale) const noexcept {
74 integration_result result = *this;
75 result *= scale;
76 return result;
77 }
78
79 friend constexpr integration_result operator*(T scale, const integration_result& r) noexcept {
80 return r * scale;
81 }
82};
83
84// Deduction guide
85template<typename T>
87
88template<typename T>
89integration_result(T, T, std::size_t, std::size_t) -> integration_result<T>;
90
91} // namespace limes::algorithms
constexpr T error() const noexcept
Definition result.hpp:35
constexpr integration_result & operator+=(const integration_result &other) noexcept
Definition result.hpp:51
std::optional< std::vector< T > > intermediate_values_
Definition result.hpp:23
constexpr T relative_error() const noexcept
Definition result.hpp:45
constexpr integration_result(T val, T err, std::size_t iters, std::size_t evals) noexcept
Definition result.hpp:30
constexpr integration_result operator*(T scale) const noexcept
Definition result.hpp:73
constexpr integration_result operator+(const integration_result &other) const noexcept
Definition result.hpp:60
constexpr integration_result() noexcept=default
constexpr std::size_t iterations() const noexcept
Definition result.hpp:36
constexpr integration_result & operator*=(T scale) noexcept
Definition result.hpp:67
constexpr T value() const noexcept
Definition result.hpp:34
constexpr bool converged() const noexcept
Definition result.hpp:38
constexpr std::size_t evaluations() const noexcept
Definition result.hpp:37
friend constexpr integration_result operator*(T scale, const integration_result &r) noexcept
Definition result.hpp:79