From 085e3d6f1d268676b6bc796b0b1fa60a8f58ee1d Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 15 Sep 2024 17:58:22 +0200 Subject: Fix and formatting --- libmu/Cargo.toml | 1 + libmu/src/lib.rs | 5 +-- libmu/src/maths/complex.rs | 57 +++++++++++++++++++++------------ libmu/src/maths/mod.rs | 3 ++ libmu/src/maths/numeric_types.rs | 68 +++++++++++++++++++++++++++++++--------- 5 files changed, 98 insertions(+), 36 deletions(-) (limited to 'libmu') diff --git a/libmu/Cargo.toml b/libmu/Cargo.toml index f451347..b495ce6 100644 --- a/libmu/Cargo.toml +++ b/libmu/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +libm = "0.2.8" diff --git a/libmu/src/lib.rs b/libmu/src/lib.rs index f96993c..83fb75c 100644 --- a/libmu/src/lib.rs +++ b/libmu/src/lib.rs @@ -1,4 +1,5 @@ +#![no_std] + mod maths; -pub use maths::complex; -pub use maths::numeric_types; +pub use maths::*; diff --git a/libmu/src/maths/complex.rs b/libmu/src/maths/complex.rs index dcd5902..79212b6 100644 --- a/libmu/src/maths/complex.rs +++ b/libmu/src/maths/complex.rs @@ -1,12 +1,29 @@ -use crate::numeric_types::FloatTypes; -use std::ops; +use crate::numeric_types::Float; +use core::{fmt, ops}; -pub struct Complex { +#[derive(Copy, Clone)] +pub struct Complex { pub real: T, pub imaginary: T, } -impl Complex { +impl fmt::Debug for Complex { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Complex {{ real: {:?}, imaginary: {:?} }}", + self.real, self.imaginary + ) + } +} + +impl fmt::Display for Complex { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} + {}i", self.real, self.imaginary) + } +} + +impl Complex { pub fn new(real: T, imaginary: T) -> Complex { Complex { real, imaginary } } @@ -32,7 +49,7 @@ impl Complex { } } -impl ops::Add for Complex { +impl ops::Add for Complex { type Output = Complex; fn add(self, other: Complex) -> Complex { @@ -43,7 +60,7 @@ impl ops::Add for Complex { } } -impl ops::Sub for Complex { +impl ops::Sub for Complex { type Output = Complex; fn sub(self, other: Complex) -> Complex { @@ -54,7 +71,7 @@ impl ops::Sub for Complex { } } -impl ops::Mul for Complex { +impl ops::Mul for Complex { type Output = Complex; fn mul(self, other: Complex) -> Complex { @@ -65,7 +82,7 @@ impl ops::Mul for Complex { } } -impl ops::Div for Complex { +impl ops::Div for Complex { type Output = Complex; fn div(self, other: Complex) -> Complex { @@ -77,7 +94,7 @@ impl ops::Div for Complex { } } -impl ops::Add for Complex { +impl ops::Add for Complex { type Output = Complex; fn add(self, other: T) -> Complex { @@ -88,7 +105,7 @@ impl ops::Add for Complex { } } -impl ops::Sub for Complex { +impl ops::Sub for Complex { type Output = Complex; fn sub(self, other: T) -> Complex { @@ -99,7 +116,7 @@ impl ops::Sub for Complex { } } -impl ops::Mul for Complex { +impl ops::Mul for Complex { type Output = Complex; fn mul(self, other: T) -> Complex { @@ -110,7 +127,7 @@ impl ops::Mul for Complex { } } -impl ops::Div for Complex { +impl ops::Div for Complex { type Output = Complex; fn div(self, other: T) -> Complex { @@ -121,21 +138,21 @@ impl ops::Div for Complex { } } -impl ops::AddAssign for Complex { +impl ops::AddAssign for Complex { fn add_assign(&mut self, other: Complex) { self.real += other.real; self.imaginary += other.imaginary; } } -impl ops::SubAssign for Complex { +impl ops::SubAssign for Complex { fn sub_assign(&mut self, other: Complex) { self.real -= other.real; self.imaginary -= other.imaginary; } } -impl ops::MulAssign for Complex { +impl ops::MulAssign for Complex { fn mul_assign(&mut self, other: Complex) { let new_real = self.real * other.real - self.imaginary * other.imaginary; let new_imaginary = self.real * other.imaginary + self.imaginary * other.real; @@ -144,7 +161,7 @@ impl ops::MulAssign for Complex { } } -impl ops::DivAssign for Complex { +impl ops::DivAssign for Complex { fn div_assign(&mut self, other: Complex) { let denom = other.real * other.real + other.imaginary * other.imaginary; let new_real = (self.real * other.real + self.imaginary * other.imaginary) / denom; @@ -154,26 +171,26 @@ impl ops::DivAssign for Complex { } } -impl ops::AddAssign for Complex { +impl ops::AddAssign for Complex { fn add_assign(&mut self, other: T) { self.real += other; } } -impl ops::SubAssign for Complex { +impl ops::SubAssign for Complex { fn sub_assign(&mut self, other: T) { self.real -= other; } } -impl ops::MulAssign for Complex { +impl ops::MulAssign for Complex { fn mul_assign(&mut self, other: T) { self.real *= other; self.imaginary *= other; } } -impl ops::DivAssign for Complex { +impl ops::DivAssign for Complex { fn div_assign(&mut self, other: T) { self.real /= other; self.imaginary /= other; diff --git a/libmu/src/maths/mod.rs b/libmu/src/maths/mod.rs index 5986ab8..a6ccc9e 100644 --- a/libmu/src/maths/mod.rs +++ b/libmu/src/maths/mod.rs @@ -1,2 +1,5 @@ pub mod complex; pub mod numeric_types; + +pub use complex::*; +pub use numeric_types::*; diff --git a/libmu/src/maths/numeric_types.rs b/libmu/src/maths/numeric_types.rs index dac766f..42e2fb7 100644 --- a/libmu/src/maths/numeric_types.rs +++ b/libmu/src/maths/numeric_types.rs @@ -1,6 +1,6 @@ -use std::ops; +use core::ops; -pub trait NumericTypes: +pub trait Numeric: Copy + PartialOrd + ops::Add @@ -13,35 +13,75 @@ pub trait NumericTypes: + ops::MulAssign + ops::DivAssign { + fn zero() -> Self; + fn one() -> Self; } -pub trait IntTypes: NumericTypes {} -pub trait FloatTypes: NumericTypes { +pub trait Integer: Numeric {} +pub trait Float: Numeric { fn sqrt(self) -> Self; fn atan2(y: Self, x: Self) -> Self; } -impl FloatTypes for f32 { +impl Float for f32 { fn sqrt(self) -> Self { - self.sqrt() + libm::sqrtf(self) } fn atan2(y: Self, x: Self) -> Self { - y.atan2(x) + libm::atan2f(y, x) } } -impl FloatTypes for f64 { +impl Float for f64 { fn sqrt(self) -> Self { - self.sqrt() + libm::sqrt(self) } fn atan2(y: Self, x: Self) -> Self { - y.atan2(x) + libm::atan2(y, x) } } -impl NumericTypes for i32 {} -impl NumericTypes for i64 {} -impl NumericTypes for f32 {} -impl NumericTypes for f64 {} +impl Integer for i64 {} +impl Integer for i32 {} + +impl Numeric for i32 { + fn zero() -> Self { + 0 + } + + fn one() -> Self { + 1 + } +} + +impl Numeric for i64 { + fn zero() -> Self { + 0 + } + + fn one() -> Self { + 1 + } +} + +impl Numeric for f32 { + fn zero() -> Self { + 0.0 + } + + fn one() -> Self { + 1.0 + } +} + +impl Numeric for f64 { + fn zero() -> Self { + 0.0 + } + + fn one() -> Self { + 1.0 + } +} -- cgit v1.3