Theoretica
Mathematical Library
Loading...
Searching...
No Matches
iter_result.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_ITER_RESULT_H
7#define THEORETICA_ITER_RESULT_H
8
9#include <string>
10#include "./constants.h"
11#include "./error.h"
12
13
14namespace theoretica {
15
16
19 enum class ConvergenceStatus {
20
22 Success,
23
26
28 Stalled,
29
32
35 };
36
37
44 template<typename Type = real>
45 struct iter_result {
46
48 Type value;
49
52
54 unsigned int iterations = 0;
55
58
59
61 iter_result() : value(Type(nan())) {}
62
63
65 iter_result(const Type& val) : value(val) {
67 }
68
70 iter_result(const Type& value, unsigned int iterations)
72
74 }
75
82
83
87 value = Type(nan());
88 }
89
95
96
99 operator Type() const {
100 return value;
101 }
102
104 inline bool converged() const {
106 }
107
110 explicit operator bool() const {
111 return converged();
112 }
113
114
116 inline std::string status_string() const {
117
118 switch(status) {
120 return "Converged successfully";
122 return "Maximum iterations exceeded without converging to desired accuracy";
124 return "Algorithm stalled";
126 return "Invalid input provided";
128 return "Algorithm diverged";
129 default:
130 return "Unknown status";
131 }
132 }
133
134
135#ifndef THEORETICA_NO_PRINT
136
138 inline std::string to_string() const {
139
140 std::stringstream res;
141
142 res << "Value = " << value << "\n";
143 res << "Status: " << status_string() << "\n";
144 res << "Iterations = " << iterations << "\n";
145 res << "Residual = " << residual;
146
147 return res.str();
148 }
149
150
153 inline friend std::ostream& operator<<(std::ostream& out, const iter_result<Type>& obj) {
154 return out << obj.to_string();
155 }
156
157#endif
158
159 };
160
161}
162
163#endif
Mathematical constants and default algorithm parameters.
Error handling with errno and exceptions.
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:207
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:78
TH_CONSTEXPR real inf()
Get positive infinity in floating point representation.
Definition error.h:100
ConvergenceStatus
Status codes for iterative algorithm termination.
Definition iter_result.h:19
@ Stalled
No progress in iterations.
@ Success
Algorithm converged successfully.
@ Diverged
Algorithm diverged.
@ MaxIterations
Maximum iterations exceeded.
@ InvalidInput
Invalid input provided.
A structure returned by iterative algorithms containing the computed value, convergence information,...
Definition iter_result.h:45
iter_result(const Type &value, unsigned int iterations)
Construct with final result for reporting success.
Definition iter_result.h:70
friend std::ostream & operator<<(std::ostream &out, const iter_result< Type > &obj)
Stream the complex number in string representation to an output stream (std::ostream)
Definition iter_result.h:153
real residual
Final error or residual norm (exact meaning depends on the algorithm)
Definition iter_result.h:57
iter_result()
Construct with default values.
Definition iter_result.h:61
std::string status_string() const
Get a human-readable string description of the status of convergence of an iterative algorithm.
Definition iter_result.h:116
unsigned int iterations
Number of iterations performed.
Definition iter_result.h:54
iter_result(const Type &value, unsigned int iterations, real residual)
Construct with final result for reporting success.
Definition iter_result.h:77
std::string to_string() const
Convert the iter_result number to string representation.
Definition iter_result.h:138
iter_result(ConvergenceStatus status, unsigned int iterations, real residual)
Construct with convergence status for reporting failure.
Definition iter_result.h:91
iter_result(const Type &val)
Construct with final result for reporting success.
Definition iter_result.h:65
Type value
Best estimate of the result.
Definition iter_result.h:48
iter_result(ConvergenceStatus status, unsigned int iterations=0)
Construct with convergence status for reporting failure.
Definition iter_result.h:85
ConvergenceStatus status
Status code indicating reason for termination.
Definition iter_result.h:51
bool converged() const
Check if the algorithm converged successfully.
Definition iter_result.h:104