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