mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-08 06:09:58 +00:00
AK: Remove unused Complex.h
This commit is contained in:
parent
fe9af7c972
commit
b88e0eb50a
Notes:
sideshowbarker
2024-07-16 21:45:42 +09:00
Author: https://github.com/awesomekling
Commit: b88e0eb50a
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/197
Reviewed-by: https://github.com/ADKaster
5 changed files with 0 additions and 558 deletions
294
AK/Complex.h
294
AK/Complex.h
|
|
@ -1,294 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Concepts.h>
|
||||
#include <AK/Math.h>
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr T real() const noexcept { return m_real; }
|
||||
|
||||
constexpr T imag() const noexcept { return m_imag; }
|
||||
|
||||
constexpr T magnitude_squared() const noexcept { return m_real * m_real + m_imag * m_imag; }
|
||||
|
||||
constexpr T magnitude() const noexcept
|
||||
{
|
||||
return hypot(m_real, m_imag);
|
||||
}
|
||||
|
||||
constexpr T phase() const noexcept
|
||||
{
|
||||
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)
|
||||
{
|
||||
V s, c;
|
||||
sincos(phase, s, c);
|
||||
return Complex<T>(magnitude * c, magnitude * s);
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T>& operator=(Complex<U> const& other)
|
||||
{
|
||||
m_real = other.real();
|
||||
m_imag = other.imag();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T>& operator=(U const& x)
|
||||
{
|
||||
m_real = x;
|
||||
m_imag = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator+=(Complex<U> const& x)
|
||||
{
|
||||
m_real += x.real();
|
||||
m_imag += x.imag();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator+=(U const& x)
|
||||
{
|
||||
m_real += x;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator-=(Complex<U> const& x)
|
||||
{
|
||||
m_real -= x.real();
|
||||
m_imag -= x.imag();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator-=(U const& x)
|
||||
{
|
||||
m_real -= x;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator*=(Complex<U> const& x)
|
||||
{
|
||||
T const 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>
|
||||
constexpr Complex<T> operator*=(U const& x)
|
||||
{
|
||||
m_real *= x;
|
||||
m_imag *= x;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator/=(Complex<U> const& x)
|
||||
{
|
||||
T const real = m_real;
|
||||
T const 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>
|
||||
constexpr Complex<T> operator/=(U const& x)
|
||||
{
|
||||
m_real /= x;
|
||||
m_imag /= x;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator+(Complex<U> const& a)
|
||||
{
|
||||
Complex<T> x = *this;
|
||||
x += a;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator+(U const& a)
|
||||
{
|
||||
Complex<T> x = *this;
|
||||
x += a;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator-(Complex<U> const& a)
|
||||
{
|
||||
Complex<T> x = *this;
|
||||
x -= a;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator-(U const& a)
|
||||
{
|
||||
Complex<T> x = *this;
|
||||
x -= a;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator*(Complex<U> const& a)
|
||||
{
|
||||
Complex<T> x = *this;
|
||||
x *= a;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator*(U const& a)
|
||||
{
|
||||
Complex<T> x = *this;
|
||||
x *= a;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator/(Complex<U> const& a)
|
||||
{
|
||||
Complex<T> x = *this;
|
||||
x /= a;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator/(U const& a)
|
||||
{
|
||||
Complex<T> x = *this;
|
||||
x /= a;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic U>
|
||||
constexpr bool operator==(Complex<U> const& a) const
|
||||
{
|
||||
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>
|
||||
constexpr Complex<T> operator+(U const& a, Complex<T> const& b)
|
||||
{
|
||||
Complex<T> x = a;
|
||||
x += b;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic T, AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator-(U const& a, Complex<T> const& b)
|
||||
{
|
||||
Complex<T> x = a;
|
||||
x -= b;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic T, AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator*(U const& a, Complex<T> const& b)
|
||||
{
|
||||
Complex<T> x = a;
|
||||
x *= b;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<AK::Concepts::Arithmetic T, AK::Concepts::Arithmetic U>
|
||||
constexpr Complex<T> operator/(U const& a, Complex<T> const& b)
|
||||
{
|
||||
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>
|
||||
static constexpr bool approx_eq(Complex<T> const& a, Complex<U> const& b, double const margin = 0.000001)
|
||||
{
|
||||
auto const x = const_cast<Complex<T>&>(a) - const_cast<Complex<U>&>(b);
|
||||
return x.magnitude() <= margin;
|
||||
}
|
||||
|
||||
// complex version of exp()
|
||||
template<AK::Concepts::Arithmetic T>
|
||||
static constexpr Complex<T> cexp(Complex<T> const& a)
|
||||
{
|
||||
// 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()));
|
||||
}
|
||||
}
|
||||
|
||||
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())));
|
||||
}
|
||||
};
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::approx_eq;
|
||||
using AK::cexp;
|
||||
using AK::Complex;
|
||||
using AK::complex_imag_unit;
|
||||
using AK::complex_real_unit;
|
||||
#endif
|
||||
|
|
@ -45,7 +45,6 @@ shared_library("AK") {
|
|||
"CircularBuffer.cpp",
|
||||
"CircularBuffer.h",
|
||||
"CircularQueue.h",
|
||||
"Complex.h",
|
||||
"Concepts.h",
|
||||
"ConstrainedStream.cpp",
|
||||
"ConstrainedStream.h",
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ tests = [
|
|||
"TestChecked",
|
||||
"TestCircularBuffer",
|
||||
"TestCircularQueue",
|
||||
"TestComplex",
|
||||
"TestByteString",
|
||||
"TestDisjointChunks",
|
||||
"TestDistinctNumeric",
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ set(AK_TEST_SOURCES
|
|||
TestChecked.cpp
|
||||
TestCircularBuffer.cpp
|
||||
TestCircularQueue.cpp
|
||||
TestComplex.cpp
|
||||
TestDisjointChunks.cpp
|
||||
TestDistinctNumeric.cpp
|
||||
TestDoublyLinkedList.cpp
|
||||
|
|
|
|||
|
|
@ -1,261 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/Complex.h>
|
||||
|
||||
using namespace Test::Randomized;
|
||||
|
||||
namespace {
|
||||
|
||||
Complex<f64> gen_complex()
|
||||
{
|
||||
auto r = Gen::number_f64();
|
||||
auto i = Gen::number_f64();
|
||||
return Complex<f64>(r, i);
|
||||
}
|
||||
|
||||
Complex<f64> gen_complex(f64 min, f64 max)
|
||||
{
|
||||
auto r = Gen::number_f64(min, max);
|
||||
auto i = Gen::number_f64(min, max);
|
||||
return Complex<f64>(r, i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void expect_approximate_complex(Complex<T> a, Complex<T> b)
|
||||
{
|
||||
EXPECT_APPROXIMATE(a.real(), b.real());
|
||||
EXPECT_APPROXIMATE(a.imag(), b.imag());
|
||||
}
|
||||
|
||||
TEST_CASE(Complex)
|
||||
{
|
||||
auto a = Complex<float> { 1.f, 1.f };
|
||||
auto b = complex_real_unit<double> + Complex<double> { 0, 1 } * 1;
|
||||
EXPECT_APPROXIMATE(a.real(), b.real());
|
||||
EXPECT_APPROXIMATE(a.imag(), b.imag());
|
||||
|
||||
#ifdef AKCOMPLEX_CAN_USE_MATH_H
|
||||
EXPECT_APPROXIMATE((complex_imag_unit<float> - complex_imag_unit<float>).magnitude(), 0);
|
||||
EXPECT_APPROXIMATE((complex_imag_unit<float> + complex_real_unit<float>).magnitude(), sqrt(2));
|
||||
|
||||
auto c = Complex<double> { 0., 1. };
|
||||
auto d = Complex<double>::from_polar(1., M_PI / 2.);
|
||||
EXPECT_APPROXIMATE(c.real(), d.real());
|
||||
EXPECT_APPROXIMATE(c.imag(), d.imag());
|
||||
|
||||
c = Complex<double> { -1., 1. };
|
||||
d = Complex<double>::from_polar(sqrt(2.), 3. * M_PI / 4.);
|
||||
EXPECT_APPROXIMATE(c.real(), d.real());
|
||||
EXPECT_APPROXIMATE(c.imag(), d.imag());
|
||||
EXPECT_APPROXIMATE(d.phase(), 3. * M_PI / 4.);
|
||||
EXPECT_APPROXIMATE(c.magnitude(), d.magnitude());
|
||||
EXPECT_APPROXIMATE(c.magnitude(), sqrt(2.));
|
||||
#endif
|
||||
EXPECT_EQ((complex_imag_unit<double> * complex_imag_unit<double>).real(), -1.);
|
||||
EXPECT_EQ((complex_imag_unit<double> / complex_imag_unit<double>).real(), 1.);
|
||||
|
||||
EXPECT_EQ(Complex(1., 10.) == (Complex<double>(1., 0.) + Complex(0., 10.)), true);
|
||||
EXPECT_EQ(Complex(1., 10.) != (Complex<double>(1., 1.) + Complex(0., 10.)), true);
|
||||
#ifdef AKCOMPLEX_CAN_USE_MATH_H
|
||||
EXPECT_EQ(approx_eq(Complex<int>(1), Complex<float>(1.0000004f)), true);
|
||||
EXPECT_APPROXIMATE(cexp(Complex<double>(0., 1.) * M_PI).real(), -1.);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE(real_operators_regression)
|
||||
{
|
||||
{
|
||||
auto c = Complex(0., 0.);
|
||||
c += 1;
|
||||
EXPECT_EQ(c.real(), 1);
|
||||
}
|
||||
{
|
||||
auto c = Complex(0., 0.);
|
||||
c -= 1;
|
||||
EXPECT_EQ(c.real(), -1);
|
||||
}
|
||||
{
|
||||
auto c1 = Complex(1., 1.);
|
||||
auto c2 = 1 - c1;
|
||||
EXPECT_EQ(c2.real(), 0);
|
||||
EXPECT_EQ(c2.imag(), -1);
|
||||
}
|
||||
{
|
||||
auto c1 = Complex(1., 1.);
|
||||
auto c2 = 1 / c1;
|
||||
EXPECT_EQ(c2.real(), 0.5);
|
||||
EXPECT_EQ(c2.imag(), -0.5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(constructor_0_is_origin)
|
||||
{
|
||||
auto c = Complex<f64>();
|
||||
EXPECT_EQ(c.real(), 0L);
|
||||
EXPECT_EQ(c.imag(), 0L);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(constructor_1)
|
||||
{
|
||||
GEN(r, Gen::number_f64());
|
||||
auto c = Complex<f64>(r);
|
||||
EXPECT_EQ(c.real(), r);
|
||||
EXPECT_EQ(c.imag(), 0L);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(constructor_2)
|
||||
{
|
||||
GEN(r, Gen::number_f64());
|
||||
GEN(i, Gen::number_f64());
|
||||
auto c = Complex<f64>(r, i);
|
||||
EXPECT_EQ(c.real(), r);
|
||||
EXPECT_EQ(c.imag(), i);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(magnitude_squared)
|
||||
{
|
||||
GEN(c, gen_complex());
|
||||
auto magnitude_squared = c.magnitude_squared();
|
||||
auto magnitude = c.magnitude();
|
||||
EXPECT_APPROXIMATE(magnitude_squared, magnitude * magnitude);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(from_polar_magnitude)
|
||||
{
|
||||
// Magnitude only makes sense non-negative, but the library allows it to be negative.
|
||||
GEN(m, Gen::number_f64(-1000, 1000));
|
||||
GEN(p, Gen::number_f64(-1000, 1000));
|
||||
auto c = Complex<f64>::from_polar(m, p);
|
||||
EXPECT_APPROXIMATE(c.magnitude(), abs(m));
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(from_polar_phase)
|
||||
{
|
||||
// To have a meaningful phase, magnitude needs to be >0.
|
||||
GEN(m, Gen::number_f64(1, 1000));
|
||||
GEN(p, Gen::number_f64(-1000, 1000));
|
||||
|
||||
auto c = Complex<f64>::from_polar(m, p);
|
||||
|
||||
// Returned phase is in the (-pi,pi] interval.
|
||||
// We need to mod from our randomly generated [-1000,1000] interval]
|
||||
// down to [0,2pi) or (-2pi,0] depending on our sign.
|
||||
// Then we can adjust and get into the -pi..pi range by adding/subtracting
|
||||
// one last 2pi.
|
||||
auto wanted_p = fmod(p, 2 * M_PI);
|
||||
if (wanted_p > M_PI)
|
||||
wanted_p -= 2 * M_PI;
|
||||
else if (wanted_p < -M_PI)
|
||||
wanted_p += 2 * M_PI;
|
||||
|
||||
EXPECT_APPROXIMATE(c.phase(), wanted_p);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(imag_untouched_c_plus_r)
|
||||
{
|
||||
GEN(c1, gen_complex());
|
||||
GEN(r2, Gen::number_f64());
|
||||
auto c2 = c1 + r2;
|
||||
EXPECT_EQ(c2.imag(), c1.imag());
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(imag_untouched_c_minus_r)
|
||||
{
|
||||
GEN(c1, gen_complex());
|
||||
GEN(r2, Gen::number_f64());
|
||||
auto c2 = c1 - r2;
|
||||
EXPECT_EQ(c2.imag(), c1.imag());
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(assignment_same_as_binop_plus)
|
||||
{
|
||||
GEN(c1, gen_complex());
|
||||
GEN(c2, gen_complex());
|
||||
auto out1 = c1 + c2;
|
||||
auto out2 = c1;
|
||||
out2 += c2;
|
||||
EXPECT_EQ(out2, out1);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(assignment_same_as_binop_minus)
|
||||
{
|
||||
GEN(c1, gen_complex());
|
||||
GEN(c2, gen_complex());
|
||||
auto out1 = c1 - c2;
|
||||
auto out2 = c1;
|
||||
out2 -= c2;
|
||||
EXPECT_EQ(out2, out1);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(assignment_same_as_binop_mult)
|
||||
{
|
||||
GEN(c1, gen_complex(-1000, 1000));
|
||||
GEN(c2, gen_complex(-1000, 1000));
|
||||
auto out1 = c1 * c2;
|
||||
auto out2 = c1;
|
||||
out2 *= c2;
|
||||
EXPECT_EQ(out2, out1);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(assignment_same_as_binop_div)
|
||||
{
|
||||
GEN(c1, gen_complex(-1000, 1000));
|
||||
GEN(c2, gen_complex(-1000, 1000));
|
||||
auto out1 = c1 / c2;
|
||||
auto out2 = c1;
|
||||
out2 /= c2;
|
||||
EXPECT_EQ(out2, out1);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(commutativity_c_c)
|
||||
{
|
||||
GEN(c1, gen_complex());
|
||||
GEN(c2, gen_complex());
|
||||
expect_approximate_complex(c1 + c2, c2 + c1);
|
||||
expect_approximate_complex(c1 * c2, c2 * c1);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(commutativity_c_r)
|
||||
{
|
||||
GEN(c, gen_complex());
|
||||
GEN(r, Gen::number_f64());
|
||||
expect_approximate_complex(r + c, c + r);
|
||||
expect_approximate_complex(r * c, c * r);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(unary_plus_noop)
|
||||
{
|
||||
GEN(c, gen_complex());
|
||||
EXPECT_EQ(+c, c);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(unary_minus_inverse)
|
||||
{
|
||||
GEN(c, gen_complex());
|
||||
expect_approximate_complex(-(-c), c);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(wrapping_real)
|
||||
{
|
||||
GEN(c, gen_complex(-1000, 1000));
|
||||
GEN(r, Gen::number_f64(-1000, 1000));
|
||||
auto cr = Complex<f64>(r);
|
||||
|
||||
expect_approximate_complex(r + c, cr + c);
|
||||
expect_approximate_complex(r - c, cr - c);
|
||||
expect_approximate_complex(r * c, cr * c);
|
||||
expect_approximate_complex(r / c, cr / c);
|
||||
|
||||
expect_approximate_complex(c + r, c + cr);
|
||||
expect_approximate_complex(c - r, c - cr);
|
||||
expect_approximate_complex(c * r, c * cr);
|
||||
expect_approximate_complex(c / r, c / cr);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue