Automatic Differentiation
 
Loading...
Searching...
No Matches
complex_base.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CORE_COMPLEX_BASE_HPP
2#define STAN_MATH_PRIM_CORE_COMPLEX_BASE_HPP
3
6#include <complex>
7
8namespace stan {
9namespace math {
10
17template <typename ValueType>
19 public:
23 using value_type = ValueType;
24
28 using complex_type = std::complex<value_type>;
29
33 complex_base() = default;
34
42 template <typename U> // , typename = require_stan_scalar_t<U>>
43 complex_base(const U& re) : re_(re) {} // NOLINT(runtime/explicit)
44
54 template <typename U, typename V>
55 complex_base(const U& re, const V& im) : re_(re), im_(im) {}
56
62 value_type real() const { return re_; }
63
69 void real(const value_type& re) { re_ = re; }
70
76 value_type imag() const { return im_; }
77
83 void imag(const value_type& im) { im_ = im; }
84
93 template <typename U, typename = require_stan_scalar_t<U>>
95 re_ = re;
96 im_ = 0;
97 return derived();
98 }
99
107 template <typename U>
109 re_ += x;
110 return derived();
111 }
112
120 template <typename U>
121 complex_type& operator+=(const std::complex<U>& other) {
122 re_ += other.real();
123 im_ += other.imag();
124 return derived();
125 }
126
134 template <typename U>
136 re_ -= x;
137 return derived();
138 }
139
147 template <typename U>
148 complex_type& operator-=(const std::complex<U>& other) {
149 re_ -= other.real();
150 im_ -= other.imag();
151 return derived();
152 }
153
161 template <typename U>
163 re_ *= x;
164 im_ *= x;
165 return derived();
166 }
167
175 template <typename U>
176 complex_type& operator*=(const std::complex<U>& other) {
177 value_type re_temp = re_ * other.real() - im_ * other.imag();
178 im_ = re_ * other.imag() + other.real() * im_;
179 re_ = re_temp;
180 return derived();
181 }
182
190 template <typename U>
192 re_ /= x;
193 im_ /= x;
194 return derived();
195 }
196
204 template <typename U>
205 complex_type& operator/=(const std::complex<U>& other) {
206 using stan::math::square;
207 value_type sum_sq_im
208 = (other.real() * other.real()) + (other.imag() * other.imag());
209 value_type re_temp = (re_ * other.real() + im_ * other.imag()) / sum_sq_im;
210 im_ = (im_ * other.real() - re_ * other.imag()) / sum_sq_im;
211 re_ = re_temp;
212 return derived();
213 }
214
215 protected:
220
225
231 complex_type& derived() { return static_cast<complex_type&>(*this); }
232};
233
234} // namespace math
235} // namespace stan
236
237#endif
complex_type & operator=(U &&re)
Assign the specified value to the real part of this complex number and set imaginary part to zero.
value_type imag() const
Return the imaginary part.
complex_type & operator-=(const std::complex< U > &other)
Subtracts specified complex number from this.
complex_type & operator+=(const std::complex< U > &other)
Adds specified complex number to this.
complex_type & operator+=(const U &x)
Add specified real value to real part.
value_type real() const
Return the real part.
complex_base()=default
Construct a complex base with zero real and imaginary parts.
complex_type & operator-=(const U &x)
Subtracts specified real number from real part.
complex_type & operator/=(const U &x)
Divides this by the specified real number.
complex_base(const U &re, const V &im)
Construct a complex base with the specified real and imaginary parts.
complex_type & operator*=(const std::complex< U > &other)
Multiplies this by specified complex number.
complex_type & derived()
Return this complex base cast to the complex type.
complex_type & operator*=(const U &x)
Multiplies this by the specified real number.
value_type im_
Imaginary part.
complex_base(const U &re)
Construct a complex base with the specified real part and a zero imaginary part.
std::complex< value_type > complex_type
Derived complex type used for function return types.
complex_type & operator/=(const std::complex< U > &other)
Divides this by the specified complex number.
void real(const value_type &re)
Set the real part to the specified value.
void imag(const value_type &im)
Set the imaginary part to the specified value.
ValueType value_type
Type of real and imaginary parts.
value_type re_
Real part.
Base class for complex numbers.
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...