From 5511aad5a8637e31db6636b2d48f7c335b9c0510 Mon Sep 17 00:00:00 2001 From: Ralf Konrad <42419984+ralfkonrad@users.noreply.github.com> Date: Fri, 4 Aug 2023 15:45:01 +0200 Subject: [PATCH] Pre-calculate the FixedRateCoupon::amount(), it isn't mutable --- ql/cashflows/fixedratecoupon.cpp | 26 ++++++++++++-------------- ql/cashflows/fixedratecoupon.hpp | 8 ++------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/ql/cashflows/fixedratecoupon.cpp b/ql/cashflows/fixedratecoupon.cpp index 0010f09c4e7..44737548a09 100644 --- a/ql/cashflows/fixedratecoupon.cpp +++ b/ql/cashflows/fixedratecoupon.cpp @@ -38,9 +38,14 @@ namespace QuantLib { const Date& refPeriodStart, const Date& refPeriodEnd, const Date& exCouponDate) - : Coupon(paymentDate, nominal, accrualStartDate, accrualEndDate, - refPeriodStart, refPeriodEnd, exCouponDate), - rate_(InterestRate(rate, dayCounter, Simple, Annual)) {} + : FixedRateCoupon(paymentDate, + nominal, + InterestRate(rate, dayCounter, Simple, Annual), + accrualStartDate, + accrualEndDate, + refPeriodStart, + refPeriodEnd, + exCouponDate) {} FixedRateCoupon::FixedRateCoupon(const Date& paymentDate, Real nominal, @@ -57,17 +62,10 @@ namespace QuantLib { refPeriodStart, refPeriodEnd, exCouponDate), - rate_(std::move(interestRate)) {} - - Real FixedRateCoupon::amount() const { - calculate(); - return amount_; - } - - void FixedRateCoupon::performCalculations() const { - amount_ = nominal() * (rate_.compoundFactor(accrualStartDate_, accrualEndDate_, - refPeriodStart_, refPeriodEnd_) - - 1.0); + rate_(std::move(interestRate)) { + amount_ = Coupon::nominal() * (rate_.compoundFactor(accrualStartDate_, accrualEndDate_, + refPeriodStart_, refPeriodEnd_) - + 1.0); } Real FixedRateCoupon::accruedAmount(const Date& d) const { diff --git a/ql/cashflows/fixedratecoupon.hpp b/ql/cashflows/fixedratecoupon.hpp index 17540e7b834..8ea8009cd5c 100644 --- a/ql/cashflows/fixedratecoupon.hpp +++ b/ql/cashflows/fixedratecoupon.hpp @@ -60,13 +60,9 @@ namespace QuantLib { const Date& refPeriodEnd = Date(), const Date& exCouponDate = Date()); //@} - //! \name LazyObject interface - //@{ - void performCalculations() const override; - //@} //! \name CashFlow interface //@{ - Real amount() const override; + Real amount() const override { return amount_; } //@} //! \name Coupon interface //@{ @@ -81,7 +77,7 @@ namespace QuantLib { //@} private: InterestRate rate_; - mutable Real amount_; + Real amount_; };