Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Attributes | Friends | List of all members
CompiledFormula Class Reference

helper class representing a formula with variables given by an id v More...

#include <qle/math/compiledformula.hpp>

+ Collaboration diagram for CompiledFormula:

Public Types

enum  Operator {
  none , plus , minus , multiply ,
  divide , max , min , pow ,
  abs , gtZero , geqZero , negate ,
  exp , log
}
 

Public Member Functions

 CompiledFormula ()
 
 CompiledFormula (const Real x)
 
 CompiledFormula (const Size v)
 
 CompiledFormula (const CompiledFormula &f)
 
 CompiledFormula (CompiledFormula &&f)
 
CompiledFormulaoperator= (const CompiledFormula &)
 
CompiledFormulaoperator= (CompiledFormula &&)
 
template<class I >
Real operator() (I begin, I end) const
 
Real operator() (const std::vector< Real > &values) const
 
CompiledFormulaoperator+= (const CompiledFormula &)
 
CompiledFormulaoperator-= (const CompiledFormula &)
 
CompiledFormulaoperator*= (const CompiledFormula &)
 
CompiledFormulaoperator/= (const CompiledFormula &)
 
CompiledFormula operator- () const
 

Private Attributes

Operator op_
 
Real x_
 
Size v_
 
std::vector< CompiledFormulaargs_
 

Friends

CompiledFormula operator+ (CompiledFormula, const CompiledFormula &)
 
CompiledFormula operator- (CompiledFormula, const CompiledFormula &)
 
CompiledFormula operator* (CompiledFormula, const CompiledFormula &)
 
CompiledFormula operator/ (CompiledFormula, const CompiledFormula &)
 
CompiledFormula max (CompiledFormula, const CompiledFormula &)
 
CompiledFormula min (CompiledFormula, const CompiledFormula &)
 
CompiledFormula pow (CompiledFormula, const CompiledFormula &)
 
CompiledFormula gtZero (CompiledFormula)
 
CompiledFormula geqZero (CompiledFormula)
 
CompiledFormula abs (CompiledFormula)
 
CompiledFormula exp (CompiledFormula)
 
CompiledFormula log (CompiledFormula)
 
CompiledFormula unaryOp (CompiledFormula, Operator op)
 
CompiledFormula binaryOp (CompiledFormula, const CompiledFormula &, Operator op)
 

Detailed Description

helper class representing a formula with variables given by an id v

Definition at line 40 of file compiledformula.hpp.

Member Enumeration Documentation

◆ Operator

enum Operator

Constructor & Destructor Documentation

◆ CompiledFormula() [1/5]

Definition at line 43 of file compiledformula.hpp.

◆ CompiledFormula() [2/5]

CompiledFormula ( const Real  x)

Definition at line 45 of file compiledformula.hpp.

45: op_(none), x_(x), v_(Null<Size>()) {}

◆ CompiledFormula() [3/5]

CompiledFormula ( const Size  v)

Definition at line 47 of file compiledformula.hpp.

47: op_(none), x_(Null<Real>()), v_(v) {}

◆ CompiledFormula() [4/5]

Definition at line 48 of file compiledformula.hpp.

48: op_(f.op_), x_(f.x_), v_(f.v_), args_(f.args_) {}
std::vector< CompiledFormula > args_

◆ CompiledFormula() [5/5]

Definition at line 49 of file compiledformula.hpp.

49: op_(f.op_), x_(f.x_), v_(f.v_) { args_.swap(f.args_); }

Member Function Documentation

◆ operator=() [1/2]

CompiledFormula & operator= ( const CompiledFormula f)

Definition at line 23 of file compiledformula.cpp.

23 {
24 op_ = f.op_;
25 x_ = f.x_;
26 v_ = f.v_;
27 args_ = f.args_;
28 return *this;
29}

◆ operator=() [2/2]

CompiledFormula & operator= ( CompiledFormula &&  f)

Definition at line 31 of file compiledformula.cpp.

31 {
32 op_ = f.op_;
33 x_ = f.x_;
34 v_ = f.v_;
35 args_.swap(f.args_);
36 return *this;
37}

◆ operator()() [1/2]

Real operator() ( begin,
end 
) const

Definition at line 91 of file compiledformula.hpp.

91 {
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}

◆ operator()() [2/2]

Real operator() ( const std::vector< Real > &  values) const

Definition at line 55 of file compiledformula.hpp.

55{ return operator()(values.begin(), values.end()); }
Real operator()(I begin, I end) const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator+=()

CompiledFormula & operator+= ( const CompiledFormula y)

Definition at line 41 of file compiledformula.cpp.

41 {
42 std::vector<CompiledFormula> newArgs;
43 newArgs.push_back(*this);
44 newArgs.push_back(y);
45 op_ = plus;
46 x_ = Null<Real>();
47 v_ = Null<Size>();
48 args_.swap(newArgs);
49 return *this;
50}

◆ operator-=()

CompiledFormula & operator-= ( const CompiledFormula y)

Definition at line 52 of file compiledformula.cpp.

52 {
53 std::vector<CompiledFormula> newArgs;
54 newArgs.push_back(*this);
55 newArgs.push_back(y);
56 op_ = minus;
57 x_ = Null<Real>();
58 v_ = Null<Size>();
59 args_.swap(newArgs);
60 return *this;
61}

◆ operator*=()

CompiledFormula & operator*= ( const CompiledFormula y)

Definition at line 63 of file compiledformula.cpp.

63 {
64 std::vector<CompiledFormula> newArgs;
65 newArgs.push_back(*this);
66 newArgs.push_back(y);
67 op_ = multiply;
68 x_ = Null<Real>();
69 v_ = Null<Size>();
70 args_.swap(newArgs);
71 return *this;
72}

◆ operator/=()

CompiledFormula & operator/= ( const CompiledFormula y)

Definition at line 74 of file compiledformula.cpp.

74 {
75 std::vector<CompiledFormula> newArgs;
76 newArgs.push_back(*this);
77 newArgs.push_back(y);
78 op_ = divide;
79 x_ = Null<Real>();
80 v_ = Null<Size>();
81 args_.swap(newArgs);
82 return *this;
83}

◆ operator-()

CompiledFormula operator- ( ) const

Definition at line 87 of file compiledformula.cpp.

87 {
89 r.args_ = std::vector<CompiledFormula>(1, *this);
90 r.op_ = negate;
91 r.x_ = Null<Real>();
92 r.v_ = Null<Size>();
93 return r;
94}

Friends And Related Function Documentation

◆ operator+

CompiledFormula operator+ ( CompiledFormula  x,
const CompiledFormula y 
)
friend

Definition at line 98 of file compiledformula.cpp.

98 {
99 x += y;
100 return x;
101}

◆ operator-

CompiledFormula operator- ( CompiledFormula  x,
const CompiledFormula y 
)
friend

Definition at line 103 of file compiledformula.cpp.

103 {
104 x -= y;
105 return x;
106}

◆ operator*

CompiledFormula operator* ( CompiledFormula  x,
const CompiledFormula y 
)
friend

Definition at line 108 of file compiledformula.cpp.

108 {
109 x *= y;
110 return x;
111}

◆ operator/

CompiledFormula operator/ ( CompiledFormula  x,
const CompiledFormula y 
)
friend

Definition at line 113 of file compiledformula.cpp.

113 {
114 x /= y;
115 return x;
116}

◆ max

CompiledFormula max ( CompiledFormula  x,
const CompiledFormula y 
)
friend

Definition at line 159 of file compiledformula.cpp.

159{ return binaryOp(x, y, CompiledFormula::max); }
friend CompiledFormula binaryOp(CompiledFormula, const CompiledFormula &, Operator op)

◆ min

CompiledFormula min ( CompiledFormula  x,
const CompiledFormula y 
)
friend

Definition at line 162 of file compiledformula.cpp.

162{ return binaryOp(x, y, CompiledFormula::min); }

◆ pow

CompiledFormula pow ( CompiledFormula  x,
const CompiledFormula y 
)
friend

Definition at line 165 of file compiledformula.cpp.

165{ return binaryOp(x, y, CompiledFormula::pow); }

◆ gtZero

CompiledFormula gtZero ( CompiledFormula  x)
friend

Definition at line 142 of file compiledformula.cpp.

142{ return unaryOp(x, CompiledFormula::gtZero); }
friend CompiledFormula unaryOp(CompiledFormula, Operator op)

◆ geqZero

CompiledFormula geqZero ( CompiledFormula  x)
friend

Definition at line 145 of file compiledformula.cpp.

145{ return unaryOp(x, CompiledFormula::geqZero); }

◆ abs

CompiledFormula abs ( CompiledFormula  x)
friend

Definition at line 148 of file compiledformula.cpp.

148{ return unaryOp(x, CompiledFormula::abs); }

◆ exp

CompiledFormula exp ( CompiledFormula  x)
friend

Definition at line 151 of file compiledformula.cpp.

151{ return unaryOp(x, CompiledFormula::exp); }

◆ log

CompiledFormula log ( CompiledFormula  x)
friend

Definition at line 154 of file compiledformula.cpp.

154{ return unaryOp(x, CompiledFormula::log); }

◆ unaryOp

Definition at line 118 of file compiledformula.cpp.

118 {
119 std::vector<CompiledFormula> newArgs;
120 newArgs.push_back(x);
121 x.op_ = op;
122 x.x_ = Null<Real>();
123 x.v_ = Null<Size>();
124 x.args_.swap(newArgs);
125 return x;
126}

◆ binaryOp

CompiledFormula binaryOp ( CompiledFormula  x,
const CompiledFormula y,
CompiledFormula::Operator  op 
)
friend

Definition at line 128 of file compiledformula.cpp.

128 {
129 std::vector<CompiledFormula> newArgs;
130 newArgs.push_back(x);
131 newArgs.push_back(y);
132 x.op_ = op;
133 x.x_ = Null<Real>();
134 x.v_ = Null<Size>();
135 x.args_.swap(newArgs);
136 return x;
137}

Member Data Documentation

◆ op_

Operator op_
private

Definition at line 83 of file compiledformula.hpp.

◆ x_

Real x_
private

Definition at line 84 of file compiledformula.hpp.

◆ v_

Size v_
private

Definition at line 85 of file compiledformula.hpp.

◆ args_

std::vector<CompiledFormula> args_
private

Definition at line 86 of file compiledformula.hpp.