feat(maths): complex div
This commit is contained in:
parent
58a0ec983d
commit
3500de6cb0
13
src/maths.rs
13
src/maths.rs
|
@ -31,6 +31,11 @@ impl Rational {
|
|||
pub fn inverse(&self) -> Self {
|
||||
Rational::new(self.denominator, self.numerator)
|
||||
}
|
||||
|
||||
pub fn simplify(lhs: Rational, rhs: Rational) -> Self {
|
||||
let res = Rational::new(lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator);
|
||||
res.reduce()
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Add<Rational> for Rational {
|
||||
|
@ -172,4 +177,12 @@ impl ops::Mul<GaussianRational> for GaussianRational {
|
|||
GaussianRational::new(self.real * rhs.real - self.imaginary * rhs.imaginary, self.real * rhs.imaginary + self.imaginary * rhs.real)
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Div<GaussianRational> for GaussianRational {
|
||||
type Output = GaussianRational;
|
||||
|
||||
fn div(self, rhs: GaussianRational) -> GaussianRational {
|
||||
let cd_squared = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary;
|
||||
GaussianRational::new(Rational::simplify(self.real * rhs.real + self.imaginary * rhs.imaginary, cd_squared), Rational::simplify(self.imaginary * rhs.real - self.real * rhs.imaginary, cd_squared))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue