diff options
| author | hachem <im@hachem.wtf> | 2026-09-06 18:53:09 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-06 18:53:09 +0200 |
| commit | 565af48c1eff190ce836293a0eda75c0dc695c82 (patch) | |
| tree | bb07b022eec34450651eaea07b474d581e936e17 /src | |
| parent | 4d404450cd53946e56cc64c66372f0b2d439dbc6 (diff) | |
refactor: split public headers into include/
Diffstat (limited to 'src')
| -rw-r--r-- | src/maths/complex.h | 28 | ||||
| -rw-r--r-- | src/maths/complex.rs | 180 | ||||
| -rw-r--r-- | src/maths/matrix.h | 35 | ||||
| -rw-r--r-- | src/maths/numeric.rs | 101 | ||||
| -rw-r--r-- | src/maths/vector.h | 46 | ||||
| -rw-r--r-- | src/maths/vector_ops.rs | 107 | ||||
| -rw-r--r-- | src/psi.h | 7 |
7 files changed, 0 insertions, 504 deletions
diff --git a/src/maths/complex.h b/src/maths/complex.h deleted file mode 100644 index c94f64a..0000000 --- a/src/maths/complex.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -struct PsiComplex -{ - double real; - double imaginary; -}; - -struct PsiComplex psi_new_complex(double real, double imaginary); -struct PsiComplex psi_new_complex_from_real(double real); - -struct PsiComplex psi_conjugate_complex(struct PsiComplex z); -struct PsiComplex psi_neg_complex(struct PsiComplex z); - -double psi_phase_complex(struct PsiComplex z); -double psi_norm2_complex(struct PsiComplex z); -double psi_abs_complex(struct PsiComplex z); -struct PsiComplex psi_sqrt_complex(struct PsiComplex z); - -struct PsiComplex psi_add_complex(struct PsiComplex a, struct PsiComplex b); -struct PsiComplex psi_sub_complex(struct PsiComplex a, struct PsiComplex b); -struct PsiComplex psi_mul_complex(struct PsiComplex a, struct PsiComplex b); -struct PsiComplex psi_div_complex(struct PsiComplex a, struct PsiComplex b); - -struct PsiComplex psi_add_complex_real(struct PsiComplex a, double b); -struct PsiComplex psi_sub_complex_real(struct PsiComplex a, double b); -struct PsiComplex psi_mul_complex_real(struct PsiComplex a, double b); -struct PsiComplex psi_div_complex_real(struct PsiComplex a, double b); diff --git a/src/maths/complex.rs b/src/maths/complex.rs deleted file mode 100644 index 31eae69..0000000 --- a/src/maths/complex.rs +++ /dev/null @@ -1,180 +0,0 @@ -use crate::Float; -use core::{fmt, ops}; - -#[macro_export] -macro_rules! complex { - ($real:expr, $imaginary:expr) => { - $crate::Complex::new($real, $imaginary) - }; -} - -macro_rules! impl_ops { - ($trait:ident, $method:ident, $op:tt) => { - impl<T: Float> ops::$trait for Complex<T> { - type Output = Complex<T>; - - fn $method(self, other: Complex<T>) -> Complex<T> { - Complex { - real: self.real $op other.real, - imaginary: self.imaginary $op other.imaginary, - } - } - } - }; - - ($trait:ident, $method:ident, $op:tt, real) => { - impl<T: Float> ops::$trait<T> for Complex<T> { - type Output = Complex<T>; - - fn $method(self, other: T) -> Complex<T> { - Complex { - real: self.real $op other, - imaginary: self.imaginary, - } - } - } - }; - - ($trait_assign:ident, $method_assign:ident, $op:tt, assign) => { - impl<T: Float> ops::$trait_assign for Complex<T> { - fn $method_assign(&mut self, other: Complex<T>) { - self.real = self.real $op other.real; - self.imaginary = self.imaginary $op other.imaginary; - } - } - }; - - ($trait_assign:ident, $method_assign:ident, $op:tt, assign_real) => { - impl<T: Float> ops::$trait_assign<T> for Complex<T> { - fn $method_assign(&mut self, other: T) { - self.real = self.real $op other; - } - } - }; -} - -#[derive(Copy, Clone, PartialOrd, PartialEq)] -pub struct Complex<T: Float> { - pub real: T, - pub imaginary: 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> ops::Neg for Complex<T> { - type Output = Complex<T>; - - fn neg(self) -> Complex<T> { - Complex { - real: -self.real, - imaginary: -self.imaginary, - } - } -} - -impl<T: Float> From<T> for Complex<T> { - fn from(real: T) -> Complex<T> { - Complex { - real, - imaginary: T::zero(), - } - } -} - -impl<T: Float> Complex<T> { - pub fn new(real: T, imaginary: T) -> Complex<T> { - Complex { real, imaginary } - } - - pub fn get_conjugate(&self) -> Complex<T> { - Complex { - real: self.real, - imaginary: -self.imaginary, - } - } - - pub fn conjugate(&mut self) { - self.imaginary = -self.imaginary; - } - - pub fn phase(&self) -> T { - T::atan2(self.imaginary, self.real) - } - - pub fn norm2(&self) -> T { - self.real * self.real + self.imaginary * self.imaginary - } - - pub fn abs(&self) -> T { - T::sqrt(self.norm2()) - } -} - -impl_ops!(Add, add, +); -impl_ops!(Sub, sub, -); - -impl<T: Float> ops::Mul for Complex<T> { - type Output = Complex<T>; - - fn mul(self, other: Complex<T>) -> Complex<T> { - // (a + bi) * (c + di) = (ac - bd) + (ad + bc)i - Complex { - real: self.real * other.real - self.imaginary * other.imaginary, - imaginary: self.real * other.imaginary + self.imaginary * other.real, - } - } -} - -impl<T: Float> ops::Div for Complex<T> { - type Output = Complex<T>; - - fn div(self, other: Complex<T>) -> Complex<T> { - // (a + bi) / (c + di) = ((ac + bd) + (bc - ad)i) / (c² + d²) - 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_ops!(AddAssign, add_assign, +, assign); -impl_ops!(SubAssign, sub_assign, -, assign); - -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_imag = self.real * other.imaginary + self.imaginary * other.real; - self.real = new_real; - self.imaginary = new_imag; - } -} - -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; - let new_imag = (self.imaginary * other.real - self.real * other.imaginary) / denom; - self.real = new_real; - self.imaginary = new_imag; - } -} - -impl_ops!(Add, add, +, real); -impl_ops!(Sub, sub, -, real); -impl_ops!(Mul, mul, *, real); -impl_ops!(Div, div, /, real); diff --git a/src/maths/matrix.h b/src/maths/matrix.h deleted file mode 100644 index 1076310..0000000 --- a/src/maths/matrix.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include <stddef.h> - -#include "maths/complex.h" -#include "maths/vector.h" - -struct PsiMatrix -{ - struct PsiComplex *data; - size_t rows; - size_t cols; -}; - -struct PsiMatrix psi_new_matrix(size_t rows, size_t cols); -struct PsiMatrix psi_new_matrix_from(const struct PsiComplex *data, size_t rows, size_t cols); -struct PsiMatrix psi_clone_matrix(struct PsiMatrix m); -void psi_free_matrix(struct PsiMatrix *m); - -#define psi_matrix(rows, cols, ...) \ - psi_new_matrix_from((struct PsiComplex[]){ __VA_ARGS__ }, rows, cols) - -struct PsiComplex psi_get_matrix(struct PsiMatrix m, size_t row, size_t col); -void psi_set_matrix(struct PsiMatrix *m, size_t row, size_t col, struct PsiComplex value); - -struct PsiMatrix psi_dot_matrix(struct PsiMatrix a, struct PsiMatrix b); -struct PsiMatrix psi_kronecker_matrix(struct PsiMatrix a, struct PsiMatrix b); -struct PsiMatrix psi_transpose_matrix(struct PsiMatrix m); -struct PsiMatrix psi_add_matrix(struct PsiMatrix a, struct PsiMatrix b); -struct PsiMatrix psi_sub_matrix(struct PsiMatrix a, struct PsiMatrix b); -struct PsiMatrix psi_scale_matrix(struct PsiMatrix m, struct PsiComplex scalar); - -struct PsiMatrix psi_matrix_from_vector(struct PsiVector v); -struct PsiVector psi_vector_from_matrix(struct PsiMatrix m, enum PsiVectorKind kind); -struct PsiVector psi_mul_vector_matrix(struct PsiVector v, struct PsiMatrix m); diff --git a/src/maths/numeric.rs b/src/maths/numeric.rs deleted file mode 100644 index f5a649e..0000000 --- a/src/maths/numeric.rs +++ /dev/null @@ -1,101 +0,0 @@ -use crate::Complex; -use core::ops; - -macro_rules! impl_numeric { - ($($t:ty),*) => { - $( - impl Numeric for $t { - fn zero() -> Self { - 0 as $t - } - - fn one() -> Self { - 1 as $t - } - } - )* - }; -} - -macro_rules! impl_cnumeric { - ($($t:ty),*) => { - $(impl Numeric for Complex<$t> { - fn zero() -> Self { Complex::new(0.0, 0.0) } - fn one() -> Self { Complex::new(1.0, 0.0) } - })* - }; -} - -macro_rules! impl_float { - ($($t:ty, $sqrt_fn:path, $atan2_fn:path),*) => { - $( - impl Float for $t { - fn sqrt(self) -> Self { - $sqrt_fn(self) - } - - fn atan2(y: Self, x: Self) -> Self { - $atan2_fn(y, x) - } - } - )* - }; -} - -macro_rules! impl_cfloat { - ($($t:ty, $sqrt_fn:path, $atan2_fn:path, $cos_fn:path, $sin_fn:path),*) => { - $( - impl Float for Complex<$t> { - fn sqrt(self) -> Self { - let r = self.abs(); - let theta = self.phase(); - - let sqrt_r = $sqrt_fn(r); - let sqrt_theta = theta / 2.0; - - Complex::new( - sqrt_r * $cos_fn(sqrt_theta), - sqrt_r * $sin_fn(sqrt_theta), - ) - } - - fn atan2(y: Self, x: Self) -> Self { - Complex::new( - $atan2_fn(y.real, x.real), - $atan2_fn(y.imaginary, x.imaginary), - ) - } - } - )* - }; -} - -pub trait Numeric: - 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 -{ - fn zero() -> Self; - fn one() -> Self; -} - -impl_numeric!(i32, i64, f32, f64); -impl_cnumeric!(f32, f64); -impl_float!(f32, libm::sqrtf, libm::atan2f); -impl_float!(f64, libm::sqrt, libm::atan2); -impl_cfloat!(f32, libm::sqrtf, libm::atan2f, libm::cosf, libm::sinf); -impl_cfloat!(f64, libm::sqrt, libm::atan2, libm::cos, libm::sin); - -pub trait Integer: Numeric {} -pub trait Float: Numeric { - fn sqrt(self) -> Self; - fn atan2(y: Self, x: Self) -> Self; -} diff --git a/src/maths/vector.h b/src/maths/vector.h deleted file mode 100644 index d452ef1..0000000 --- a/src/maths/vector.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include <stddef.h> - -#include "maths/complex.h" - -enum PsiVectorKind -{ - PSI_ROW_VECTOR, - PSI_COLUMN_VECTOR, -}; - -struct PsiVector -{ - struct PsiComplex *data; - size_t size; - enum PsiVectorKind kind; -}; - -struct PsiVector psi_new_vector(size_t size, enum PsiVectorKind kind); -struct PsiVector psi_new_vector_from(const struct PsiComplex *data, size_t size, enum PsiVectorKind kind); -struct PsiVector psi_clone_vector(struct PsiVector v); -void psi_free_vector(struct PsiVector *v); - -#define psi_row_vector(...) \ - psi_new_vector_from((struct PsiComplex[]){ __VA_ARGS__ }, \ - sizeof((struct PsiComplex[]){ __VA_ARGS__ }) / sizeof(struct PsiComplex), \ - PSI_ROW_VECTOR) -#define psi_column_vector(...) \ - psi_new_vector_from((struct PsiComplex[]){ __VA_ARGS__ }, \ - sizeof((struct PsiComplex[]){ __VA_ARGS__ }) / sizeof(struct PsiComplex), \ - PSI_COLUMN_VECTOR) - -struct PsiComplex psi_get_vector(struct PsiVector v, size_t index); -void psi_set_vector(struct PsiVector *v, size_t index, struct PsiComplex value); - -struct PsiComplex psi_dot_vector(struct PsiVector a, struct PsiVector b); -struct PsiComplex psi_norm_vector(struct PsiVector v); -struct PsiComplex psi_sum_vector(struct PsiVector v); -struct PsiComplex psi_max_vector(struct PsiVector v); -struct PsiComplex psi_min_vector(struct PsiVector v); - -struct PsiVector psi_add_vector(struct PsiVector a, struct PsiVector b); -struct PsiVector psi_sub_vector(struct PsiVector a, struct PsiVector b); -struct PsiVector psi_scale_vector(struct PsiVector v, struct PsiComplex scalar); -struct PsiVector psi_transpose_vector(struct PsiVector v); diff --git a/src/maths/vector_ops.rs b/src/maths/vector_ops.rs deleted file mode 100644 index 1e7148c..0000000 --- a/src/maths/vector_ops.rs +++ /dev/null @@ -1,107 +0,0 @@ -use super::{Float, Matrix}; -use crate::{ColumnVector, RowVector, VectorImpl}; -use core::ops; - -impl<T: Float, const ROWS: usize, const COLS: usize> ops::Add<&VectorImpl<T, ROWS, COLS>> - for VectorImpl<T, ROWS, COLS> -{ - type Output = Option<VectorImpl<T, ROWS, COLS>>; - - fn add(self, other: &VectorImpl<T, ROWS, COLS>) -> Self::Output { - self.add_to(other) - } -} - -impl<T: Float, const ROWS: usize, const COLS: usize> ops::Sub<&VectorImpl<T, ROWS, COLS>> - for VectorImpl<T, ROWS, COLS> -{ - type Output = Option<VectorImpl<T, ROWS, COLS>>; - - fn sub(self, other: &VectorImpl<T, ROWS, COLS>) -> Self::Output { - self.subtract(other) - } -} - -impl<T: Float, const ROWS: usize, const COLS: usize> ops::Mul<T> for VectorImpl<T, ROWS, COLS> { - type Output = VectorImpl<T, ROWS, COLS>; - - fn mul(self, scalar: T) -> Self::Output { - self.scale(scalar) - } -} - -impl<T: Float, const ROWS: usize, const COLS: usize> ops::Div<T> for VectorImpl<T, ROWS, COLS> { - type Output = VectorImpl<T, ROWS, COLS>; - - fn div(self, scalar: T) -> Self::Output { - self.scale(T::one() / scalar) - } -} - -impl<T: Float, const ROWS: usize, const COLS: usize> ops::AddAssign<VectorImpl<T, ROWS, COLS>> - for VectorImpl<T, ROWS, COLS> -{ - fn add_assign(&mut self, other: VectorImpl<T, ROWS, COLS>) { - if let Some(result) = self.add_to(&other) { - *self = result; - } - } -} - -impl<T: Float, const ROWS: usize, const COLS: usize> ops::SubAssign<VectorImpl<T, ROWS, COLS>> - for VectorImpl<T, ROWS, COLS> -{ - fn sub_assign(&mut self, other: VectorImpl<T, ROWS, COLS>) { - if let Some(result) = self.subtract(&other) { - *self = result; - } - } -} - -impl<T: Float, const ROWS: usize, const COLS: usize> ops::MulAssign<T> - for VectorImpl<T, ROWS, COLS> -{ - fn mul_assign(&mut self, scalar: T) { - *self = self.scale(scalar); - } -} - -impl<T: Float, const ROWS: usize, const COLS: usize> ops::DivAssign<T> - for VectorImpl<T, ROWS, COLS> -{ - fn div_assign(&mut self, scalar: T) { - *self = self.scale(T::one() / scalar); - } -} - -impl<T: Float> ops::Mul<&Matrix<T>> for RowVector<T> { - type Output = Option<RowVector<T>>; - - fn mul(self, matrix: &Matrix<T>) -> Self::Output { - self.mul_matrix(matrix) - } -} - -impl<T: Float> ops::Mul<&Matrix<T>> for ColumnVector<T> { - type Output = Option<ColumnVector<T>>; - - fn mul(self, matrix: &Matrix<T>) -> Self::Output { - self.mul_matrix(matrix) - } -} - -impl<T: Float> ops::MulAssign<&Matrix<T>> for RowVector<T> { - fn mul_assign(&mut self, matrix: &Matrix<T>) { - if let Some(result) = self.mul_matrix(matrix) { - *self = result; - } - } -} - -impl<T: Float> ops::MulAssign<&Matrix<T>> for ColumnVector<T> { - fn mul_assign(&mut self, matrix: &Matrix<T>) { - if let Some(result) = self.mul_matrix(matrix) { - *self = result; - } - } -} diff --git a/src/psi.h b/src/psi.h deleted file mode 100644 index 8c62319..0000000 --- a/src/psi.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#define PSI_VERSION_MAJOR 0 -#define PSI_VERSION_MINOR 1 -#define PSI_VERSION_PATCH 0 - -const char *psi_version(void); |
