made Complex constexpr

This commit is contained in:
Ahmet Inan 2019-01-17 14:07:16 +01:00
commit 69c3080931

View file

@ -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();
}