From b217876a9d1444f37d48d227ac75f722637d5dfb Mon Sep 17 00:00:00 2001 From: Luigi Ballabio Date: Wed, 2 Aug 2023 12:30:21 +0200 Subject: [PATCH] Some small cleanups: - avoid a few copies; - use initializer lists for readability; - use delegating constructors to reduce duplication --- .../yield/fittedbonddiscountcurve.cpp | 2 +- .../yield/fittedbonddiscountcurve.hpp | 7 +- .../yield/nonlinearfittingmethods.cpp | 95 +++++++------------ .../yield/nonlinearfittingmethods.hpp | 16 +--- 4 files changed, 43 insertions(+), 77 deletions(-) diff --git a/ql/termstructures/yield/fittedbonddiscountcurve.cpp b/ql/termstructures/yield/fittedbonddiscountcurve.cpp index fba963e602e..488d3528b31 100644 --- a/ql/termstructures/yield/fittedbonddiscountcurve.cpp +++ b/ql/termstructures/yield/fittedbonddiscountcurve.cpp @@ -126,7 +126,7 @@ namespace QuantLib { Constraint constraint) : constrainAtZero_(constrainAtZero), weights_(weights), l2_(std::move(l2)), calculateWeights_(weights.empty()), optimizationMethod_(std::move(optimizationMethod)), - minCutoffTime_(minCutoffTime), maxCutoffTime_(maxCutoffTime), constraint_(constraint) {} + minCutoffTime_(minCutoffTime), maxCutoffTime_(maxCutoffTime), constraint_(std::move(constraint)) {} void FittedBondDiscountCurve::FittingMethod::init() { // yield conventions diff --git a/ql/termstructures/yield/fittedbonddiscountcurve.hpp b/ql/termstructures/yield/fittedbonddiscountcurve.hpp index f3ea2b99319..d7ad89d403c 100644 --- a/ql/termstructures/yield/fittedbonddiscountcurve.hpp +++ b/ql/termstructures/yield/fittedbonddiscountcurve.hpp @@ -205,15 +205,14 @@ namespace QuantLib { //! return optimization method being used ext::shared_ptr optimizationMethod() const; //! return constraint of the solution being used - Constraint constraint() const; + const Constraint& constraint() const; //! open discountFunction to public DiscountFactor discount(const Array& x, Time t) const; protected: //! constructors FittingMethod(bool constrainAtZero = true, const Array& weights = Array(), - ext::shared_ptr optimizationMethod = - ext::shared_ptr(), + ext::shared_ptr optimizationMethod = {}, Array l2 = Array(), Real minCutoffTime = 0.0, Real maxCutoffTime = QL_MAX_REAL, @@ -329,7 +328,7 @@ namespace QuantLib { return optimizationMethod_; } - inline Constraint + inline const Constraint& FittedBondDiscountCurve::FittingMethod::constraint() const { return constraint_; } diff --git a/ql/termstructures/yield/nonlinearfittingmethods.cpp b/ql/termstructures/yield/nonlinearfittingmethods.cpp index 42f55010bab..70094ea962f 100644 --- a/ql/termstructures/yield/nonlinearfittingmethods.cpp +++ b/ql/termstructures/yield/nonlinearfittingmethods.cpp @@ -35,36 +35,33 @@ namespace QuantLib { const Size numCoeffs, const Real fixedKappa, Constraint constraint) - : FittedBondDiscountCurve::FittingMethod( - constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint), - numCoeffs_(numCoeffs), fixedKappa_(fixedKappa) - { - QL_REQUIRE(ExponentialSplinesFitting::size() > 0, "At least 1 unconstrained coefficient required"); + : FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, optimizationMethod, l2, + minCutoffTime, maxCutoffTime, std::move(constraint)), + numCoeffs_(numCoeffs), fixedKappa_(fixedKappa) { + QL_REQUIRE(ExponentialSplinesFitting::size() > 0, + "At least 1 unconstrained coefficient required"); } ExponentialSplinesFitting::ExponentialSplinesFitting(bool constrainAtZero, - const Array& weights, - const Array& l2, const Real minCutoffTime, const Real maxCutoffTime, + const Array& weights, + const Array& l2, + const Real minCutoffTime, + const Real maxCutoffTime, const Size numCoeffs, const Real fixedKappa, Constraint constraint) - : FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, ext::shared_ptr(), l2, - minCutoffTime, maxCutoffTime, constraint), - numCoeffs_(numCoeffs),fixedKappa_(fixedKappa) - { - QL_REQUIRE(ExponentialSplinesFitting::size() > 0, "At least 1 unconstrained coefficient required"); - } + : ExponentialSplinesFitting(constrainAtZero, weights, {}, l2, + minCutoffTime, maxCutoffTime, + numCoeffs, fixedKappa, std::move(constraint)) {} ExponentialSplinesFitting::ExponentialSplinesFitting(bool constrainAtZero, - const Size numCoeffs, - const Real fixedKappa, + const Size numCoeffs, + const Real fixedKappa, const Array& weights, Constraint constraint) - : FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, ext::shared_ptr(), Array(),0.0,QL_MAX_REAL, constraint), - numCoeffs_(numCoeffs), fixedKappa_(fixedKappa) - { - QL_REQUIRE(ExponentialSplinesFitting::size() > 0, "At least 1 unconstrained coefficient required"); - } + : ExponentialSplinesFitting(constrainAtZero, weights, {}, Array(), + 0.0, QL_MAX_REAL, + numCoeffs, fixedKappa, std::move(constraint)) {} std::unique_ptr ExponentialSplinesFitting::clone() const { @@ -113,16 +110,15 @@ namespace QuantLib { Real minCutoffTime, Real maxCutoffTime, Constraint constraint) - : FittedBondDiscountCurve::FittingMethod( - true, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint) {} + : FittedBondDiscountCurve::FittingMethod(true, weights, optimizationMethod, l2, + minCutoffTime, maxCutoffTime, std::move(constraint)) {} NelsonSiegelFitting::NelsonSiegelFitting(const Array& weights, const Array& l2, Real minCutoffTime, Real maxCutoffTime, Constraint constraint) - : FittedBondDiscountCurve::FittingMethod(true, weights, ext::shared_ptr(), l2, - minCutoffTime, maxCutoffTime, constraint) {} + : NelsonSiegelFitting(weights, {}, l2, minCutoffTime, maxCutoffTime, std::move(constraint)) {} std::unique_ptr NelsonSiegelFitting::clone() const { @@ -152,16 +148,15 @@ namespace QuantLib { const Real minCutoffTime, const Real maxCutoffTime, Constraint constraint) - : FittedBondDiscountCurve::FittingMethod( - true, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint) {} + : FittedBondDiscountCurve::FittingMethod(true, weights, optimizationMethod, l2, + minCutoffTime, maxCutoffTime, std::move(constraint)) {} SvenssonFitting::SvenssonFitting(const Array& weights, const Array& l2, const Real minCutoffTime, const Real maxCutoffTime, Constraint constraint) - : FittedBondDiscountCurve::FittingMethod(true, weights, ext::shared_ptr(), l2, - minCutoffTime, maxCutoffTime, constraint) {} + : SvenssonFitting(weights, {}, l2, minCutoffTime, maxCutoffTime, std::move(constraint)) {} std::unique_ptr SvenssonFitting::clone() const { @@ -197,8 +192,8 @@ namespace QuantLib { const Real minCutoffTime, const Real maxCutoffTime, Constraint constraint) - : FittedBondDiscountCurve::FittingMethod( - constrainAtZero, weights, optimizationMethod, l2, minCutoffTime, maxCutoffTime, constraint), + : FittedBondDiscountCurve::FittingMethod(constrainAtZero, weights, optimizationMethod, l2, + minCutoffTime, maxCutoffTime, std::move(constraint)), splines_(3, knots.size() - 5, knots) { QL_REQUIRE(knots.size() >= 8, @@ -221,35 +216,14 @@ namespace QuantLib { } CubicBSplinesFitting::CubicBSplinesFitting(const std::vector