lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 7dd2680 14/14: Add and use a forward-summatio


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 7dd2680 14/14: Add and use a forward-summation function template
Date: Thu, 18 Feb 2021 12:03:42 -0500 (EST)

branch: master
commit 7dd2680044d48d794d1e68e087d0795ea70b2525
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Add and use a forward-summation function template
    
    Incidentally, this commit will make it simpler to
      s/partial_sum/inclusive_scan/
    once gcc is upgraded from version 8 for lmi production.
---
 commutation_functions_test.cpp | 13 ++++---------
 math_functions.hpp             |  9 +++++++++
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/commutation_functions_test.cpp b/commutation_functions_test.cpp
index aba09cd..3dfbfc6 100644
--- a/commutation_functions_test.cpp
+++ b/commutation_functions_test.cpp
@@ -35,7 +35,6 @@
 #include <functional>                   // bind(), ref()
 #include <iomanip>                      // setw() etc.
 #include <ios>                          // ios_base::fixed()
-#include <numeric>                      // partial_sum()
 #include <vector>
 
 namespace
@@ -79,8 +78,7 @@ void mete_reserve
 {
     double premium = (10.0 * ulcf.aDomega() + ulcf.kM()[0]) / ulcf.aN()[0];
     assign(reserve, premium * ulcf.aD() - ulcf.kC());
-    std::partial_sum(reserve.begin(), reserve.end(), reserve.begin());
-    reserve /= ulcf.EaD();
+    assign(reserve, fwd_sum(reserve) / ulcf.EaD());
 }
 
 /// Exactly reproduce Table 2 from Eckley's paper.
@@ -165,8 +163,7 @@ void TestEckleyTable2()
 
     std::vector<double> reserve(coi.size());
     reserve += premium[0] * CF.aD() - CF.kC();
-    std::partial_sum(reserve.begin(), reserve.end(), reserve.begin());
-    reserve /= CF.EaD();
+    assign(reserve, fwd_sum(reserve) / CF.EaD());
 
     {
     double tolerance = 0.0000005;
@@ -287,8 +284,7 @@ void TestEckleyTables3and4()
 
     std::vector<double> reserve(coi.size());
     reserve += premium[0] * CF.aD() - CF.kC();
-    std::partial_sum(reserve.begin(), reserve.end(), reserve.begin());
-    reserve /= CF.EaD();
+    assign(reserve, fwd_sum(reserve) / CF.EaD());
 
     double tolerance = 0.000005;
     double worst_discrepancy = 0.0;
@@ -638,8 +634,7 @@ void Test_1980_CSO_Male_ANB()
     double premium = (10.0 * ulcf.aDomega() + ulcf.kM()[0]) / ulcf.aN()[0];
     std::vector<double> reserve(q.size());
     assign(reserve, premium * ulcf.aD() - ulcf.kC());
-    std::partial_sum(reserve.begin(), reserve.end(), reserve.begin());
-    reserve /= ulcf.EaD();
+    assign(reserve, fwd_sum(reserve) / ulcf.EaD());
 
     double tolerance = 1.0e-13;
     double worst_discrepancy = 0.0;
diff --git a/math_functions.hpp b/math_functions.hpp
index 15b0370..6935035 100644
--- a/math_functions.hpp
+++ b/math_functions.hpp
@@ -35,6 +35,15 @@
 // TODO ?? Write functions here for other refactorable uses of
 // std::pow() throughout lmi, to facilitate reuse and unit testing.
 
+/// Forward partial summation.
+
+template<typename T>
+std::vector<T>& fwd_sum(std::vector<T>& v)
+{
+    std::partial_sum(v.begin(), v.end(), v.begin());
+    return v;
+}
+
 /// Backward partial summation.
 
 template<typename T>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]