aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libmu/src/lib.rs5
-rw-r--r--libmu/src/maths/complex.rs2
-rw-r--r--libmu/src/maths/matrix.rs39
-rw-r--r--libmu/src/maths/mod.rs1
-rw-r--r--libmu/src/maths/vector.rs45
-rw-r--r--mu/src/main.rs16
6 files changed, 79 insertions, 29 deletions
diff --git a/libmu/src/lib.rs b/libmu/src/lib.rs
index 1dea3c3..00ac5b6 100644
--- a/libmu/src/lib.rs
+++ b/libmu/src/lib.rs
@@ -1,3 +1,6 @@
mod maths;
-pub use maths::*;
+pub use maths::complex::*;
+pub use maths::matrix::*;
+pub use maths::numeric_types::*;
+pub use maths::vector::*;
diff --git a/libmu/src/maths/complex.rs b/libmu/src/maths/complex.rs
index ccae000..28a3121 100644
--- a/libmu/src/maths/complex.rs
+++ b/libmu/src/maths/complex.rs
@@ -1,4 +1,4 @@
-use crate::numeric_types::Float;
+use crate::Float;
use core::{fmt, ops};
#[macro_export]
diff --git a/libmu/src/maths/matrix.rs b/libmu/src/maths/matrix.rs
index 9724086..9932895 100644
--- a/libmu/src/maths/matrix.rs
+++ b/libmu/src/maths/matrix.rs
@@ -1,38 +1,29 @@
+// TODO(Hachem): Refactor
+
use super::Float;
use core::{fmt, ops};
#[macro_export]
macro_rules! matrix {
- ($( $( $x:expr ),* );* ) => {{
- let data = vec![
- $(
- $(
- $x,
- )*
- )*
- ];
+ ( $( $( $x:expr ),* );* ) => {{
+ let mut data = Vec::new();
+ let mut rows = 0;
+ let mut cols = 0;
- let rows = <[()]>::len(&[$( $crate::count_items!($($x),*) ),*]);
- let cols = $crate::count_items!($($x),*);
+ $(
+ let row_data = $( $x )*;
+ if cols == 0 {
+ cols = row_data.len();
+ }
+ assert_eq!(cols, row_data.len(), "All rows must have the same number of columns.");
+ data.extend(row_data);
+ rows += 1;
+ )*
$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<T: Float> {
pub data: Vec<T>,
pub rows: usize,
diff --git a/libmu/src/maths/mod.rs b/libmu/src/maths/mod.rs
index 651fcf2..734cfb4 100644
--- a/libmu/src/maths/mod.rs
+++ b/libmu/src/maths/mod.rs
@@ -6,4 +6,3 @@ pub mod vector;
pub use complex::*;
pub use matrix::*;
pub use numeric_types::*;
-pub use vector::*;
diff --git a/libmu/src/maths/vector.rs b/libmu/src/maths/vector.rs
index 682907e..c97b415 100644
--- a/libmu/src/maths/vector.rs
+++ b/libmu/src/maths/vector.rs
@@ -1,5 +1,8 @@
+// TODO(Hachem): Operators Matrix/Vector don't work
+// TODO(Hachem): Refactor
+
use super::{Float, Matrix};
-use core::ops;
+use core::{fmt, ops};
#[macro_export]
macro_rules! row_vector {
@@ -330,3 +333,43 @@ impl<T: Float> ops::MulAssign<&Matrix<T>> for ColumnVector<T> {
}
}
}
+
+impl<T: Float + fmt::Debug> fmt::Debug for RowVector<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "RowVector({:?})", self.0)
+ }
+}
+
+impl<T: Float + fmt::Debug> fmt::Debug for ColumnVector<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "ColumnVector({:?})", self.0)
+ }
+}
+
+impl<T: Float + fmt::Display> fmt::Display for RowVector<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "[{}]",
+ self.0
+ .iter()
+ .map(|x| x.to_string())
+ .collect::<Vec<String>>()
+ .join(", ")
+ )
+ }
+}
+
+impl<T: Float + fmt::Display> fmt::Display for ColumnVector<T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "[{}]",
+ self.0
+ .iter()
+ .map(|x| x.to_string())
+ .collect::<Vec<String>>()
+ .join(", ")
+ )
+ }
+}
diff --git a/mu/src/main.rs b/mu/src/main.rs
index f328e4d..a254401 100644
--- a/mu/src/main.rs
+++ b/mu/src/main.rs
@@ -1 +1,15 @@
-fn main() {}
+use libmu::*;
+
+fn main() {
+ #[rustfmt::skip]
+ let matrix = matrix![[1.0, 2.0, 3.0];
+ [4.0, 5.0, 6.0];
+ [7.0, 8.0, 9.0]];
+ let vector = row_vector![1.0, 2.0, 3.0];
+
+ let result = vector.mul_matrix(&matrix).unwrap();
+
+ println!("Matrix: \n{}", matrix);
+ println!("Vector: \n{}", vector);
+ println!("Result: \n{}", result);
+}