use num_complex::Complex64;
use crate::core::CalcError;
#[inline]
pub fn add(a: Complex64, b: Complex64) -> Complex64 {
a + b
}
#[inline]
pub fn sub(a: Complex64, b: Complex64) -> Complex64 {
a - b
}
#[inline]
pub fn mul(a: Complex64, b: Complex64) -> Complex64 {
a * b
}
pub fn div(a: Complex64, b: Complex64) -> Result<Complex64, CalcError> {
if b.norm() == 0.0 {
return Err(CalcError::division_by_zero());
}
Ok(a / b)
}
pub fn pow(a: Complex64, b: Complex64) -> Result<Complex64, CalcError> {
let zero = Complex64::new(0.0, 0.0);
if a == zero && b == zero {
return Ok(Complex64::new(1.0, 0.0));
}
let result = a.powc(b);
if !result.is_finite() {
return Err(CalcError::nan_or_inf());
}
Ok(result)
}
#[inline]
pub fn norm(c: Complex64) -> f64 {
c.norm()
}
pub fn arg(c: Complex64) -> Result<f64, CalcError> {
if c.re == 0.0 && c.im == 0.0 {
return Err(CalcError::domain(
"arg(0+0i) is undefined (atan2(0,0) is indeterminate)".to_string(),
)
.with_i18n("msg.complex.arg_zero_undefined", vec![]));
}
Ok(c.arg())
}
#[inline]
pub fn conj(c: Complex64) -> Complex64 {
c.conj()
}
#[inline]
pub fn exp(c: Complex64) -> Complex64 {
c.exp()
}
#[inline]
pub fn ln(c: Complex64) -> Complex64 {
c.ln()
}
#[cfg(test)]
mod tests {
use super::*;
const EPS: f64 = 1e-10;
fn assert_c64_approx(actual: Complex64, expected_re: f64, expected_im: f64) {
assert!(
(actual.re - expected_re).abs() < EPS && (actual.im - expected_im).abs() < EPS,
"expected ({}, {}), got ({}, {})",
expected_re,
expected_im,
actual.re,
actual.im,
);
}
#[test]
fn test_add() {
let a = Complex64::new(1.0, 2.0);
let b = Complex64::new(3.0, 4.0);
assert_c64_approx(add(a, b), 4.0, 6.0);
}
#[test]
fn test_sub() {
let a = Complex64::new(1.0, 2.0);
let b = Complex64::new(3.0, 4.0);
assert_c64_approx(sub(a, b), -2.0, -2.0);
}
#[test]
fn test_mul() {
let a = Complex64::new(1.0, 2.0);
let b = Complex64::new(3.0, 4.0);
assert_c64_approx(mul(a, b), -5.0, 10.0);
}
#[test]
fn test_div_normal() {
let a = Complex64::new(1.0, 2.0);
let b = Complex64::new(3.0, 4.0);
let result = div(a, b).unwrap();
assert_c64_approx(result, 11.0 / 25.0, 2.0 / 25.0);
}
#[test]
fn test_div_by_zero() {
let a = Complex64::new(1.0, 2.0);
let b = Complex64::new(0.0, 0.0);
assert!(
matches!(div(a, b), Err(ref e) if e.kind == crate::core::ErrorKind::DivisionByZero)
);
}
#[test]
fn test_pow_squared() {
let a = Complex64::new(1.0, 1.0);
let b = Complex64::new(2.0, 0.0);
assert_c64_approx(pow(a, b).unwrap(), 0.0, 2.0);
}
#[test]
fn test_pow_zero_zero() {
let zero = Complex64::new(0.0, 0.0);
assert_c64_approx(pow(zero, zero).unwrap(), 1.0, 0.0);
}
#[test]
fn test_pow_overflow() {
let a = Complex64::new(1e308, 1e308);
let b = Complex64::new(1e308, 1e308);
assert!(matches!(pow(a, b), Err(ref e) if e.kind == crate::core::ErrorKind::NaNOrInf));
}
#[test]
fn test_norm() {
let c = Complex64::new(3.0, 4.0);
assert!((norm(c) - 5.0).abs() < EPS);
}
#[test]
fn test_norm_pure_imaginary() {
let c = Complex64::new(0.0, 3.0);
assert!((norm(c) - 3.0).abs() < EPS);
}
#[test]
fn test_arg_first_quadrant() {
let c = Complex64::new(1.0, 1.0);
assert!((arg(c).unwrap() - std::f64::consts::FRAC_PI_4).abs() < EPS);
}
#[test]
fn test_arg_positive_real() {
let c = Complex64::new(2.0, 0.0);
assert!((arg(c).unwrap() - 0.0).abs() < EPS);
}
#[test]
fn test_arg_zero_is_error() {
let c = Complex64::new(0.0, 0.0);
assert!(matches!(arg(c), Err(ref e) if e.kind == crate::core::ErrorKind::Domain));
}
#[test]
fn test_conj() {
let c = Complex64::new(3.0, 4.0);
assert_c64_approx(conj(c), 3.0, -4.0);
}
#[test]
fn test_conj_pure_real() {
let c = Complex64::new(5.0, 0.0);
assert_c64_approx(conj(c), 5.0, 0.0);
}
#[test]
fn test_exp_euler() {
let c = Complex64::new(0.0, std::f64::consts::PI);
assert_c64_approx(exp(c), -1.0, 0.0);
}
#[test]
fn test_exp_general() {
let c = Complex64::new(1.0, 1.0);
let result = exp(c);
let expected_re = std::f64::consts::E * 1.0_f64.cos();
let expected_im = std::f64::consts::E * 1.0_f64.sin();
assert_c64_approx(result, expected_re, expected_im);
}
#[test]
fn test_ln_one_plus_i() {
let c = Complex64::new(1.0, 1.0);
let expected = c.ln();
assert_c64_approx(ln(c), expected.re, expected.im);
}
#[test]
fn test_ln_positive_real() {
let c = Complex64::new(2.0, 0.0);
assert_c64_approx(ln(c), std::f64::consts::LN_2, 0.0);
}
}