Skip to content

Commit

Permalink
feat(complex): 增加复数
Browse files Browse the repository at this point in the history
  • Loading branch information
donjuanplatinum committed Jun 27, 2024
1 parent 5fa9f83 commit bf2fd26
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
- [Heap 堆/优先队列](./doc/structure/_index.md)
### Searching 搜索
- [BinarySearch 二分搜索](./doc/searching/_index.md)

### Math 数学
- [Complex 复数](./doc/math/_index.md)
18 changes: 18 additions & 0 deletions doc/math/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Math 数学
math: true
---

+ 复数

## 复数
形如`a+bi`的数为复数 a为`实部` b为`虚部` i为`虚数单位`

复数一般用`z`表示 `z = a + bi`

### 加法
`(a + bi) + (c + di) = (a + c) + (b + d)i`
### 乘法
`(a + bi) * (c + di) = (a * c - b * d) + (a * d + b * c)i`
### 除法
`a + bi / c + di = (a * c + b * d)/ (c * c + d * d) + ((b * c - a * d)/(c * c + d * d))i`
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ pub mod structure;

/// Searching
pub mod searching;

/// Math
pub mod math;
149 changes: 149 additions & 0 deletions src/math/complex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use super::NumOps;
use core::ops::{Add, Mul, Sub,Div};


/// impl<'a,'b,T: Clone + NumOps> Method<&'a Complex<T>> for &'b Complex<T>
macro_rules! impl_ref_ref {
(impl $imp:ident, $method:ident) => {
impl<'a, 'b, T: Clone + NumOps> $imp<&'a Complex<T>> for &'b Complex<T> {
type Output = Complex<T>;

#[inline]
fn $method(self, other: &'a Complex<T>) -> Self::Output {
self.clone().$method(other.clone())
}
}
};
}

/// impl<'a,T: Clone + NumOps> Complex<T> for &'a Complex<T>
macro_rules! impl_val_ref {
(impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + NumOps> $imp<&'a Complex<T>> for Complex<T> {
type Output = Complex<T>;

#[inline]
fn $method(self, other: &'a Complex<T>) -> Self::Output {
self.$method(other.clone())
}
}
};
}

/// impl<'a,T: Clone + NumOps> &'a Complex<T> for Complex<T>
macro_rules! impl_ref_val {
(impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + NumOps> $imp<Complex<T>> for &'a Complex<T> {
type Output = Complex<T>;

#[inline]
fn $method(self, other: Complex<T>) -> Self::Output {
self.clone().$method(other)
}
}
};
}

macro_rules! impl_all {
(impl $imp:ident ,$method: ident) => {
impl_ref_val!(impl $imp, $method);
impl_ref_ref!(impl $imp, $method);
impl_val_ref!(impl $imp, $method);
};
}

#[derive(Clone, Debug)]
#[repr(C)]
/// Complex
pub struct Complex<T> {
/// Real
pub re: T,
/// Imaginary
pub im: T,
}

impl<T> Complex<T> {
#[inline]
/// Create a new `Complex`
pub fn new(re: T, im: T) -> Self {
return Complex { re, im };
}
}

/// (a+bi) + (c+di) = (a+c)+(b+d)i
impl<T: NumOps> Add<Complex<T>> for Complex<T> {
type Output = Complex<T>;
#[inline]
fn add(self, rhs: Complex<T>) -> Self::Output {
return Complex {
re: self.re + rhs.re,
im: self.im + rhs.im,
};
}
}

impl_all!(impl Add, add);

/// (a + bi) - (c + di) = (a - c ) + (b - d)i
impl<T: NumOps> Sub<Complex<T>> for Complex<T> {
type Output = Complex<T>;
#[inline]
fn sub(self,rhs:Complex<T>) -> Self::Output {
return Complex {
re: self.re - rhs.re,
im: self.im - rhs.im,
};
}
}

impl_all!(impl Sub,sub);

/// (a+bi) * (c+di) = (a*c - b*d)+(a*d + b*c)i
impl<T: NumOps + Clone> Mul<Complex<T>>
for Complex<T>
{
type Output = Complex<T>;
#[inline]
fn mul(self, rhs: Complex<T>) -> Self::Output {
return Complex {
re: self.re.clone() * rhs.re.clone() - self.im.clone() * rhs.im.clone(),
im: self.re * rhs.im + self.im * rhs.re,
};
}
}





impl_all!(impl Mul, mul);


/// a+bi / c+di = (a * c + b * d)/ (c*c + d*d) + ((b*c - a*d)/(c*c + d*d))i
impl<T: NumOps + Clone> Div<Complex<T>>
for Complex<T>
{
type Output = Complex<T>;
#[inline]
fn div(self, rhs: Complex<T>) -> Self::Output {
let tmp = rhs.re.clone() * rhs.re.clone() + rhs.im.clone() * rhs.im.clone();
let re = (self.re.clone() * rhs.re.clone() + self.im.clone() * rhs.im.clone()) / tmp.clone();
let im = (self.im.clone() * rhs.re.clone() - self.re.clone() * rhs.im.clone()) / tmp;
return Complex {
re,
im,
};
}
}

impl_all!(impl Div, div);

/// a+bi -> (a,b)
impl<T> core::fmt::Display for Complex<T>
where T: core::fmt::Display,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
return write!(f,"({},{})",self.re,self.im);
}
}

1 change: 1 addition & 0 deletions src/math/dft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

21 changes: 21 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mod complex;
pub use complex::*;

use core::ops::{Add, Div, Mul, Rem, Sub};
pub trait NumOps<Rhs = Self, Output = Self>:
Add<Rhs, Output = Output>
+ Sub<Rhs, Output = Output>
+ Mul<Rhs, Output = Output>
+ Div<Rhs, Output = Output>
+ Rem<Rhs, Output = Output>
{
}

impl<T, Rhs, Output> NumOps<Rhs, Output> for T where
T: Add<Rhs, Output = Output>
+ Sub<Rhs, Output = Output>
+ Mul<Rhs, Output = Output>
+ Div<Rhs, Output = Output>
+ Rem<Rhs, Output = Output>
{
}

0 comments on commit bf2fd26

Please sign in to comment.