2021-03-19 21:23:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-19 21:23:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Concepts.h>
|
2021-07-17 16:29:28 +00:00
|
|
|
#include <AK/Math.h>
|
2021-03-19 21:23:48 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic T>
|
|
|
|
class [[gnu::packed]] Complex {
|
|
|
|
public:
|
|
|
|
constexpr Complex()
|
|
|
|
: m_real(0)
|
|
|
|
, m_imag(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Complex(T real)
|
|
|
|
: m_real(real)
|
|
|
|
, m_imag((T)0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Complex(T real, T imaginary)
|
|
|
|
: m_real(real)
|
|
|
|
, m_imag(imaginary)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-31 18:01:00 +00:00
|
|
|
constexpr T real() const noexcept { return m_real; }
|
2021-03-19 21:23:48 +00:00
|
|
|
|
2023-05-31 18:01:00 +00:00
|
|
|
constexpr T imag() const noexcept { return m_imag; }
|
2021-03-19 21:23:48 +00:00
|
|
|
|
2023-05-31 18:01:00 +00:00
|
|
|
constexpr T magnitude_squared() const noexcept { return m_real * m_real + m_imag * m_imag; }
|
2021-03-19 21:23:48 +00:00
|
|
|
|
2023-05-31 18:01:00 +00:00
|
|
|
constexpr T magnitude() const noexcept
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
2021-07-17 16:29:28 +00:00
|
|
|
return hypot(m_real, m_imag);
|
2021-03-19 21:23:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 18:01:00 +00:00
|
|
|
constexpr T phase() const noexcept
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
return atan2(m_imag, m_real);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U, AK::Concepts::Arithmetic V>
|
|
|
|
static constexpr Complex<T> from_polar(U magnitude, V phase)
|
|
|
|
{
|
2022-02-03 11:48:17 +00:00
|
|
|
V s, c;
|
|
|
|
sincos(phase, s, c);
|
|
|
|
return Complex<T>(magnitude * c, magnitude * s);
|
2021-03-19 21:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Complex<T>& operator=(Complex<U> const& other)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
m_real = other.real();
|
|
|
|
m_imag = other.imag();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr Complex<T>& operator=(U const& x)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
m_real = x;
|
|
|
|
m_imag = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Complex<T> operator+=(Complex<U> const& x)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
m_real += x.real();
|
|
|
|
m_imag += x.imag();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr Complex<T> operator+=(U const& x)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
2023-11-23 00:34:40 +00:00
|
|
|
m_real += x;
|
2021-03-19 21:23:48 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Complex<T> operator-=(Complex<U> const& x)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
m_real -= x.real();
|
|
|
|
m_imag -= x.imag();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr Complex<T> operator-=(U const& x)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
2023-11-23 00:34:40 +00:00
|
|
|
m_real -= x;
|
2021-03-19 21:23:48 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Complex<T> operator*=(Complex<U> const& x)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
const T real = m_real;
|
|
|
|
m_real = real * x.real() - m_imag * x.imag();
|
|
|
|
m_imag = real * x.imag() + m_imag * x.real();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr Complex<T> operator*=(U const& x)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
m_real *= x;
|
|
|
|
m_imag *= x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Complex<T> operator/=(Complex<U> const& x)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
const T real = m_real;
|
|
|
|
const T divisor = x.real() * x.real() + x.imag() * x.imag();
|
|
|
|
m_real = (real * x.real() + m_imag * x.imag()) / divisor;
|
|
|
|
m_imag = (m_imag * x.real() - x.real() * x.imag()) / divisor;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr Complex<T> operator/=(U const& x)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
m_real /= x;
|
|
|
|
m_imag /= x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Complex<T> operator+(Complex<U> const& a)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = *this;
|
|
|
|
x += a;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr Complex<T> operator+(U const& a)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = *this;
|
|
|
|
x += a;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Complex<T> operator-(Complex<U> const& a)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = *this;
|
|
|
|
x -= a;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr Complex<T> operator-(U const& a)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = *this;
|
|
|
|
x -= a;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Complex<T> operator*(Complex<U> const& a)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = *this;
|
|
|
|
x *= a;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr Complex<T> operator*(U const& a)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = *this;
|
|
|
|
x *= a;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Complex<T> operator/(Complex<U> const& a)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = *this;
|
|
|
|
x /= a;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr Complex<T> operator/(U const& a)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = *this;
|
|
|
|
x /= a;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr bool operator==(Complex<U> const& a) const
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
return (this->real() == a.real()) && (this->imag() == a.imag());
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Complex<T> operator+()
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Complex<T> operator-()
|
|
|
|
{
|
|
|
|
return Complex<T>(-m_real, -m_imag);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_real;
|
|
|
|
T m_imag;
|
|
|
|
};
|
|
|
|
|
|
|
|
// reverse associativity operators for scalars
|
|
|
|
template<AK::Concepts::Arithmetic T, AK::Concepts::Arithmetic U>
|
2023-11-23 00:34:40 +00:00
|
|
|
constexpr Complex<T> operator+(U const& a, Complex<T> const& b)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = a;
|
|
|
|
x += b;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic T, AK::Concepts::Arithmetic U>
|
2023-11-23 00:34:40 +00:00
|
|
|
constexpr Complex<T> operator-(U const& a, Complex<T> const& b)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = a;
|
|
|
|
x -= b;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic T, AK::Concepts::Arithmetic U>
|
2023-11-23 00:34:40 +00:00
|
|
|
constexpr Complex<T> operator*(U const& a, Complex<T> const& b)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = a;
|
|
|
|
x *= b;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic T, AK::Concepts::Arithmetic U>
|
2023-11-23 00:34:40 +00:00
|
|
|
constexpr Complex<T> operator/(U const& a, Complex<T> const& b)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
|
|
|
Complex<T> x = a;
|
|
|
|
x /= b;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// some identities
|
|
|
|
template<AK::Concepts::Arithmetic T>
|
|
|
|
static constinit Complex<T> complex_real_unit = Complex<T>((T)1, (T)0);
|
|
|
|
template<AK::Concepts::Arithmetic T>
|
|
|
|
static constinit Complex<T> complex_imag_unit = Complex<T>((T)0, (T)1);
|
|
|
|
|
|
|
|
template<AK::Concepts::Arithmetic T, AK::Concepts::Arithmetic U>
|
2022-04-01 17:58:27 +00:00
|
|
|
static constexpr bool approx_eq(Complex<T> const& a, Complex<U> const& b, double const margin = 0.000001)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
auto const x = const_cast<Complex<T>&>(a) - const_cast<Complex<U>&>(b);
|
2021-03-19 21:23:48 +00:00
|
|
|
return x.magnitude() <= margin;
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:12:03 +00:00
|
|
|
// complex version of exp()
|
2021-03-19 21:23:48 +00:00
|
|
|
template<AK::Concepts::Arithmetic T>
|
2022-04-01 17:58:27 +00:00
|
|
|
static constexpr Complex<T> cexp(Complex<T> const& a)
|
2021-03-19 21:23:48 +00:00
|
|
|
{
|
2021-07-17 16:29:28 +00:00
|
|
|
// FIXME: this can probably be faster and not use so many "expensive" trigonometric functions
|
|
|
|
return exp(a.real()) * Complex<T>(cos(a.imag()), sin(a.imag()));
|
2021-03-19 21:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 15:27:41 +00:00
|
|
|
template<AK::Concepts::Arithmetic T>
|
|
|
|
struct AK::Formatter<AK::Complex<T>> : Formatter<StringView> {
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, AK::Complex<T> c)
|
|
|
|
{
|
|
|
|
return Formatter<StringView>::format(builder, TRY(String::formatted("{}{:+}i", c.real(), c.imag())));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-31 18:01:00 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-07-17 16:29:28 +00:00
|
|
|
using AK::approx_eq;
|
|
|
|
using AK::cexp;
|
2021-03-19 21:23:48 +00:00
|
|
|
using AK::Complex;
|
|
|
|
using AK::complex_imag_unit;
|
|
|
|
using AK::complex_real_unit;
|
|
|
|
#endif
|