-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fa9f83
commit bf2fd26
Showing
6 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,6 @@ pub mod structure; | |
|
||
/// Searching | ||
pub mod searching; | ||
|
||
/// Math | ||
pub mod math; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
{ | ||
} |