sigma  0.0.0
Loading...
Searching...
No Matches
setter.hpp
Go to the documentation of this file.
1#pragma once
2#include "sigma/uncertain.hpp"
3
7
8namespace sigma::detail_ {
9
18template<typename UncertainType>
19class Setter {
20public:
23
25 using uncertain_t = UncertainType;
26
28 using value_t = typename uncertain_t::value_t;
29
31 using dep_sd_t = typename uncertain_t::dep_sd_t;
32
34 using dep_sd_ptr = typename uncertain_t::dep_sd_ptr;
35
37 using deps_map_t = typename uncertain_t::deps_map_t;
38
45 Setter(uncertain_t& u) : m_x_(u) {}
46
53 void update_mean(value_t mean) { m_x_.m_mean_ = mean; }
54
60 void update_sd() {
61 m_x_.m_sd_ = 0.0;
62 std::vector<dep_sd_ptr> zero_contributions{};
63 for(const auto& [dep, deriv] : m_x_.m_deps_) {
64 if(deriv == 0.0) {
65 zero_contributions.emplace_back(dep);
66 continue;
67 }
68 m_x_.m_sd_ += std::pow(*dep.get() * deriv, 2.0);
69 }
70 for(const auto& dep : zero_contributions) { m_x_.m_deps_.erase(dep); }
71 m_x_.m_sd_ = std::sqrt(m_x_.m_sd_);
72 }
73
84 void update_derivatives(value_t dxda, bool call_update_std = true) {
85 if(dxda != 1.0) {
86 for(const auto& [dep, deriv] : m_x_.m_deps_) {
87 m_x_.m_deps_[dep] *= dxda;
88 }
89 }
90 if(call_update_std) update_sd();
91 }
92
105 void update_derivatives(const deps_map_t& deps, value_t dxda,
106 bool call_update_std = true) {
107 for(const auto& [dep, deriv] : deps) {
108 auto new_deriv = dxda * deriv;
109 if(m_x_.m_deps_.count(dep) != 0) {
110 m_x_.m_deps_[dep] += new_deriv;
111 } else {
112 m_x_.m_deps_.emplace(std::make_pair(std::move(dep), new_deriv));
113 }
114 }
115 if(call_update_std) update_sd();
116 }
117
118private:
120 uncertain_t& m_x_;
121};
122
123} // namespace sigma::detail_
UncertainType uncertain_t
The numeric type of the variable.
Definition setter.hpp:25
typename uncertain_t::dep_sd_ptr dep_sd_ptr
A pointer to a dependency of this variable.
Definition setter.hpp:34
Setter(uncertain_t &u)
Construct a Setter for a variable.
Definition setter.hpp:45
void update_sd()
Calculate the standatd deviation of m_x_ based on the uncertainty of its dependencies.
Definition setter.hpp:60
void update_derivatives(value_t dxda, bool call_update_std=true)
Update of existing derivatives.
Definition setter.hpp:84
Setter< UncertainType > my_t
Type of the instance.
Definition setter.hpp:22
void update_mean(value_t mean)
Update the mean of the wrapped variable.
Definition setter.hpp:53
typename uncertain_t::value_t value_t
The type of the values of the variable.
Definition setter.hpp:28
void update_derivatives(const deps_map_t &deps, value_t dxda, bool call_update_std=true)
Update/addition of derivatives.
Definition setter.hpp:105
typename uncertain_t::dep_sd_t dep_sd_t
The type of a standard deviation that this instance depends on.
Definition setter.hpp:31
typename uncertain_t::deps_map_t deps_map_t
The type of the map holding the variable's dependencies.
Definition setter.hpp:37
The namespace that contains the implementation details of the library.
Definition operation_common.hpp:10
Defines the Uncertain class.