use std::ops::{Add, Div, Mul, Sub};
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Complex {
pub re: f32,
pub im: f32,
}
impl Complex {
pub fn new(re: f32, im: f32) -> Self {
Self { re, im }
}
pub fn from_rect(x: f32, y: f32) -> Self {
Self { re: x, im: y }
}
pub fn from_polar(r: f32, theta: f32) -> Self {
Self {
re: r * theta.cos(),
im: r * theta.sin(),
}
}
pub fn conj(&self) -> Self {
Self {
re: self.re,
im: -self.im,
}
}
pub fn r#mod(&self) -> f32 {
(self.re * self.re + self.im * self.im).sqrt()
}
}
impl Add for Complex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
re: self.re + rhs.re,
im: self.im + rhs.im,
}
}
}
impl Sub for Complex {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
re: self.re - rhs.re,
im: self.im - rhs.im,
}
}
}
impl Mul for Complex {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self {
re: self.re * rhs.re - self.im * rhs.im,
im: self.re * rhs.im + self.im * rhs.re,
}
}
}
impl Div for Complex {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
let numerator = self * rhs.conj();
let denominator = rhs.re * rhs.re + rhs.im * rhs.im;
Self {
re: numerator.re / denominator,
im: numerator.im / denominator,
}
}
}