use core::ops::*;
pub type Complex32 = Complex<f32>;
pub type Complex64 = Complex<f64>;
use crate::traits::Number;
use super::ComplexPolar as Polar;
#[inline(always)]
#[must_use]
pub const fn complex<FT>(re: FT, im: FT) -> Complex<FT> {
Complex::new(re, im)
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
#[repr(C)]
pub struct Complex<FT> {
pub re: FT,
pub im: FT,
}
impl<FT> Complex<FT> {
pub const fn new(re: FT, im: FT) -> Self {
Self { re, im }
}
}
impl<FT: Number> Complex<FT> {
pub const ZERO: Self = Self::new(FT::ZERO, FT::ZERO);
pub const ONE: Self = Self::new(FT::ONE, FT::ZERO);
pub const I: Self = Self::new(FT::ZERO, FT::ONE);
pub fn conjugate(self) -> Self {
Self::new(self.re, -self.im)
}
pub fn abs(self) -> FT {
self.abs_sq().sqrt()
}
pub fn square(mut self) -> Self {
let two = FT::ONE + FT::ONE;
let re = self.re * self.re - self.im * self.im;
self.im = self.re * self.im * two;
self.re = re;
self
}
pub fn abs_sq(self) -> FT {
self.re * self.re + self.im * self.im
}
pub fn arg(self) -> FT {
self.im.atan2(self.re)
}
pub fn recip(self) -> Self {
self.conjugate() / self.abs_sq()
}
pub fn to_polar(self) -> Polar<FT> {
Polar::new(self.abs(), self.arg())
}
pub fn exp(self) -> Polar<FT> {
Polar::new(self.re.exp(), self.im)
}
pub fn exp2(self) -> Polar<FT> {
Polar::new(self.re.exp2(), self.im * FT::LN_2())
}
pub fn ln(self) -> Self {
self.to_polar().ln()
}
pub fn ln_1p(self) -> Self {
let two = FT::ONE + FT::ONE;
let re = (two * self.re + self.abs_sq()).ln_1p() / two;
let im = (self + FT::ONE).arg();
Self::new(re, im)
}
pub fn log2(self) -> Self {
self.ln() / FT::LN_2()
}
pub fn log10(self) -> Self {
self.ln() / FT::LN_10()
}
pub fn ln_branch(self, k: i32) -> Self {
self.to_polar().ln_branch(k)
}
pub fn ln_1p_branch(self, k: i32) -> Self {
let p = self.ln_1p();
Self::new(p.re, p.im + FT::TAU() * FT::from_i32(k))
}
pub fn log2_branch(self, k: i32) -> Self {
self.to_polar().log2_branch(k)
}
pub fn log10_branch(self, k: i32) -> Self {
self.to_polar().log10_branch(k)
}
pub fn sqrt_branch(self, k: i32) -> Polar<FT> {
self.to_polar().sqrt_branch(k)
}
pub fn nth_root(self, n: i32, k: i32) -> Polar<FT> {
self.to_polar().nth_root(n, k)
}
pub fn pow_rational(self, p: i32, q: i32, k: i32) -> Polar<FT> {
self.to_polar().pow_rational(p, q, k)
}
pub fn powi(self, n: i32) -> Polar<FT> {
self.to_polar().powi(n)
}
pub fn powf(self, x: FT) -> Polar<FT> {
self.to_polar().powf(x)
}
pub fn sqrt(self) -> Self {
let two = FT::ONE + FT::ONE;
let abs = self.abs();
Self::new(
((abs + self.re) / two).sqrt(),
((abs - self.re) / two).sqrt().copysign(self.im),
)
}
pub fn distance(self, other: Self) -> FT {
(self - other).abs()
}
pub fn distance_squared(self, other: Self) -> FT {
(self - other).abs_sq()
}
pub fn lerp(self, other: Self, t: FT) -> Self {
self + (other - self) * t
}
}
impl<FT: Number> Add for Complex<FT> {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Complex::new(self.re + other.re, self.im + other.im)
}
}
impl<FT: Number> Add<FT> for Complex<FT> {
type Output = Self;
fn add(self, re: FT) -> Self::Output {
Complex::new(self.re + re, self.im)
}
}
impl<FT: Number> AddAssign for Complex<FT> {
fn add_assign(&mut self, other: Self) {
self.re += other.re;
self.im += other.im;
}
}
impl<FT: Number> AddAssign<FT> for Complex<FT> {
fn add_assign(&mut self, re: FT) {
self.re += re;
}
}
impl<FT: Number> Sub for Complex<FT> {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Complex::new(self.re - other.re, self.im - other.im)
}
}
impl<FT: Number> Sub<FT> for Complex<FT> {
type Output = Self;
fn sub(self, re: FT) -> Self::Output {
Complex::new(self.re - re, self.im)
}
}
impl<FT: Number> SubAssign for Complex<FT> {
fn sub_assign(&mut self, other: Self) {
self.re -= other.re;
self.im -= other.im;
}
}
impl<FT: Number> SubAssign<FT> for Complex<FT> {
fn sub_assign(&mut self, re: FT) {
self.re -= re;
}
}
impl<FT: Number> Mul for Complex<FT> {
type Output = Self;
fn mul(mut self, other: Self) -> Self {
self *= other;
self
}
}
impl<FT: Number> Mul<FT> for Complex<FT> {
type Output = Self;
fn mul(self, re: FT) -> Self {
Complex::new(self.re * re, self.im * re)
}
}
impl<FT: Number> MulAssign for Complex<FT> {
fn mul_assign(&mut self, other: Self) {
let re = self.re * other.re - self.im * other.im;
self.im = self.re * other.im + self.im * other.re;
self.re = re;
}
}
impl<FT: Number> MulAssign<FT> for Complex<FT> {
fn mul_assign(&mut self, re: FT) {
self.re *= re;
self.im *= re;
}
}
impl<FT: Number> Div for Complex<FT> {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
self * other.recip()
}
}
impl<FT: Number> Div<FT> for Complex<FT> {
type Output = Self;
fn div(self, re: FT) -> Self::Output {
Complex::new(self.re / re, self.im / re)
}
}
impl<FT: Number> DivAssign for Complex<FT> {
fn div_assign(&mut self, other: Self) {
*self = *self / other;
}
}
impl<FT: Number> DivAssign<FT> for Complex<FT> {
fn div_assign(&mut self, re: FT) {
self.re /= re;
self.im /= re;
}
}
impl<FT: Number> Neg for Complex<FT> {
type Output = Self;
fn neg(self) -> Self::Output {
Self::new(-self.re, -self.im)
}
}
impl<FT: Number> From<FT> for Complex<FT> {
fn from(value: FT) -> Self {
Self::new(value, FT::ZERO)
}
}
#[cfg(feature = "approx")]
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
#[cfg(feature = "approx")]
impl<FT: AbsDiffEq + Copy> AbsDiffEq for Complex<FT>
where
<FT as AbsDiffEq>::Epsilon: Copy,
{
type Epsilon = <FT as AbsDiffEq>::Epsilon;
fn default_epsilon() -> Self::Epsilon {
FT::default_epsilon()
}
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
FT::abs_diff_eq(&self.re, &other.re, epsilon)
&& FT::abs_diff_eq(&self.im, &other.im, epsilon)
}
}
#[cfg(feature = "approx")]
impl<FT: RelativeEq + Copy> RelativeEq for Complex<FT>
where
<FT as AbsDiffEq>::Epsilon: Copy,
{
fn default_max_relative() -> Self::Epsilon {
FT::default_max_relative()
}
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
FT::relative_eq(&self.re, &other.re, epsilon, max_relative)
&& FT::relative_eq(&self.im, &other.im, epsilon, max_relative)
}
}
#[cfg(feature = "approx")]
impl<FT: UlpsEq + Copy> UlpsEq for Complex<FT>
where
<FT as AbsDiffEq>::Epsilon: Copy,
{
fn default_max_ulps() -> u32 {
FT::default_max_ulps()
}
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
FT::ulps_eq(&self.re, &other.re, epsilon, max_ulps)
&& FT::ulps_eq(&self.im, &other.im, epsilon, max_ulps)
}
}