Skip to main content

oxiphysics_core/complex/
complex_traits.rs

1//! # Complex - Trait Implementations
2//!
3//! This module contains trait implementations for `Complex`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Add`
8//! - `Sub`
9//! - `Mul`
10//! - `Div`
11//! - `Neg`
12//!
13//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
14
15#[allow(unused_imports)]
16use super::functions::*;
17use std::ops::{Add, Div, Mul, Neg, Sub};
18
19use super::types::Complex;
20
21impl Add for Complex {
22    type Output = Self;
23    fn add(self, rhs: Self) -> Self {
24        Self::new(self.re + rhs.re, self.im + rhs.im)
25    }
26}
27
28impl Sub for Complex {
29    type Output = Self;
30    fn sub(self, rhs: Self) -> Self {
31        Self::new(self.re - rhs.re, self.im - rhs.im)
32    }
33}
34
35impl Mul for Complex {
36    type Output = Self;
37    fn mul(self, rhs: Self) -> Self {
38        Self::new(
39            self.re * rhs.re - self.im * rhs.im,
40            self.re * rhs.im + self.im * rhs.re,
41        )
42    }
43}
44
45impl Div for Complex {
46    type Output = Self;
47    fn div(self, rhs: Self) -> Self {
48        let denom = rhs.norm_sq();
49        Self::new(
50            (self.re * rhs.re + self.im * rhs.im) / denom,
51            (self.im * rhs.re - self.re * rhs.im) / denom,
52        )
53    }
54}
55
56impl Neg for Complex {
57    type Output = Self;
58    fn neg(self) -> Self {
59        Self::new(-self.re, -self.im)
60    }
61}