From 565af48c1eff190ce836293a0eda75c0dc695c82 Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 6 Sep 2026 18:53:09 +0200 Subject: refactor: split public headers into include/ --- include/maths/complex.h | 28 ++++++++ include/maths/matrix.h | 36 ++++++++++ include/maths/vector.h | 46 +++++++++++++ include/psi.h | 7 ++ premake5.lua | 8 ++- src/maths/complex.h | 28 -------- src/maths/complex.rs | 180 ------------------------------------------------ src/maths/matrix.h | 35 ---------- src/maths/numeric.rs | 101 --------------------------- src/maths/vector.h | 46 ------------- src/maths/vector_ops.rs | 107 ---------------------------- src/psi.h | 7 -- 12 files changed, 123 insertions(+), 506 deletions(-) create mode 100644 include/maths/complex.h create mode 100644 include/maths/matrix.h create mode 100644 include/maths/vector.h create mode 100644 include/psi.h delete mode 100644 src/maths/complex.h delete mode 100644 src/maths/complex.rs delete mode 100644 src/maths/matrix.h delete mode 100644 src/maths/numeric.rs delete mode 100644 src/maths/vector.h delete mode 100644 src/maths/vector_ops.rs delete mode 100644 src/psi.h diff --git a/include/maths/complex.h b/include/maths/complex.h new file mode 100644 index 0000000..c94f64a --- /dev/null +++ b/include/maths/complex.h @@ -0,0 +1,28 @@ +#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/include/maths/matrix.h b/include/maths/matrix.h new file mode 100644 index 0000000..c4e5530 --- /dev/null +++ b/include/maths/matrix.h @@ -0,0 +1,36 @@ +#pragma once + +#include + +#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_identity_matrix(size_t size); +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/include/maths/vector.h b/include/maths/vector.h new file mode 100644 index 0000000..d452ef1 --- /dev/null +++ b/include/maths/vector.h @@ -0,0 +1,46 @@ +#pragma once + +#include + +#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/include/psi.h b/include/psi.h new file mode 100644 index 0000000..8c62319 --- /dev/null +++ b/include/psi.h @@ -0,0 +1,7 @@ +#pragma once + +#define PSI_VERSION_MAJOR 0 +#define PSI_VERSION_MINOR 1 +#define PSI_VERSION_PATCH 0 + +const char *psi_version(void); diff --git a/premake5.lua b/premake5.lua index 8038a9a..f2d2d2d 100644 --- a/premake5.lua +++ b/premake5.lua @@ -65,11 +65,15 @@ project "psi" setup_c_target() files { + "include/**.h", "src/**.h", "src/**.c", } - includedirs "src" + includedirs { + "include", + "src", + } filter { "system:macosx or system:linux" } buildoptions { @@ -103,7 +107,7 @@ project "tester" } includedirs { - "src", + "include", "tester", } 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 ops::$trait for Complex { - type Output = Complex; - - fn $method(self, other: Complex) -> Complex { - Complex { - real: self.real $op other.real, - imaginary: self.imaginary $op other.imaginary, - } - } - } - }; - - ($trait:ident, $method:ident, $op:tt, real) => { - impl ops::$trait for Complex { - type Output = Complex; - - fn $method(self, other: T) -> Complex { - Complex { - real: self.real $op other, - imaginary: self.imaginary, - } - } - } - }; - - ($trait_assign:ident, $method_assign:ident, $op:tt, assign) => { - impl ops::$trait_assign for Complex { - fn $method_assign(&mut self, other: Complex) { - 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 ops::$trait_assign for Complex { - fn $method_assign(&mut self, other: T) { - self.real = self.real $op other; - } - } - }; -} - -#[derive(Copy, Clone, PartialOrd, PartialEq)] -pub struct Complex { - pub real: T, - pub imaginary: T, -} - -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 ops::Neg for Complex { - type Output = Complex; - - fn neg(self) -> Complex { - Complex { - real: -self.real, - imaginary: -self.imaginary, - } - } -} - -impl From for Complex { - fn from(real: T) -> Complex { - Complex { - real, - imaginary: T::zero(), - } - } -} - -impl Complex { - pub fn new(real: T, imaginary: T) -> Complex { - Complex { real, imaginary } - } - - pub fn get_conjugate(&self) -> Complex { - 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 ops::Mul for Complex { - type Output = Complex; - - fn mul(self, other: Complex) -> Complex { - // (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 ops::Div for Complex { - type Output = Complex; - - fn div(self, other: Complex) -> Complex { - // (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 ops::MulAssign for Complex { - fn mul_assign(&mut self, other: Complex) { - 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 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; - 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 - -#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 - + ops::Mul - + ops::Sub - + ops::Div - + ops::Neg - + 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 - -#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 ops::Add<&VectorImpl> - for VectorImpl -{ - type Output = Option>; - - fn add(self, other: &VectorImpl) -> Self::Output { - self.add_to(other) - } -} - -impl ops::Sub<&VectorImpl> - for VectorImpl -{ - type Output = Option>; - - fn sub(self, other: &VectorImpl) -> Self::Output { - self.subtract(other) - } -} - -impl ops::Mul for VectorImpl { - type Output = VectorImpl; - - fn mul(self, scalar: T) -> Self::Output { - self.scale(scalar) - } -} - -impl ops::Div for VectorImpl { - type Output = VectorImpl; - - fn div(self, scalar: T) -> Self::Output { - self.scale(T::one() / scalar) - } -} - -impl ops::AddAssign> - for VectorImpl -{ - fn add_assign(&mut self, other: VectorImpl) { - if let Some(result) = self.add_to(&other) { - *self = result; - } - } -} - -impl ops::SubAssign> - for VectorImpl -{ - fn sub_assign(&mut self, other: VectorImpl) { - if let Some(result) = self.subtract(&other) { - *self = result; - } - } -} - -impl ops::MulAssign - for VectorImpl -{ - fn mul_assign(&mut self, scalar: T) { - *self = self.scale(scalar); - } -} - -impl ops::DivAssign - for VectorImpl -{ - fn div_assign(&mut self, scalar: T) { - *self = self.scale(T::one() / scalar); - } -} - -impl ops::Mul<&Matrix> for RowVector { - type Output = Option>; - - fn mul(self, matrix: &Matrix) -> Self::Output { - self.mul_matrix(matrix) - } -} - -impl ops::Mul<&Matrix> for ColumnVector { - type Output = Option>; - - fn mul(self, matrix: &Matrix) -> Self::Output { - self.mul_matrix(matrix) - } -} - -impl ops::MulAssign<&Matrix> for RowVector { - fn mul_assign(&mut self, matrix: &Matrix) { - if let Some(result) = self.mul_matrix(matrix) { - *self = result; - } - } -} - -impl ops::MulAssign<&Matrix> for ColumnVector { - fn mul_assign(&mut self, matrix: &Matrix) { - 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); -- cgit v1.3