From 5abda23c3ed21db3c0a4541b85b6e2e916ce569e Mon Sep 17 00:00:00 2001 From: hachem Date: Mon, 16 Sep 2024 20:53:46 +0200 Subject: Matrix and vector products + macros --- libmu/src/maths/matrix.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'libmu/src/maths/matrix.rs') diff --git a/libmu/src/maths/matrix.rs b/libmu/src/maths/matrix.rs index 7662652..9724086 100644 --- a/libmu/src/maths/matrix.rs +++ b/libmu/src/maths/matrix.rs @@ -1,6 +1,38 @@ use super::Float; use core::{fmt, ops}; +#[macro_export] +macro_rules! matrix { + ($( $( $x:expr ),* );* ) => {{ + let data = vec![ + $( + $( + $x, + )* + )* + ]; + + let rows = <[()]>::len(&[$( $crate::count_items!($($x),*) ),*]); + let cols = $crate::count_items!($($x),*); + + $crate::Matrix::new(rows, cols, data) + }}; +} + +#[macro_export] +macro_rules! count_items { + ($($item:expr),*) => { + <[()]>::len(&[$( $crate::replace_expr!($item ())),*]) + }; +} + +#[macro_export] +macro_rules! replace_expr { + ($_t:tt $sub:expr) => { + $sub + }; +} + pub struct Matrix { pub data: Vec, pub rows: usize, -- cgit v1.3