sigma  1.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 <memory>
5#include <type_traits>
6#include <typeinfo>
7#include <unordered_map>
8#include <utility>
9
13
14namespace sigma {
15
16// Foward Declaration
17namespace detail_ {
18template<typename T>
19class Setter; // Class to alter values inside Uncertain
20} // namespace detail_
21
33template<typename ValueType>
34class Uncertain {
35public:
38
40 using value_t = ValueType;
41
44
46 using dep_sd_ptr = std::shared_ptr<dep_sd_t>;
47
49 using deps_map_t = std::unordered_map<dep_sd_ptr, value_t>;
50
63 value_t threshold = std::numeric_limits<value_t>::epsilon());
64
71 value_t mean() const { return m_mean_; }
72
79 value_t sd() const { return m_sd_; }
80
87 const deps_map_t& deps() const { return m_deps_; }
88
95 value_t threshold() const { return m_threshold_; }
96
97private:
99 value_t m_mean_;
100
102 value_t m_sd_;
103
105 value_t m_threshold_;
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_(std::abs(mean) < threshold ? 0 : mean),
125 m_sd_(std::abs(sd) < threshold ? 0 : std::abs(sd)),
126 m_threshold_(threshold) {
127 if(m_sd_ > 0.0) {
128 m_deps_.emplace(std::make_shared<dep_sd_t>(m_sd_), value_t{1.0});
129 }
130}
131
132// -- Utility functions --------------------------------------------------------
133
146template<typename ValueType>
147std::ostream& operator<<(std::ostream& os, const Uncertain<ValueType>& u) {
148 os << u.mean() << "+/-" << u.sd();
149 return os;
150}
151
162template<typename ValueType1, typename ValueType2>
164 const Uncertain<ValueType2>& rhs) {
165 if constexpr(!std::is_same_v<ValueType1, ValueType2>) {
166 return false;
167 } else {
168 if(lhs.mean() != rhs.mean()) return false;
169 if(lhs.sd() != rhs.sd()) return false;
170 if(lhs.deps() != rhs.deps()) return false;
171 if(lhs.threshold() != rhs.threshold()) return false;
172 return true;
173 }
174}
175
186template<typename ValueType1, typename ValueType2>
188 const Uncertain<ValueType2>& rhs) {
189 return !(lhs == rhs);
190}
191
204template<typename ValueType1, typename ValueType2>
206 const Uncertain<ValueType2>& rhs) {
207 return lhs.mean() < rhs.mean();
208}
209
222template<typename ValueType1, typename ValueType2>
224 const Uncertain<ValueType2>& rhs) {
225 return rhs < lhs;
226}
227
238template<typename ValueType1, typename ValueType2>
240 const Uncertain<ValueType2>& rhs) {
241 return (lhs == rhs) || (lhs < rhs);
242}
243
254template<typename ValueType1, typename ValueType2>
256 const Uncertain<ValueType2>& rhs) {
257 return (lhs == rhs) || (lhs > rhs);
258}
259
262
265
266} // namespace sigma
267
Models an unceratin variable.
Definition uncertain.hpp:34
std::ostream & operator<<(std::ostream &os, const Uncertain< ValueType > &u)
Overload stream insertion to print uncertain variable.
Definition uncertain.hpp:147
bool operator<=(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Whether one uncertain variable is less than or equal to another.
Definition uncertain.hpp:239
bool operator>(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Whether one uncertain variable is greater than another.
Definition uncertain.hpp:223
value_t mean() const
Definition uncertain.hpp:71
bool operator>=(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Whether one uncertain variable is greater than or equal to another.
Definition uncertain.hpp:255
value_t dep_sd_t
Definition uncertain.hpp:43
bool operator<(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Whether one uncertain variable is less than another.
Definition uncertain.hpp:205
const deps_map_t & deps() const
Get the dependencies of the variable.
Definition uncertain.hpp:87
Uncertain< float > my_t
Definition uncertain.hpp:37
bool operator==(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Compare two uncertain variables for equality.
Definition uncertain.hpp:163
bool operator!=(const Uncertain< ValueType1 > &lhs, const Uncertain< ValueType2 > &rhs)
Compare two uncertain variables for inequality.
Definition uncertain.hpp:187
float value_t
Definition uncertain.hpp:40
std::unordered_map< dep_sd_ptr, value_t > deps_map_t
Definition uncertain.hpp:49
value_t sd() const
Definition uncertain.hpp:79
std::shared_ptr< dep_sd_t > dep_sd_ptr
Definition uncertain.hpp:46
Uncertain(value_t mean=0.0, value_t sd=0.0, value_t threshold=std::numeric_limits< value_t >::epsilon())
Construct an uncertain value from mean and standard deviation.
Definition uncertain.hpp:123
value_t threshold() const
Definition uncertain.hpp:95
Modifies an unceratin variable.
Definition setter.hpp:20
The namespace that contains the implementation details of the library.
The primary namespace for the sigma library.
Definition affine.hpp:12
Uncertain< double > UDouble
Typedef for an uncertain double.
Definition uncertain.hpp:264
Affine< T > abs(const Affine< T > &a)
Absolute Value of an affine form.
Definition basic.ipp:8
Uncertain< float > UFloat
Typedef for an uncertain float.
Definition uncertain.hpp:261
Components for compatibility with Eigen.
Convenience header for uncertain value operations.