aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2024-09-15 17:58:22 +0200
committerhachem <im@hachem.wtf>2024-09-15 17:58:22 +0200
commit085e3d6f1d268676b6bc796b0b1fa60a8f58ee1d (patch)
tree6cae6e39d50e5c078b7f68c3603314352e1d1625
parentaca7dbace9f02a85004ad9b61a410977cd6c64f1 (diff)
Fix and formatting
-rw-r--r--libmu/Cargo.toml1
-rw-r--r--libmu/src/lib.rs5
-rw-r--r--libmu/src/maths/complex.rs57
-rw-r--r--libmu/src/maths/mod.rs3
-rw-r--r--libmu/src/maths/numeric_types.rs68
5 files changed, 98 insertions, 36 deletions
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<T: FloatTypes> {
+#[derive(Copy, Clone)]
+pub struct Complex<T: Float> {
pub real: T,
pub imaginary: T,
}
-impl<T: FloatTypes> Complex<T> {
+impl<T: Float + fmt::Debug> fmt::Debug for Complex<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "Complex {{ real: {:?}, imaginary: {:?} }}",
+ self.real, self.imaginary
+ )
+ }
+}
+
+impl<T: Float + fmt::Display> fmt::Display for Complex<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{} + {}i", self.real, self.imaginary)
+ }
+}
+
+impl<T: Float> Complex<T> {
pub fn new(real: T, imaginary: T) -> Complex<T> {
Complex { real, imaginary }
}
@@ -32,7 +49,7 @@ impl<T: FloatTypes> Complex<T> {
}
}
-impl<T: FloatTypes> ops::Add for Complex<T> {
+impl<T: Float> ops::Add for Complex<T> {
type Output = Complex<T>;
fn add(self, other: Complex<T>) -> Complex<T> {
@@ -43,7 +60,7 @@ impl<T: FloatTypes> ops::Add for Complex<T> {
}
}
-impl<T: FloatTypes> ops::Sub for Complex<T> {
+impl<T: Float> ops::Sub for Complex<T> {
type Output = Complex<T>;
fn sub(self, other: Complex<T>) -> Complex<T> {
@@ -54,7 +71,7 @@ impl<T: FloatTypes> ops::Sub for Complex<T> {
}
}
-impl<T: FloatTypes> ops::Mul for Complex<T> {
+impl<T: Float> ops::Mul for Complex<T> {
type Output = Complex<T>;
fn mul(self, other: Complex<T>) -> Complex<T> {
@@ -65,7 +82,7 @@ impl<T: FloatTypes> ops::Mul for Complex<T> {
}
}
-impl<T: FloatTypes> ops::Div for Complex<T> {
+impl<T: Float> ops::Div for Complex<T> {
type Output = Complex<T>;
fn div(self, other: Complex<T>) -> Complex<T> {
@@ -77,7 +94,7 @@ impl<T: FloatTypes> ops::Div for Complex<T> {
}
}
-impl<T: FloatTypes> ops::Add<T> for Complex<T> {
+impl<T: Float> ops::Add<T> for Complex<T> {
type Output = Complex<T>;
fn add(self, other: T) -> Complex<T> {
@@ -88,7 +105,7 @@ impl<T: FloatTypes> ops::Add<T> for Complex<T> {
}
}
-impl<T: FloatTypes> ops::Sub<T> for Complex<T> {
+impl<T: Float> ops::Sub<T> for Complex<T> {
type Output = Complex<T>;
fn sub(self, other: T) -> Complex<T> {
@@ -99,7 +116,7 @@ impl<T: FloatTypes> ops::Sub<T> for Complex<T> {
}
}
-impl<T: FloatTypes> ops::Mul<T> for Complex<T> {
+impl<T: Float> ops::Mul<T> for Complex<T> {
type Output = Complex<T>;
fn mul(self, other: T) -> Complex<T> {
@@ -110,7 +127,7 @@ impl<T: FloatTypes> ops::Mul<T> for Complex<T> {
}
}
-impl<T: FloatTypes> ops::Div<T> for Complex<T> {
+impl<T: Float> ops::Div<T> for Complex<T> {
type Output = Complex<T>;
fn div(self, other: T) -> Complex<T> {
@@ -121,21 +138,21 @@ impl<T: FloatTypes> ops::Div<T> for Complex<T> {
}
}
-impl<T: FloatTypes> ops::AddAssign for Complex<T> {
+impl<T: Float> ops::AddAssign for Complex<T> {
fn add_assign(&mut self, other: Complex<T>) {
self.real += other.real;
self.imaginary += other.imaginary;
}
}
-impl<T: FloatTypes> ops::SubAssign for Complex<T> {
+impl<T: Float> ops::SubAssign for Complex<T> {
fn sub_assign(&mut self, other: Complex<T>) {
self.real -= other.real;
self.imaginary -= other.imaginary;
}
}
-impl<T: FloatTypes> ops::MulAssign for Complex<T> {
+impl<T: Float> ops::MulAssign for Complex<T> {
fn mul_assign(&mut self, other: Complex<T>) {
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<T: FloatTypes> ops::MulAssign for Complex<T> {
}
}
-impl<T: FloatTypes> ops::DivAssign for Complex<T> {
+impl<T: Float> ops::DivAssign for Complex<T> {
fn div_assign(&mut self, other: Complex<T>) {
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<T: FloatTypes> ops::DivAssign for Complex<T> {
}
}
-impl<T: FloatTypes> ops::AddAssign<T> for Complex<T> {
+impl<T: Float> ops::AddAssign<T> for Complex<T> {
fn add_assign(&mut self, other: T) {
self.real += other;
}
}
-impl<T: FloatTypes> ops::SubAssign<T> for Complex<T> {
+impl<T: Float> ops::SubAssign<T> for Complex<T> {
fn sub_assign(&mut self, other: T) {
self.real -= other;
}
}
-impl<T: FloatTypes> ops::MulAssign<T> for Complex<T> {
+impl<T: Float> ops::MulAssign<T> for Complex<T> {
fn mul_assign(&mut self, other: T) {
self.real *= other;
self.imaginary *= other;
}
}
-impl<T: FloatTypes> ops::DivAssign<T> for Complex<T> {
+impl<T: Float> ops::DivAssign<T> for Complex<T> {
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<Output = Self>
@@ -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
+ }
+}