Xped
Loading...
Searching...
No Matches
complex_var.hpp
Go to the documentation of this file.
1#ifndef XPED_COMPLEX_VAR_HPP_
2#define XPED_COMPLEX_VAR_HPP_
3
5#include "stan/math/rev/core/var.hpp"
6
7namespace stan::math {
8
9template <>
10class var_value<std::complex<double>, void>
11{
12public:
13 using value_type = std::complex<double>; // type in vari_value.
14 using vari_type = vari_value<value_type>;
15
23 vari_type* vi_;
24
34 inline bool is_uninitialized() { return (vi_ == nullptr); }
35
43 var_value()
44 : vi_(nullptr)
45 {}
46
54 template <typename S, require_convertible_t<S&, value_type>* = nullptr>
55 var_value(S x)
56 : vi_(new vari_type(x, false))
57 {} // NOLINT
58
63 var_value(vari_type* vi)
64 : vi_(vi)
65 {} // NOLINT
66
72 inline const auto& val() const noexcept { return vi_->val(); }
73
82 inline auto& adj() const noexcept { return vi_->adj(); }
83
92 inline auto& adj() noexcept { return vi_->adj_; }
93
103 void grad() { stan::math::grad(vi_); }
104
105 // POINTER OVERRIDES
106
119 inline vari_type& operator*() { return *vi_; }
120
131 inline vari_type* operator->() { return vi_; }
132
133 // COMPOUND ASSIGNMENT OPERATORS
134
145 inline var_value<value_type>& operator+=(const var_value<value_type>& b);
146
157 inline var_value<value_type>& operator+=(value_type b);
158
170 inline var_value<value_type>& operator-=(const var_value<value_type>& b);
171
183 inline var_value<value_type>& operator-=(value_type b);
184
196 inline var_value<value_type>& operator*=(const var_value<value_type>& b);
197
209 inline var_value<value_type>& operator*=(value_type b);
210
221 inline var_value<value_type>& operator/=(const var_value<value_type>& b);
222
234 inline var_value<value_type>& operator/=(value_type b);
235
244 friend std::ostream& operator<<(std::ostream& os, const var_value<value_type>& v)
245 {
246 if(v.vi_ == nullptr) { return os << "uninitialized"; }
247 return os << v.val();
248 }
249};
250
251inline var_value<std::complex<double>> operator/(const var_value<std::complex<double>>& dividend, const var_value<std::complex<double>>& divisor)
252{
253 return make_callback_var(dividend.val() / divisor.val(), [dividend, divisor](auto&& vi) {
254 dividend.adj() += vi.adj() / divisor.val();
255 divisor.adj() -= vi.adj() * dividend.val() / (divisor.val() * divisor.val());
256 });
257}
258
259} // namespace stan::math
260
261namespace std {
262
263stan::math::var_value<double> real(const stan::math::var_value<std::complex<double>>& z)
264{
265 return stan::math::make_callback_var(z.val().real(), [z](auto&& vi) { z.adj() += vi.adj(); });
266}
267
268} // namespace std
269
270#endif
XTensor< TRACK, Scalar, Rank, CoRank, Symmetry > operator*(const Tensor< Scalar, Rank, CoRank, Symmetry, true > &t, Scalar s)
Definition: ADTensor.hpp:452
std::ostream & operator<<(std::ostream &os, const FusionTree< depth, Symmetry > &tree)
Definition: FusionTree.hpp:93