lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 3edb81f 12/14: Add and use a backward-summati


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

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

    Add and use a backward-summation function template
---
 gpt_commutation_functions.cpp |  9 ++++-----
 ihs_irc7702.cpp               | 38 ++++++++------------------------------
 math_functions.hpp            | 10 ++++++++++
 3 files changed, 22 insertions(+), 35 deletions(-)

diff --git a/gpt_commutation_functions.cpp b/gpt_commutation_functions.cpp
index 5e1ac5c..54203e6 100644
--- a/gpt_commutation_functions.cpp
+++ b/gpt_commutation_functions.cpp
@@ -26,11 +26,11 @@
 #include "assert_lmi.hpp"
 #include "commutation_functions.hpp"
 #include "et_vector.hpp"
+#include "math_functions.hpp"           // back_sum()
 #include "miscellany.hpp"               // minmax
 #include "ssize_lmi.hpp"
 
 #include <algorithm>                    // min_element()
-#include <numeric>                      // partial_sum()
 #include <stdexcept>
 
 namespace
@@ -45,11 +45,10 @@ namespace
 /// exactly that, but the code is unpublished.
 
 template<typename T, typename E>
-std::vector<T>& back_sum(std::vector<T>& vt, E e)
+std::vector<T>& back_sum(std::vector<T>& v, E e)
 {
-    assign(vt, e);
-    std::partial_sum(vt.rbegin(), vt.rend(), vt.rbegin());
-    return vt;
+    assign(v, e);
+    return ::back_sum(v);
 }
 } // Unnamed namespace.
 
diff --git a/ihs_irc7702.cpp b/ihs_irc7702.cpp
index 8e880e0..f883f14 100644
--- a/ihs_irc7702.cpp
+++ b/ihs_irc7702.cpp
@@ -28,11 +28,11 @@
 #include "commutation_functions.hpp"
 #include "et_vector.hpp"
 #include "materially_equal.hpp"
+#include "math_functions.hpp"           // back_sum()
 #include "ssize_lmi.hpp"
 #include "value_cast.hpp"
 
-#include <algorithm>                    // max(), min(), reverse()
-#include <numeric>                      // partial_sum()
+#include <algorithm>                    // max(), min()
 #include <string>
 
 // TAXATION !! Update this block comment, or simply delete it. The
@@ -517,7 +517,6 @@ void Irc7702::InitCorridor()
 ///
 /// TAXATION !! Eliminate aliasing references.
 /// TAXATION !! Rename '[46]Pct' to 'g[ls]p'.
-/// TAXATION !! Write a utility function for rotate-partial_sum_rotate.
 /// TAXATION !! Add unit tests.
 
 void Irc7702::InitPvVectors(EIOBasis const& a_EIOBasis)
@@ -543,40 +542,23 @@ void Irc7702::InitPvVectors(EIOBasis const& a_EIOBasis)
     std::vector<double>& chg_pol = PvChgPol[a_EIOBasis];
     chg_pol.resize(Length);
     chg_pol += AnnChgPol * comm_fns.aD() + MlyChgPol * comm_fns.kD();
-
-    // ET !! This is just APL written verbosely in a funny C++ syntax.
-    // Perhaps we could hope for an expression-template library to do this:
-    //   chg_pol = chg_pol.reverse().partial_sum().reverse();
-    // but that would require a special type: 'chg_pol' couldn't be a
-    // std::vector anymore.
-    std::reverse(chg_pol.begin(), chg_pol.end());
-    std::partial_sum(chg_pol.begin(), chg_pol.end(), chg_pol.begin());
-    std::reverse(chg_pol.begin(), chg_pol.end());
+    chg_pol = back_sum(chg_pol);
 
     // Present value of charges per $1 specified amount
 
-    // APL: chg_sa gets rotate plus scan rotate MlyChgSpecAmt times kD
     std::vector<double>& chg_sa = PvChgSpecAmt[a_EIOBasis];
     chg_sa.resize(Length);
     chg_sa += MlyChgSpecAmt * comm_fns.kD();
-    std::reverse(chg_sa.begin(), chg_sa.end());
-    std::partial_sum(chg_sa.begin(), chg_sa.end(), chg_sa.begin());
-    std::reverse(chg_sa.begin(), chg_sa.end());
+    chg_sa = back_sum(chg_sa);
 
-    // APL: chg_add gets rotate plus scan rotate MlyChgADD times kD
     std::vector<double>& chg_add = PvChgADD[a_EIOBasis];
     chg_add.resize(Length);
     chg_add += MlyChgADD * comm_fns.kD();
-    std::reverse(chg_add.begin(), chg_add.end());
-    std::partial_sum(chg_add.begin(), chg_add.end(), chg_add.begin());
-    std::reverse(chg_add.begin(), chg_add.end());
+    chg_add = back_sum(chg_add);
 
-    // APL: chg_mort gets rotate plus scan rotate kC
     std::vector<double>& chg_mort = PvChgMort[a_EIOBasis];
     chg_mort = comm_fns.kC();
-    std::reverse(chg_mort.begin(), chg_mort.end());
-    std::partial_sum(chg_mort.begin(), chg_mort.end(), chg_mort.begin());
-    std::reverse(chg_mort.begin(), chg_mort.end());
+    chg_mort = back_sum(chg_mort);
 
     // Present value of 1 - target premium load
 
@@ -586,9 +568,7 @@ void Irc7702::InitPvVectors(EIOBasis const& a_EIOBasis)
 
     std::vector<double>& npf_lvl_tgt = PvNpfLvlTgt[a_EIOBasis];
     npf_lvl_tgt = npf_sgl_tgt;
-    std::reverse(npf_lvl_tgt.begin(), npf_lvl_tgt.end());
-    std::partial_sum(npf_lvl_tgt.begin(), npf_lvl_tgt.end(), 
npf_lvl_tgt.begin());
-    std::reverse(npf_lvl_tgt.begin(), npf_lvl_tgt.end());
+    npf_lvl_tgt = back_sum(npf_lvl_tgt);
 
     // Present value of 1 - excess premium load
 
@@ -598,9 +578,7 @@ void Irc7702::InitPvVectors(EIOBasis const& a_EIOBasis)
 
     std::vector<double>& npf_lvl_exc = PvNpfLvlExc[a_EIOBasis];
     npf_lvl_exc = npf_sgl_exc;
-    std::reverse(npf_lvl_exc.begin(), npf_lvl_exc.end());
-    std::partial_sum(npf_lvl_exc.begin(), npf_lvl_exc.end(), 
npf_lvl_exc.begin());
-    std::reverse(npf_lvl_exc.begin(), npf_lvl_exc.end());
+    npf_lvl_exc = back_sum(npf_lvl_exc);
 }
 
 /// For illustrations, we can't initialize everything in the ctor.
diff --git a/math_functions.hpp b/math_functions.hpp
index 8dd1e88..15b0370 100644
--- a/math_functions.hpp
+++ b/math_functions.hpp
@@ -27,6 +27,7 @@
 #include <algorithm>                    // max(), min(), transform()
 #include <cmath>                        // expm1l(), log1pl()
 #include <limits>
+#include <numeric>                      // partial_sum()
 #include <stdexcept>
 #include <type_traits>
 #include <vector>
@@ -34,6 +35,15 @@
 // TODO ?? Write functions here for other refactorable uses of
 // std::pow() throughout lmi, to facilitate reuse and unit testing.
 
+/// Backward partial summation.
+
+template<typename T>
+std::vector<T>& back_sum(std::vector<T>& v)
+{
+    std::partial_sum(v.rbegin(), v.rend(), v.rbegin());
+    return v;
+}
+
 // Some of these provide the typedefs that std::unary_function or
 // std::binary_function would have provided, because they're still
 // required for std::binder1st() or std::binder2nd(), or for PETE.



reply via email to

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