Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
compiledformula.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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/*! \file qle/math/compiledformula.hpp
20 \brief compiled formula
21 \ingroup indexes
22*/
23
24#pragma once
25
26#include <ql/errors.hpp>
27#include <ql/math/comparison.hpp>
28#include <ql/types.hpp>
29#include <ql/utilities/null.hpp>
30
31#include <vector>
32
33using QuantLib::Null;
34using QuantLib::Real;
35using QuantLib::Size;
36
37namespace QuantExt {
38
39//! helper class representing a formula with variables given by an id v
41public:
43 CompiledFormula() : op_(none), x_(0.0), v_(Null<Size>()) {}
44 // ctor representing a formula that is a constant value x
45 CompiledFormula(const Real x) : op_(none), x_(x), v_(Null<Size>()) {}
46 // ctor representing a formula that is a variable with index v
47 CompiledFormula(const Size v) : op_(none), x_(Null<Real>()), v_(v) {}
48 CompiledFormula(const CompiledFormula& f) : op_(f.op_), x_(f.x_), v_(f.v_), args_(f.args_) {}
49 CompiledFormula(CompiledFormula&& f) : op_(f.op_), x_(f.x_), v_(f.v_) { args_.swap(f.args_); }
52
53 // evaluate given values for the variables at index 0, 1, 2, ...
54 template <class I> Real operator()(I begin, I end) const;
55 Real operator()(const std::vector<Real>& values) const { return operator()(values.begin(), values.end()); }
56
57 // Operators
62 //
64 //
69 //
73 //
79
80private:
84 Real x_;
85 Size v_;
86 std::vector<CompiledFormula> args_;
87};
88
89// implementation
90
91template <class I> Real CompiledFormula::operator()(I begin, I end) const {
92 if (x_ != Null<Real>())
93 return x_;
94 if (v_ != Null<Size>()) {
95 QL_REQUIRE((end - begin) > static_cast<int>(v_),
96 "CompiledFormula: need value for index " << v_ << ", given values size is " << (end - begin));
97 return *(begin + v_);
98 }
99 switch (op_) {
100 case plus:
101 return args_[0](begin, end) + args_[1](begin, end);
102 case minus:
103 return args_[0](begin, end) - args_[1](begin, end);
104 case multiply:
105 return args_[0](begin, end) * args_[1](begin, end);
106 case divide:
107 return args_[0](begin, end) / args_[1](begin, end);
108 case max:
109 return std::max(args_[0](begin, end), args_[1](begin, end));
110 case min:
111 return std::min(args_[0](begin, end), args_[1](begin, end));
112 case pow:
113 return std::pow(args_[0](begin, end), args_[1](begin, end));
114 case gtZero: {
115 Real tmp = args_[0](begin, end);
116 return tmp > 0.0 && !QuantLib::close_enough(tmp, 0.0) ? 1.0 : 0.0;
117 }
118 case geqZero: {
119 Real tmp = args_[0](begin, end);
120 return tmp > 0.0 || QuantLib::close_enough(tmp, 0.0) ? 1.0 : 0.0;
121 }
122 case abs:
123 return std::abs(args_[0](begin, end));
124 case negate:
125 return -args_[0](begin, end);
126 case exp:
127 return std::exp(args_[0](begin, end));
128 case log:
129 return std::log(args_[0](begin, end));
130 default:
131 QL_FAIL("CompiledFormula: unknown operator");
132 }
133}
134
135} // namespace QuantExt
helper class representing a formula with variables given by an id v
friend CompiledFormula operator/(CompiledFormula, const CompiledFormula &)
Real operator()(I begin, I end) const
CompiledFormula(const CompiledFormula &f)
std::vector< CompiledFormula > args_
CompiledFormula(CompiledFormula &&f)
friend CompiledFormula operator*(CompiledFormula, const CompiledFormula &)
Real operator()(const std::vector< Real > &values) const
CompiledFormula & operator-=(const CompiledFormula &)
friend CompiledFormula operator+(CompiledFormula, const CompiledFormula &)
CompiledFormula & operator/=(const CompiledFormula &)
friend CompiledFormula unaryOp(CompiledFormula, Operator op)
CompiledFormula operator-() const
friend CompiledFormula binaryOp(CompiledFormula, const CompiledFormula &, Operator op)
CompiledFormula & operator=(const CompiledFormula &)
CompiledFormula & operator+=(const CompiledFormula &)
CompiledFormula & operator*=(const CompiledFormula &)