Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
ssaform.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
19#include <qle/ad/ssaform.hpp>
20
23
24#include <sstream>
25
26namespace QuantExt {
27
28namespace {
29std::string getLabel(const ComputationGraph& g, const std::size_t i, const bool includeLabels = true) {
30 auto l = g.labels().find(i);
31 std::string label = "v_" + std::to_string(i);
32 if (l != g.labels().end() && includeLabels) {
33 for (auto tmp = l->second.begin(); tmp != l->second.end(); ++tmp) {
34 label = label + (tmp == l->second.begin() ? "[" : "") + *tmp +
35 (tmp != std::next(l->second.end(), -1) ? ";" : "]");
36 }
37 }
38 return label;
39}
40} // namespace
41
42template <class T>
43std::string ssaForm(const ComputationGraph& g, const std::vector<std::string>& opCodeLabels,
44 const std::vector<T>& values, const std::vector<T>& values2) {
45
46 std::ostringstream os;
47
48 for (std::size_t i = 0; i < g.size(); ++i) {
49
50 os << i << "," << getLabel(g, i);
51
52 os << ",";
53 if (!g.predecessors(i).empty()) {
54 os << (opCodeLabels.size() > g.opId(i) ? opCodeLabels[g.opId(i)] : "???") << "(";
55 for (std::size_t j = 0; j < g.predecessors(i).size(); ++j) {
56 auto p = g.predecessors(i)[j];
57 os << getLabel(g, p, false) << (j < g.predecessors(i).size() - 1 ? ";" : "");
58 }
59 os << ")";
60 }
61
62 if (values.size() > i) {
63 os << "," << values[i];
64 }
65
66 if (values2.size() > i) {
67 os << "," << values2[i];
68 }
69
70 os << "\n";
71 }
72
73 return os.str();
74}
75
76template std::string ssaForm(const ComputationGraph& g, const std::vector<std::string>& opCodeLabels,
77 const std::vector<double>& values, const std::vector<double>& values2);
78template std::string ssaForm(const ComputationGraph& g, const std::vector<std::string>& opCodeLabels,
79 const std::vector<RandomVariable>& values, const std::vector<RandomVariable>& values2);
80
81} // namespace QuantExt
const std::vector< std::size_t > & predecessors(const std::size_t node) const
std::size_t opId(const std::size_t node) const
std::string ssaForm(const ComputationGraph &g, const std::vector< std::string > &opCodeLabels, const std::vector< T > &values, const std::vector< T > &values2)
Definition: ssaform.cpp:43
convert cg to ssa form