sigma  0.0.0
Loading...
Searching...
No Matches
uncertain.hpp
Go to the documentation of this file.
1#pragma once
2#include <cmath>
3#include <iostream>
4#include <map>
5#include <memory>
6#include <type_traits>
7#include <utility>
8
12
13namespace sigma {
14
15// Foward Declaration
16namespace detail_ {
17template<typename T>
18class Setter; // Class to alter values inside Uncertain
19} // namespace detail_
20
32template<typename ValueType>
33class Uncertain {
34public:
37
39 using value_t = ValueType;
40
43
45 using dep_sd_ptr = std::shared_ptr<dep_sd_t>;
46
48 using deps_map_t = std::map<dep_sd_ptr, value_t>;
49
51 Uncertain() noexcept = default;
52
62 Uncertain(value_t mean) : m_mean_(mean), m_sd_(0.0) {}
63
75
82 value_t mean() const { return m_mean_; }
83
90 value_t sd() const { return m_sd_; }
91
98 const deps_map_t& deps() const { return m_deps_; }
99
100private:
102 value_t m_mean_;
103
105 value_t m_sd_;
106
110 deps_map_t m_deps_ = {};
111
115 template<typename T>
116 friend class detail_::Setter;
117
118}; // class Uncertain
119
120// -- Out-of-line Definitions --------------------------------------------------
121
122template<typename ValueType>
124 m_mean_(mean), m_sd_(std::abs(sd)) {
125 m_deps_.emplace(
126 std::make_pair(std::make_shared<dep_sd_t>(sd), value_t{1.0}));
127}
128
129// -- Utility functions --------------------------------------------------------
130
143template<typename ValueType>
144std::ostream& operator<<(std::ostream& os, const Uncertain<ValueType>& u) {
145 os << u.mean() << "+/-" << u.sd();
146 return os;
147}
148
159template<typename ValueType1, typename ValueType2>
161 const Uncertain<ValueType2>& rhs) {
162 if constexpr(!std::is_same_v<ValueType1, ValueType2>) {
163 return false;
164 } else {
165 if(lhs.mean() != rhs.mean()) return false;
166 if(lhs.sd() != rhs.sd()) return false;
167 if(lhs.deps() != rhs.deps()) return false;
168 return true;
169 }
170}
171
182template<typename ValueType1, typename ValueType2>
184 const Uncertain<ValueType2>& rhs) {
185 return !(lhs == rhs);
186}
187
200template<typename ValueType1, typename ValueType2>
202 const Uncertain<ValueType2>& rhs) {
203 return lhs.mean() < rhs.mean();
204}
205
218template<typename ValueType1, typename ValueType2>
220 const Uncertain<ValueType2>& rhs) {
221 return rhs < lhs;
222}
223
234template<typename ValueType1, typename ValueType2>
236 const Uncertain<ValueType2>& rhs) {
237 return (lhs == rhs) || (lhs < rhs);
238}
239
250template<typename ValueType1, typename ValueType2>
252 const Uncertain<ValueType2>& rhs) {
253 return (lhs == rhs) || (lhs > rhs);
254}
255
258
261
262} // namespace sigma
Models an unceratin variable.
Definition uncertain.hpp:33
std::ostream & operator<<(std::ostream &os, const Uncertain< ValueType > &u)
Overload stream insertion to print uncertain variable.
Definition uncertain.hpp:144
bool operator<=(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Whether one variable is less than or equal to another.
Definition uncertain.hpp:235
bool operator>(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Whether one variable is greater than another.
Definition uncertain.hpp:219
Uncertain(value_t mean, value_t sd)
Construct an uncertain value from mean and standard deviation.
Definition uncertain.hpp:123
value_t mean() const
Definition uncertain.hpp:82
std::map< dep_sd_ptr, value_t > deps_map_t
Definition uncertain.hpp:48
bool operator>=(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Whether one variable is greater than or equal to another.
Definition uncertain.hpp:251
value_t dep_sd_t
Definition uncertain.hpp:42
bool operator<(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Whether one variable is less than another.
Definition uncertain.hpp:201
const deps_map_t & deps() const
Get the dependencies of the variable.
Definition uncertain.hpp:98
Uncertain< float > my_t
Definition uncertain.hpp:36
Uncertain() noexcept=default
Default ctor.
bool operator==(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Compare two variables for equality.
Definition uncertain.hpp:160
bool operator!=(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Compare two variables for inequality.
Definition uncertain.hpp:183
float value_t
Definition uncertain.hpp:39
value_t sd() const
Definition uncertain.hpp:90
std::shared_ptr< dep_sd_t > dep_sd_ptr
Definition uncertain.hpp:45
Modifies an unceratin variable.
Definition setter.hpp:19
The namespace that contains the implementation details of the library.
Definition operation_common.hpp:10
The primary namespace for the sigma library.
Definition operation_common.hpp:10
Uncertain< double > UDouble
Typedef for an uncertain double.
Definition uncertain.hpp:260
Uncertain< float > UFloat
Typedef for an uncertain float.
Definition uncertain.hpp:257
Uncertain< T > abs(const Uncertain< T > &a)
Absolute Value.
Definition basic.ipp:9