Skip to content

Commit e8682f4

Browse files
pcaspersjenkins
authored andcommitted
QPR-11984 cleaner implementation of == and != for Filter, RandomVariable
1 parent b8eda2c commit e8682f4

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

QuantExt/qle/math/randomvariable.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ bool operator==(const Filter& a, const Filter& b) {
9090
return true;
9191
}
9292

93+
bool operator!=(const Filter& a, const Filter& b) { return !(a == b); }
94+
9395
Filter operator&&(Filter x, const Filter& y) {
9496
QL_REQUIRE(!x.initialised() || !y.initialised() || x.size() == y.size(),
9597
"RandomVariable: x && y: x size (" << x.size() << ") must be equal to y size (" << y.size() << ")");
@@ -278,12 +280,18 @@ void checkTimeConsistency(const RandomVariable& x, const RandomVariable& y) {
278280
bool operator==(const RandomVariable& a, const RandomVariable& b) {
279281
if (a.size() != b.size())
280282
return false;
281-
for (Size j = 0; j < a.size(); ++j)
282-
if (a[j] != b[j])
283-
return false;
283+
if (a.deterministic_ && b.deterministic_) {
284+
return a.data_.front() == b.data_.front();
285+
} else {
286+
for (Size j = 0; j < a.size(); ++j)
287+
if (a[j] != b[j])
288+
return false;
289+
}
284290
return QuantLib::close_enough(a.time(), b.time());
285291
}
286292

293+
bool operator!=(const RandomVariable& a, const RandomVariable b) { return !(a == b); }
294+
287295
RandomVariable& RandomVariable::operator+=(const RandomVariable& y) {
288296
if (!y.initialised())
289297
clear();

QuantExt/qle/math/randomvariable.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct Filter {
6868
};
6969

7070
bool operator==(const Filter& a, const Filter& b);
71+
bool operator!=(const Filter& a, const Filter& b);
7172

7273
Filter operator&&(Filter, const Filter&);
7374
Filter operator||(Filter, const Filter&);
@@ -107,6 +108,7 @@ struct RandomVariable {
107108
RandomVariable& operator-=(const RandomVariable&);
108109
RandomVariable& operator*=(const RandomVariable&);
109110
RandomVariable& operator/=(const RandomVariable&);
111+
friend bool operator==(const RandomVariable&, const RandomVariable&);
110112
friend RandomVariable operator+(RandomVariable, const RandomVariable&);
111113
friend RandomVariable operator-(RandomVariable, const RandomVariable&);
112114
friend RandomVariable operator*(RandomVariable, const RandomVariable&);
@@ -149,6 +151,7 @@ struct RandomVariable {
149151
};
150152

151153
bool operator==(const RandomVariable& a, const RandomVariable& b);
154+
bool operator!=(const RandomVariable& a, const RandomVariable& b);
152155

153156
RandomVariable operator+(RandomVariable, const RandomVariable&);
154157
RandomVariable operator-(RandomVariable, const RandomVariable&);

0 commit comments

Comments
 (0)