Skip to content

Commit

Permalink
Deprecate function objects and replace them with lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Apr 23, 2022
1 parent b47371f commit 4258759
Show file tree
Hide file tree
Showing 33 changed files with 195 additions and 193 deletions.
3 changes: 1 addition & 2 deletions ql/discretizedasset.hpp
Expand Up @@ -27,7 +27,6 @@

#include <ql/exercise.hpp>
#include <ql/math/comparison.hpp>
#include <ql/math/functional.hpp>
#include <ql/numericalmethod.hpp>
#include <utility>

Expand Down Expand Up @@ -231,7 +230,7 @@ namespace QuantLib {
std::vector<Time> times = underlying_->mandatoryTimes();
// discard negative times...
auto i = std::find_if(exerciseTimes_.begin(), exerciseTimes_.end(),
greater_or_equal_to<Time>(0.0));
[](Time t){ return t >= 0.0; });
// and add the positive ones
times.insert(times.end(), i, exerciseTimes_.end());
return times;
Expand Down
3 changes: 1 addition & 2 deletions ql/experimental/credit/binomiallossmodel.hpp
Expand Up @@ -23,7 +23,6 @@
#include <ql/experimental/credit/basket.hpp>
#include <ql/experimental/credit/constantlosslatentmodel.hpp>
#include <ql/experimental/credit/defaultlossmodel.hpp>
#include <ql/functional.hpp>
#include <ql/handle.hpp>
#include <algorithm>
#include <numeric>
Expand Down Expand Up @@ -192,7 +191,7 @@ namespace QuantLib {
std::vector<Probability> oneMinusDefProb;//: 1.-condDefProb[j]
std::transform(condDefProb.begin(), condDefProb.end(),
std::back_inserter(oneMinusDefProb),
subtract_from<Real>(1.0));
[](Real x){ return 1.0-x; });

//breaks condDefProb and lgdsLeft to spare memory
std::transform(condDefProb.begin(), condDefProb.end(),
Expand Down
5 changes: 2 additions & 3 deletions ql/experimental/credit/distribution.cpp
Expand Up @@ -19,11 +19,10 @@

/*! \file distribution.cpp
\brief Discretized probability density and cumulative probability
*/
*/

#include <ql/experimental/credit/distribution.hpp>
#include <ql/math/comparison.hpp>
#include <ql/math/functional.hpp>
#include <ql/errors.hpp>
#include <algorithm>
#include <functional>
Expand Down Expand Up @@ -255,7 +254,7 @@ namespace QuantLib {
}

// remove losses over detachment point:
auto detachPosit = std::find_if(x_.begin(), x_.end(), greater_than<Real>(detachmentPoint));
auto detachPosit = std::find_if(x_.begin(), x_.end(), [=](Real x){ return x > detachmentPoint; });
if(detachPosit != x_.end())
x_.erase(detachPosit + 1, x_.end());

Expand Down
3 changes: 1 addition & 2 deletions ql/experimental/credit/homogeneouspooldef.hpp
Expand Up @@ -25,7 +25,6 @@
#include <ql/experimental/credit/basket.hpp>
#include <ql/experimental/credit/constantlosslatentmodel.hpp>
#include <ql/experimental/credit/defaultlossmodel.hpp>
#include <ql/math/functional.hpp>

// Intended to replace HomogeneousPoolCDOEngine in syntheticcdoengines.hpp

Expand Down Expand Up @@ -130,7 +129,7 @@ namespace QuantLib {
std::vector<Real> recoveries = copula_->recoveries();
std::transform(recoveries.begin(), recoveries.end(),
std::back_inserter(lgd),
subtract_from<Real>(1.0));
[](Real x){ return 1.0-x; });
std::transform(lgd.begin(), lgd.end(), notionals_.begin(),
lgd.begin(), std::multiplies<Real>());
std::vector<Real> prob = basket_->remainingProbabilities(d);
Expand Down
3 changes: 1 addition & 2 deletions ql/experimental/credit/inhomogeneouspooldef.hpp
Expand Up @@ -25,7 +25,6 @@
#include <ql/experimental/credit/basket.hpp>
#include <ql/experimental/credit/constantlosslatentmodel.hpp>
#include <ql/experimental/credit/defaultlossmodel.hpp>
#include <ql/math/functional.hpp>

// Intended to replace InhomogeneousPoolCDOEngine in syntheticcdoengines.hpp

Expand Down Expand Up @@ -138,7 +137,7 @@ namespace QuantLib {
std::vector<Real> recoveries = copula_->recoveries();
std::transform(recoveries.begin(), recoveries.end(),
std::back_inserter(lgd),
subtract_from<Real>(1.0));
[](Real x){ return 1.0-x; });
std::transform(lgd.begin(), lgd.end(), notionals_.begin(),
lgd.begin(), std::multiplies<Real>());
std::vector<Real> prob = basket_->remainingProbabilities(d);
Expand Down
7 changes: 3 additions & 4 deletions ql/experimental/credit/randomdefaultlatentmodel.hpp
Expand Up @@ -28,7 +28,6 @@
#include <ql/experimental/math/latentmodel.hpp>
#include <ql/experimental/math/tcopulapolicy.hpp>
#include <ql/math/beta.hpp>
#include <ql/math/functional.hpp>
#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
#include <ql/math/randomnumbers/sobolrsg.hpp>
#include <ql/math/solvers1d/brent.hpp>
Expand Down Expand Up @@ -288,7 +287,7 @@ namespace QuantLib {
}
std::transform(hitsByDate.begin(), hitsByDate.end(),
hitsByDate.begin(),
divide_by<Real>(Real(nSims_)));
[=](Real x){ return x/nSims_; });
return hitsByDate;
// \todo Provide confidence interval
}
Expand Down Expand Up @@ -503,7 +502,7 @@ namespace QuantLib {
/*
std::vector<Real>::iterator itPastPerc =
std::find_if(losses.begin() + position, losses.end(),
greater_or_equal_to<Real>(perctlInf));
[=](Real x){ return x >= perctlInf; });
// notice if the sample is flat at the end this might be zero
Size pointsOverVal = nSims_ - std::distance(itPastPerc, losses.end());
return pointsOverVal == 0 ? 0. :
Expand Down Expand Up @@ -625,7 +624,7 @@ namespace QuantLib {
std::vector<Real> varLevels = splitVaRAndError(date, loss, 0.95)[0];
// turn relative units into absolute:
std::transform(varLevels.begin(), varLevels.end(), varLevels.begin(),
multiply_by<Real>(loss));
[=](Real x){ return x * loss; });
return varLevels;
}

Expand Down
3 changes: 1 addition & 2 deletions ql/experimental/math/latentmodel.hpp
Expand Up @@ -28,7 +28,6 @@
#include <ql/experimental/math/gaussiancopulapolicy.hpp>
#include <ql/experimental/math/tcopulapolicy.hpp>
#include <ql/math/randomnumbers/boxmullergaussianrng.hpp>
#include <ql/math/functional.hpp>
#include <ql/experimental/math/polarstudenttrng.hpp>
#include <ql/handle.hpp>
#include <ql/quote.hpp>
Expand All @@ -47,7 +46,7 @@ namespace QuantLib {
std::vector<Real> operator()(Real d, std::vector<Real> v)
{
std::transform(v.begin(), v.end(), v.begin(),
multiply_by<Real>(d));
[=](Real x){ return x*d; });
return v;
}
};
Expand Down
5 changes: 2 additions & 3 deletions ql/experimental/math/multidimquadrature.hpp
Expand Up @@ -30,7 +30,6 @@
#ifndef QL_PATCH_SOLARIS

#include <ql/math/integrals/gaussianquadratures.hpp>
#include <ql/math/functional.hpp>
#include <ql/functional.hpp>

namespace QuantLib {
Expand Down Expand Up @@ -60,8 +59,8 @@ namespace QuantLib {
//first one, we do not know the size of the vector returned by f
Integer i = order()-1;
std::vector<Real> term = f(x_[i]);// potential copy! @#$%^!!!
std::for_each(term.begin(), term.end(),
multiply_by<Real>(w_[i]));
std::for_each(term.begin(), term.end(),
[&](Real x){ return x * w_[i]; });
std::vector<Real> sum = term;

for (i--; i >= 0; --i) {
Expand Down
3 changes: 1 addition & 2 deletions ql/legacy/libormarketmodels/lfmcovarproxy.cpp
Expand Up @@ -18,7 +18,6 @@
*/

#include <ql/legacy/libormarketmodels/lfmcovarproxy.hpp>
#include <ql/math/functional.hpp>
#include <ql/math/integrals/kronrodintegral.hpp>
#include <utility>

Expand Down Expand Up @@ -52,7 +51,7 @@ namespace QuantLib {
for (Size i=0; i<size_; ++i) {
std::transform(pca.row_begin(i), pca.row_end(i),
pca.row_begin(i),
multiply_by<Real>(vol[i]));
[&](Real x){ return x * vol[i]; });
}

return pca;
Expand Down
8 changes: 4 additions & 4 deletions ql/legacy/libormarketmodels/lfmhullwhiteparam.cpp
Expand Up @@ -19,7 +19,6 @@

#include <ql/math/matrixutilities/pseudosqrt.hpp>
#include <ql/legacy/libormarketmodels/lfmhullwhiteparam.hpp>
#include <ql/math/functional.hpp>

namespace QuantLib {

Expand Down Expand Up @@ -51,11 +50,12 @@ namespace QuantLib {
// "Reconstructing a valid correlation matrix from invalid data"
// (<http://www.quarchome.org/correlationmatrix.pdf>)
for (Size i=0; i < size_-1; ++i) {
Real p = std::sqrt(std::inner_product(
tmpSqrtCorr[i],tmpSqrtCorr[i]+factors_,
tmpSqrtCorr[i], 0.0));
std::transform(
tmpSqrtCorr[i], tmpSqrtCorr[i]+factors_, sqrtCorr[i],
divide_by<Real>(std::sqrt(std::inner_product(
tmpSqrtCorr[i],tmpSqrtCorr[i]+factors_,
tmpSqrtCorr[i], 0.0))));
[=](Real x){ return x/p; });
}
}

Expand Down
36 changes: 19 additions & 17 deletions ql/math/array.hpp
Expand Up @@ -28,7 +28,6 @@

#include <ql/types.hpp>
#include <ql/errors.hpp>
#include <ql/math/functional.hpp>
#include <ql/utilities/null.hpp>
#include <boost/iterator/reverse_iterator.hpp>
#include <boost/type_traits.hpp>
Expand Down Expand Up @@ -323,8 +322,8 @@ namespace QuantLib {


inline const Array& Array::operator+=(Real x) {
std::transform(begin(),end(),begin(),
add<Real>(x));
std::transform(begin(), end(), begin(),
[=](Real y) { return y+x; });
return *this;
}

Expand All @@ -339,7 +338,7 @@ namespace QuantLib {

inline const Array& Array::operator-=(Real x) {
std::transform(begin(),end(),begin(),
subtract<Real>(x));
[=](Real y) { return y-x; });
return *this;
}

Expand All @@ -353,8 +352,8 @@ namespace QuantLib {
}

inline const Array& Array::operator*=(Real x) {
std::transform(begin(),end(),begin(),
multiply_by<Real>(x));
std::transform(begin(), end(), begin(),
[=](Real y) { return y*x; });
return *this;
}

Expand All @@ -368,8 +367,11 @@ namespace QuantLib {
}

inline const Array& Array::operator/=(Real x) {
std::transform(begin(),end(),begin(),
divide_by<Real>(x));
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE(x != 0.0, "division by zero");
#endif
std::transform(begin(), end(), begin(),
[=](Real y) { return y/x; });
return *this;
}

Expand Down Expand Up @@ -534,15 +536,15 @@ namespace QuantLib {

inline Array operator+(const Array& v1, Real a) {
Array result(v1.size());
std::transform(v1.begin(),v1.end(),result.begin(),
add<Real>(a));
std::transform(v1.begin(), v1.end(), result.begin(),
[=](Real y) { return y+a; });
return result;
}

inline Array operator+(Real a, const Array& v2) {
Array result(v2.size());
std::transform(v2.begin(),v2.end(),result.begin(),
add<Real>(a));
[=](Real y) { return a+y; });
return result;
}

Expand All @@ -559,14 +561,14 @@ namespace QuantLib {
inline Array operator-(const Array& v1, Real a) {
Array result(v1.size());
std::transform(v1.begin(),v1.end(),result.begin(),
subtract<Real>(a));
[=](Real y) { return y-a; });
return result;
}

inline Array operator-(Real a, const Array& v2) {
Array result(v2.size());
std::transform(v2.begin(),v2.end(),result.begin(),
subtract_from<Real>(a));
[=](Real y) { return a-y; });
return result;
}

Expand All @@ -583,14 +585,14 @@ namespace QuantLib {
inline Array operator*(const Array& v1, Real a) {
Array result(v1.size());
std::transform(v1.begin(),v1.end(),result.begin(),
multiply_by<Real>(a));
[=](Real y) { return y*a; });
return result;
}

inline Array operator*(Real a, const Array& v2) {
Array result(v2.size());
std::transform(v2.begin(),v2.end(),result.begin(),
multiply_by<Real>(a));
[=](Real y) { return a*y; });
return result;
}

Expand All @@ -607,14 +609,14 @@ namespace QuantLib {
inline Array operator/(const Array& v1, Real a) {
Array result(v1.size());
std::transform(v1.begin(),v1.end(),result.begin(),
divide_by<Real>(a));
[=](Real y) { return y/a; });
return result;
}

inline Array operator/(Real a, const Array& v2) {
Array result(v2.size());
std::transform(v2.begin(),v2.end(),result.begin(),
divide<Real>(a));
[=](Real y) { return a/y; });
return result;
}

Expand Down
4 changes: 1 addition & 3 deletions ql/math/autocovariance.hpp
Expand Up @@ -26,7 +26,6 @@

#include <ql/math/fastfouriertransform.hpp>
#include <ql/math/array.hpp>
#include <ql/math/functional.hpp>
#include <complex>
#include <vector>
#include <algorithm>
Expand Down Expand Up @@ -65,8 +64,7 @@ namespace QuantLib {
std::size_t n = 1;
for (InputIterator it = begin; it != end; ++it, ++n)
mean = (mean*Real(n-1) + *it)/n;
std::transform(begin, end, out,
subtract<Real>(mean));
std::transform(begin, end, out, [=](Real x){ return x - mean; });
return mean;
}

Expand Down
3 changes: 1 addition & 2 deletions ql/math/distributions/chisquaredistribution.cpp
Expand Up @@ -125,8 +125,7 @@ namespace QuantLib {
// use a Brent solver for the rest
Brent solver;
solver.setMaxEvaluations(evaluations);
return solver.solve(compose(subtract<Real>(x),
nonCentralDist_),
return solver.solve([&](Real y) { return nonCentralDist_(y) - x; },
accuracy_, 0.75*upper,
(evaluations == maxEvaluations_)? 0.0: 0.5*upper,
upper);
Expand Down

0 comments on commit 4258759

Please sign in to comment.