21#include <ql/errors.hpp>
30 const std::set<std::string>& ids,
const bool requireUniqueIds,
31 const std::function<Real(Real a, Real x)>& accumulator,
const Real accumulatorInit)
32 :
JointNPVCube({cube1, cube2}, ids, requireUniqueIds, accumulator, accumulatorInit) {}
35 const bool requireUniqueIds,
const std::function<Real(Real a, Real x)>& accumulator,
36 const Real accumulatorInit)
37 :
NPVCube(), cubes_(cubes), accumulator_(accumulator), accumulatorInit_(accumulatorInit) {
41 QL_REQUIRE(!cubes.empty(),
"JointNPVCube: at least one cube must be given");
45 for (Size i = 1; i < cubes.size(); ++i) {
47 "JointNPVCube: numDates do not match for cube #"
50 "JointNPVCube: samples do not match for cube #" << i <<
" (" <<
cubes_[i]->
samples()
53 << i <<
" (" <<
cubes_[i]->
depth() <<
" vs. cube #0 ("
54 << cubes[0]->
depth() <<
")");
57 std::set<std::string> allIds;
63 for (Size i = 0; i <
cubes_.size(); ++i) {
65 const auto& [ignored2, success] = allIds.insert(
id);
66 QL_REQUIRE(!requireUniqueIds || success,
67 "JointNPVSensiCube: input cubes have duplicate id '" <<
id <<
"', this is not allowed");
74 for (
const auto&
id : allIds) {
80 for (
const auto& [
id, jointPos] :
idIdx_) {
81 for (
auto const& c :
cubes_) {
82 auto searchIt = c->idsAndIndexes().find(
id);
83 if (searchIt != c->idsAndIndexes().end()) {
84 cubeAndId_[jointPos].insert(std::make_pair(c, searchIt->second));
89 "JointNPVCube: internal error, got no input cubes for id '" <<
id <<
"'");
91 "JointNPVCube: internal error, got more than one input cube for id '"
92 <<
id <<
"', but unique input ids qre required");
112 "JointNPVCube: id (" <<
id <<
") out of range, have " <<
cubeAndId_.size() <<
" ids");
118 if (cids.size() == 1)
119 return cids.begin()->first->getT0(cids.begin()->second,
depth);
121 for (
auto const& p : cids)
128 QL_REQUIRE(c.size() == 1,
129 "JointNPVCube::setT0(): not allowed, because id '" <<
id <<
"' occurs in more than one input cube");
130 (*c.begin()).first->setT0(
value, (*c.begin()).second,
depth);
135 if (cids.size() == 1)
136 return cids.begin()->first->get(cids.begin()->second, date, sample,
depth);
138 for (
auto const& p : cids)
145 QL_REQUIRE(c.size() == 1,
146 "JointNPVCube::set(): not allowed, because id '" <<
id <<
"' occurs in more than one input cube");
147 (*c.begin()).first->set(
value, (*c.begin()).second, date, sample,
depth);
JointNPVCube(const QuantLib::ext::shared_ptr< NPVCube > &cube1, const QuantLib::ext::shared_ptr< NPVCube > &cube2, const std::set< std::string > &ids={}, const bool requireUniqueIds=true, const std::function< Real(Real a, Real x)> &accumulator=[](Real a, Real x) { return a+x;}, const Real accumulatorInit=0.0)
const std::function< Real(Real a, Real x)> accumulator_
void set(Real value, Size id, Size date, Size sample, Size depth=0) override
Set a value in the cube using index.
Size numDates() const override
const std::map< std::string, Size > & idsAndIndexes() const override
Get a map of id and their index position in this cube.
Real getT0(Size id, Size depth=0) const override
Get a T0 value from the cube using index.
std::set< std::pair< QuantLib::ext::shared_ptr< NPVCube >, Size > > cubeAndId(Size id) const
Size numIds() const override
Return the length of each dimension.
const Real accumulatorInit_
const std::vector< QuantLib::ext::shared_ptr< NPVCube > > cubes_
const std::vector< QuantLib::Date > & dates() const override
Get the vector of dates for this cube.
Size depth() const override
QuantLib::Date asof() const override
Return the asof date (T0 date)
std::map< std::string, Size > idIdx_
Size samples() const override
void setT0(Real value, Size id, Size depth=0) override
Set a value in the cube using index.
std::vector< std::set< std::pair< QuantLib::ext::shared_ptr< NPVCube >, Size > > > cubeAndId_
Real get(Size id, Size date, Size sample, Size depth=0) const override
Get a value from the cube using index.
NPV Cube class stores both future and current NPV values.
const std::set< std::string > ids() const
Get a set of all ids in the cube.
SafeStack< ValueType > value
join n cubes in terms of stored ids
Size size(const ValueType &v)