aboutsummaryrefslogtreecommitdiff
path: root/libmu
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2024-09-15 14:06:15 +0200
committerhachem <im@hachem.wtf>2024-09-15 14:06:15 +0200
commit6afec96567c1735b539214d87fb9069ad0828904 (patch)
treecbb54d3132a937f83e7e3909b8059cedccfc8fce /libmu
parent0808323fd6d0afa6adb476a28f33a6b7bed8e297 (diff)
Numeric Types and Complex Numbers
Diffstat (limited to 'libmu')
-rw-r--r--libmu/Cargo.toml6
-rw-r--r--libmu/src/lib.rs4
-rw-r--r--libmu/src/maths/complex.rs181
-rw-r--r--libmu/src/maths/mod.rs2
-rw-r--r--libmu/src/maths/numeric_types.rs47
5 files changed, 240 insertions, 0 deletions
diff --git a/libmu/Cargo.toml b/libmu/Cargo.toml
new file mode 100644
index 0000000..f451347
--- /dev/null
+++ b/libmu/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "libmu"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/libmu/src/lib.rs b/libmu/src/lib.rs
new file mode 100644
index 0000000..f96993c
--- /dev/null
+++ b/libmu/src/lib.rs
@@ -0,0 +1,4 @@
+mod maths;
+
+pub use maths::complex;
+pub use maths::numeric_types;
diff --git a/libmu/src/maths/complex.rs b/libmu/src/maths/complex.rs
new file mode 100644
index 0000000..dcd5902
--- /dev/null
+++ b/libmu/src/maths/complex.rs
@@ -0,0 +1,181 @@
+use crate::numeric_types::FloatTypes;
+use std::ops;
+
+pub struct Complex<T: FloatTypes> {
+ pub real: T,
+ pub imaginary: T,
+}
+
+impl<T: FloatTypes> Complex<T> {
+ pub fn new(real: T, imaginary: T) -> Complex<T> {
+ Complex { real, imaginary }
+ }
+
+ pub fn get_conjugate(&self) -> Complex<T> {
+ Complex::new(self.real, -self.imaginary)
+ }
+
+ pub fn conjugate(&mut self) {
+ self.imaginary = -self.imaginary;
+ }
+
+ pub fn phase(&self) -> T {
+ T::atan2(self.imaginary, self.real)
+ }
+
+ pub fn norm(&self) -> T {
+ self.real * self.real + self.imaginary * self.imaginary
+ }
+
+ pub fn abs(&self) -> T {
+ T::sqrt(self.norm())
+ }
+}
+
+impl<T: FloatTypes> ops::Add for Complex<T> {
+ type Output = Complex<T>;
+
+ fn add(self, other: Complex<T>) -> Complex<T> {
+ Complex {
+ real: self.real + other.real,
+ imaginary: self.imaginary + other.imaginary,
+ }
+ }
+}
+
+impl<T: FloatTypes> ops::Sub for Complex<T> {
+ type Output = Complex<T>;
+
+ fn sub(self, other: Complex<T>) -> Complex<T> {
+ Complex {
+ real: self.real - other.real,
+ imaginary: self.imaginary - other.imaginary,
+ }
+ }
+}
+
+impl<T: FloatTypes> ops::Mul for Complex<T> {
+ type Output = Complex<T>;
+
+ fn mul(self, other: Complex<T>) -> Complex<T> {
+ Complex {
+ real: self.real * other.real - self.imaginary * other.imaginary,
+ imaginary: self.real * other.imaginary + self.imaginary * other.real,
+ }
+ }
+}
+
+impl<T: FloatTypes> ops::Div for Complex<T> {
+ type Output = Complex<T>;
+
+ fn div(self, other: Complex<T>) -> Complex<T> {
+ let denom = other.real * other.real + other.imaginary * other.imaginary;
+ Complex {
+ real: (self.real * other.real + self.imaginary * other.imaginary) / denom,
+ imaginary: (self.imaginary * other.real - self.real * other.imaginary) / denom,
+ }
+ }
+}
+
+impl<T: FloatTypes> ops::Add<T> for Complex<T> {
+ type Output = Complex<T>;
+
+ fn add(self, other: T) -> Complex<T> {
+ Complex {
+ real: self.real + other,
+ imaginary: self.imaginary,
+ }
+ }
+}
+
+impl<T: FloatTypes> ops::Sub<T> for Complex<T> {
+ type Output = Complex<T>;
+
+ fn sub(self, other: T) -> Complex<T> {
+ Complex {
+ real: self.real - other,
+ imaginary: self.imaginary,
+ }
+ }
+}
+
+impl<T: FloatTypes> ops::Mul<T> for Complex<T> {
+ type Output = Complex<T>;
+
+ fn mul(self, other: T) -> Complex<T> {
+ Complex {
+ real: self.real * other,
+ imaginary: self.imaginary * other,
+ }
+ }
+}
+
+impl<T: FloatTypes> ops::Div<T> for Complex<T> {
+ type Output = Complex<T>;
+
+ fn div(self, other: T) -> Complex<T> {
+ Complex {
+ real: self.real / other,
+ imaginary: self.imaginary / other,
+ }
+ }
+}
+
+impl<T: FloatTypes> 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> {
+ fn sub_assign(&mut self, other: Complex<T>) {
+ self.real -= other.real;
+ self.imaginary -= other.imaginary;
+ }
+}
+
+impl<T: FloatTypes> 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;
+ self.real = new_real;
+ self.imaginary = new_imaginary;
+ }
+}
+
+impl<T: FloatTypes> 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;
+ let new_imaginary = (self.imaginary * other.real - self.real * other.imaginary) / denom;
+ self.real = new_real;
+ self.imaginary = new_imaginary;
+ }
+}
+
+impl<T: FloatTypes> 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> {
+ fn sub_assign(&mut self, other: T) {
+ self.real -= other;
+ }
+}
+
+impl<T: FloatTypes> 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> {
+ 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
new file mode 100644
index 0000000..5986ab8
--- /dev/null
+++ b/libmu/src/maths/mod.rs
@@ -0,0 +1,2 @@
+pub mod complex;
+pub mod numeric_types;
diff --git a/libmu/src/maths/numeric_types.rs b/libmu/src/maths/numeric_types.rs
new file mode 100644
index 0000000..dac766f
--- /dev/null
+++ b/libmu/src/maths/numeric_types.rs
@@ -0,0 +1,47 @@
+use std::ops;
+
+pub trait NumericTypes:
+ Copy
+ + PartialOrd
+ + ops::Add<Output = Self>
+ + ops::Mul<Output = Self>
+ + ops::Sub<Output = Self>
+ + ops::Div<Output = Self>
+ + ops::Neg<Output = Self>
+ + ops::AddAssign
+ + ops::SubAssign
+ + ops::MulAssign
+ + ops::DivAssign
+{
+}
+
+pub trait IntTypes: NumericTypes {}
+pub trait FloatTypes: NumericTypes {
+ fn sqrt(self) -> Self;
+ fn atan2(y: Self, x: Self) -> Self;
+}
+
+impl FloatTypes for f32 {
+ fn sqrt(self) -> Self {
+ self.sqrt()
+ }
+
+ fn atan2(y: Self, x: Self) -> Self {
+ y.atan2(x)
+ }
+}
+
+impl FloatTypes for f64 {
+ fn sqrt(self) -> Self {
+ self.sqrt()
+ }
+
+ fn atan2(y: Self, x: Self) -> Self {
+ y.atan2(x)
+ }
+}
+
+impl NumericTypes for i32 {}
+impl NumericTypes for i64 {}
+impl NumericTypes for f32 {}
+impl NumericTypes for f64 {}