Theoretica
Scientific Computing
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
49
52
54 unsigned int iterations = 0;
55
58
59
64
65
67 iter_result(const Type& val) : value(val) {
69 }
70
77
84
85
91
97
101
102
105 operator Type() const {
106 return value;
107 }
108
110 inline bool converged() const {
112 }
113
116 explicit operator bool() const {
117 return converged();
118 }
119
123 inline Type safe() const {
124
125 if(!converged()) {
126 throw math_exception(
127 MathError::NoConvergence, "iter_result::safe()", __FILE__, __LINE__, false
128 );
129 }
130
131 return value;
132 }
133
134
136 inline std::string status_string() const {
137
138 switch(status) {
140 return "Converged successfully";
142 return "Maximum iterations exceeded without converging to desired accuracy";
144 return "Algorithm stalled";
146 return "Invalid input provided";
148 return "Algorithm diverged";
149 default:
150 return "Unknown status";
151 }
152 }
153
154
155#ifndef THEORETICA_NO_PRINT
156
158 inline std::string to_string() const {
159
160 std::stringstream res;
161
162 res << "Value = " << value << "\n";
163 res << "Status: " << status_string() << "\n";
164 res << "Iterations = " << iterations << "\n";
165 res << "Residual = " << residual;
166
167 return res.str();
168 }
169
170
173 inline friend std::ostream& operator<<(std::ostream& out, const iter_result<Type>& obj) {
174 return out << obj.to_string();
175 }
176
177#endif
178
179 };
180
181}
182
183#endif
A class for representing mathematical errors.
Definition error.h:109
Mathematical constants and default algorithm parameters.
Error handling for IO operations.
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
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
@ NoConvergence
Algorithm did not converge.
TH_CONSTEXPR real inf()
Get positive infinity in floating point representation.
Definition error.h:96
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:72
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:173
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:136
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:79
Type safe() const
Returns the computed value in a safe way, checking for convergence and throwing an exception if the a...
Definition iter_result.h:123
std::string to_string() const
Convert the iter_result number to string representation.
Definition iter_result.h:158
iter_result(ConvergenceStatus status, unsigned int iterations, real residual)
Construct with convergence status for reporting failure.
Definition iter_result.h:93
iter_result(const Type &value, ConvergenceStatus status, unsigned int iterations, real residual)
Construct with all fields specified.
Definition iter_result.h:99
iter_result(const Type &val)
Construct with final result for reporting success.
Definition iter_result.h:67
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:87
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:110