[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 28955d1 01/13: Refactor for flexibility
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 28955d1 01/13: Refactor for flexibility |
Date: |
Sun, 4 Jul 2021 19:04:43 -0400 (EDT) |
branch: master
commit 28955d1015733ea7ffdf3ce501a52263d094922f
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Refactor for flexibility
Factored out the rounding function, making it default to std::identity,
and the tolerance, making it an argument, so that decimal_root() can be
used with any tolerance and without any rounding.
Documentation changes follow in a separate commit.
---
zero.hpp | 36 ++++++++++++++++++++++++++++++++----
1 file changed, 32 insertions(+), 4 deletions(-)
diff --git a/zero.hpp b/zero.hpp
index 988412d..e027a4f 100644
--- a/zero.hpp
+++ b/zero.hpp
@@ -29,6 +29,7 @@
#include "round_to.hpp"
#include <cmath> // fabs(), pow()
+#include <functional> // function(), identity()
#include <iomanip> // setw()
#include <limits>
#include <ostream>
@@ -60,6 +61,11 @@ struct root_type
int n_iter {0};
};
+namespace detail
+{
+using RoundT = std::function<double(double)>;
+} // namespace detail
+
/// Return a zero z of a function f within input bounds [a,b].
///
/// Preconditions: bounds are distinct after rounding; and either
@@ -234,16 +240,15 @@ template<typename FunctionalType>
root_type decimal_root
(double bound0
,double bound1
+ ,double tolerance
,root_bias bias
- ,int decimals
,FunctionalType& f
+ ,detail::RoundT round_dec = std::identity()
,std::ostream& os_trace = null_stream()
)
{
constexpr double epsilon {std::numeric_limits<double>::epsilon()};
- round_to<double> const round_dec {decimals, r_to_nearest};
-
int n_iter {0};
interpolation_technique technique {interpolate_initialization};
@@ -275,7 +280,7 @@ root_type decimal_root
;
};
- double t = 0.5 * std::pow(10.0, -decimals);
+ double t = tolerance;
a = round_dec(bound0);
b = round_dec(bound1);
@@ -426,6 +431,29 @@ root_type decimal_root
}
}
+template<typename FunctionalType>
+root_type decimal_root
+ (double bound0
+ ,double bound1
+ ,root_bias bias
+ ,int decimals
+ ,FunctionalType& f
+ ,std::ostream& os_trace = null_stream()
+ )
+{
+ round_to<double> const round_dec {decimals, r_to_nearest};
+
+ return decimal_root
+ (bound0
+ ,bound1
+ ,0.5 * std::pow(10.0, -decimals)
+ ,bias
+ ,f
+ ,detail::RoundT(round_dec)
+ ,os_trace
+ );
+}
+
/// A C++ equivalent of Brent's algol60 original, for reference only.
template<typename FunctionalType>
- [lmi-commits] [lmi] master updated (a03c499 -> 55873f1), Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master bfd60ed 03/13: Rename primary root finder, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master aaadfe1 05/13: Translate the ALGOL scrolls more literally, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master e68be69 07/13: Test lmi_root() against reference implementation, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master 41d1896 08/13: Move some tests out of a hodgepodge, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master 0e29fb4 10/13: Augment a block of unit tests, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master bf65f24 12/13: Remove duplicative tests, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master 28955d1 01/13: Refactor for flexibility,
Greg Chicares <=
- [lmi-commits] [lmi] master 8a1404f 02/13: Describe the preceding commit, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master e6b40ee 04/13: Write root-finder arguments in a more natural order, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master d5bd712 06/13: Add an instrumented reference implementation, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master 9be9b1d 09/13: Refactor, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master a1a1901 11/13: Refactor, Greg Chicares, 2021/07/04
- [lmi-commits] [lmi] master 55873f1 13/13: Test an uncommon code path, Greg Chicares, 2021/07/04