mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
made Complex constexpr
This commit is contained in:
parent
79a6a85b47
commit
69c3080931
1 changed files with 17 additions and 17 deletions
34
complex.hh
34
complex.hh
|
|
@ -13,11 +13,11 @@ class Complex
|
|||
T re, im;
|
||||
public:
|
||||
typedef T value_type;
|
||||
Complex() : re(0), im(0) {}
|
||||
Complex(T r) : re(r), im(0) {}
|
||||
Complex(T r, T i) : re(r), im(i) {}
|
||||
inline T real() const { return re; }
|
||||
inline T imag() const { return im; }
|
||||
constexpr Complex() : re(0), im(0) {}
|
||||
constexpr Complex(T r) : re(r), im(0) {}
|
||||
constexpr Complex(T r, T i) : re(r), im(i) {}
|
||||
constexpr T real() const { return re; }
|
||||
constexpr T imag() const { return im; }
|
||||
inline void real(T r) { re = r; }
|
||||
inline void imag(T i) { im = i; }
|
||||
inline Complex<T> operator = (T a)
|
||||
|
|
@ -53,74 +53,74 @@ public:
|
|||
};
|
||||
|
||||
template <typename T>
|
||||
static inline Complex<T> operator + (Complex<T> a, Complex<T> b)
|
||||
static constexpr Complex<T> operator + (Complex<T> a, Complex<T> b)
|
||||
{
|
||||
return Complex<T>(a.real() + b.real(), a.imag() + b.imag());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Complex<T> operator + (Complex<T> a)
|
||||
static constexpr Complex<T> operator + (Complex<T> a)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Complex<T> operator - (Complex<T> a, Complex<T> b)
|
||||
static constexpr Complex<T> operator - (Complex<T> a, Complex<T> b)
|
||||
{
|
||||
return Complex<T>(a.real() - b.real(), a.imag() - b.imag());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Complex<T> operator - (Complex<T> a)
|
||||
static constexpr Complex<T> operator - (Complex<T> a)
|
||||
{
|
||||
return Complex<T>(-a.real(), -a.imag());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Complex<T> operator * (T a, Complex<T> b)
|
||||
static constexpr Complex<T> operator * (T a, Complex<T> b)
|
||||
{
|
||||
return Complex<T>(a * b.real(), a * b.imag());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Complex<T> operator / (Complex<T> a, T b)
|
||||
static constexpr Complex<T> operator / (Complex<T> a, T b)
|
||||
{
|
||||
return Complex<T>(a.real() / b, a.imag() / b);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Complex<T> operator * (Complex<T> a, Complex<T> b)
|
||||
static constexpr Complex<T> operator * (Complex<T> a, Complex<T> b)
|
||||
{
|
||||
return Complex<T>(a.real() * b.real() - a.imag() * b.imag(), a.real() * b.imag() + a.imag() * b.real());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Complex<T> operator / (Complex<T> a, Complex<T> b)
|
||||
static constexpr Complex<T> operator / (Complex<T> a, Complex<T> b)
|
||||
{
|
||||
return Complex<T>((a.real() * b.real() + a.imag() * b.imag()) / (b.real() * b.real() + b.imag() * b.imag()),
|
||||
(a.imag() * b.real() - a.real() * b.imag()) / (b.real() * b.real() + b.imag() * b.imag()));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Complex<T> exp(Complex<T> a)
|
||||
static constexpr Complex<T> exp(Complex<T> a)
|
||||
{
|
||||
return Complex<T>(exp(a.real()) * cos(a.imag()), exp(a.real()) * sin(a.imag()));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline T abs(Complex<T> a)
|
||||
static constexpr T abs(Complex<T> a)
|
||||
{
|
||||
return hypot(a.real(), a.imag());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline T arg(Complex<T> a)
|
||||
static constexpr T arg(Complex<T> a)
|
||||
{
|
||||
return atan2(a.imag(), a.real());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline T norm(Complex<T> a)
|
||||
static constexpr T norm(Complex<T> a)
|
||||
{
|
||||
return a.real() * a.real() + a.imag() * a.imag();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue