Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
context.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
22
23#include <ql/tuple.hpp>
24
25#include <iomanip>
26#include <sstream>
27
28namespace ore {
29namespace data {
30
31Size Context::varSize() const {
32 QL_REQUIRE(!scalars.empty() || !arrays.empty(), "Context::varSize(): context is empty()");
33 Size res = Null<Size>();
34 for (auto const& v : scalars) {
35 if (res != Null<Size>())
36 QL_REQUIRE(size(v.second) == res, "Context::varSize(): inconsistent var sizes");
37 else
38 res = size(v.second);
39 }
40 for (auto const& v : arrays) {
41 for (auto const& a : v.second) {
42 if (res != Null<Size>())
43 QL_REQUIRE(size(a) == res, "Context::varSize(): inconsistent var sizes");
44 else
45 res = size(a);
46 }
47 }
48 return res;
49}
50
51std::ostream& operator<<(std::ostream& out, const Context& context) {
52 constexpr Size wd1 = 30, wd2 = 10;
53 for (auto const& v : context.scalars) {
54 out << std::setw(wd1) << std::left << v.first;
55 out << "(" << std::setw(wd2) << valueTypeLabels.at(v.second.which()) << ")";
56 if (std::find(context.constants.begin(), context.constants.end(), v.first) != context.constants.end())
57 out << " const ";
58 else
59 out << " ";
60 out << std::left << v.second << '\n';
61 }
62 for (auto const& v : context.arrays) {
63 Size counter = 0;
64 for (auto const& d : v.second) {
65 out << std::setw(wd1) << std::left << (v.first + "[" + std::to_string(counter + 1) + "]");
66 out << "(" << std::setw(wd2) << valueTypeLabels.at(d.which()) << ")";
67 if (std::find(context.constants.begin(), context.constants.end(), v.first) != context.constants.end())
68 out << " const ";
69 else
70 out << " ";
71 out << std::left << d << '\n';
72 ++counter;
73 }
74 }
75 return out;
76}
77
78namespace {
79void resetSize(ValueType& v, const Size n) {
80 switch (v.which()) {
82 QuantLib::ext::get<RandomVariable>(v).resetSize(n);
83 break;
85 QuantLib::ext::get<Filter>(v).resetSize(n);
86 break;
88 QuantLib::ext::get<EventVec>(v).size = n;
89 break;
91 QuantLib::ext::get<CurrencyVec>(v).size = n;
92 break;
94 QuantLib::ext::get<IndexVec>(v).size = n;
95 break;
97 QuantLib::ext::get<DaycounterVec>(v).size = n;
98 break;
99 default:
100 QL_FAIL("resetSize(): value type not handled. internal error, contact dev.");
101 }
102}
103} // namespace
104
105void Context::resetSize(const std::size_t n) {
106 for (auto& [_, v] : scalars) {
107 ::ore::data::resetSize(v, n);
108 }
109 for (auto& [_, a] : arrays) {
110 for (auto& v : a) {
111 ::ore::data::resetSize(v, n);
112 }
113 }
114}
115
116} // namespace data
117} // namespace ore
script engine context holding variable names and values
@ data
Definition: log.hpp:77
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
Size size(const ValueType &v)
Definition: value.cpp:145
boost::variant< RandomVariable, EventVec, CurrencyVec, IndexVec, DaycounterVec, Filter > ValueType
Definition: value.hpp:60
Serializable Credit Default Swap.
Definition: namespaces.docs:23
std::map< std::string, ValueType > scalars
Definition: context.hpp:45
std::map< std::string, std::vector< ValueType > > arrays
Definition: context.hpp:46
Size varSize() const
Definition: context.cpp:31
void resetSize(const std::size_t n)
Definition: context.cpp:105
std::set< std::string > constants
Definition: context.hpp:41
string conversion utilities