From 8f35a08d215246823bbce610e3b68da7443272b9 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 26 Apr 2025 17:43:22 +0100 Subject: [PATCH] Refactor README example to improve type annotations and clarity in matrix operations --- README.md | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 93624e0..d44e91c 100644 --- a/README.md +++ b/README.md @@ -53,34 +53,35 @@ let dates: Vec = .list() .unwrap(); -let col_names = vec!["a".to_string(), "b".to_string()]; + let col_names: Vec = vec!["a".to_string(), "b".to_string()]; -let ma = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0, 4.0], vec![5.0, 6.0, 7.0, 8.0]]); -let mb = Matrix::from_cols(vec![vec![4.0, 3.0, 2.0, 1.0], vec![8.0, 7.0, 6.0, 5.0]]); + let ma: Matrix = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0, 4.0], vec![5.0, 6.0, 7.0, 8.0]]); + let mb: Matrix = Matrix::from_cols(vec![vec![4.0, 3.0, 2.0, 1.0], vec![8.0, 7.0, 6.0, 5.0]]); -let fa = Frame::new( - ma.clone(), - col_names.clone(), - Some(RowIndex::Date(dates.clone())), -); -let fb = Frame::new(mb, col_names, Some(RowIndex::Date(dates))); + let fa: Frame = Frame::new( + ma.clone(), + col_names.clone(), + Some(RowIndex::Date(dates.clone())), + ); + let fb: Frame = Frame::new(mb, col_names, Some(RowIndex::Date(dates))); -// Math that reads like math -let result = &fa * &fb; // element‑wise multiply -let total = result.sum_vertical().iter().sum::(); -assert_eq!(total, 184.0); + // Math that reads like math + let result: Frame = &fa * &fb; // element‑wise multiply + let total: f64 = result.sum_vertical().iter().sum::(); + assert_eq!(total, 184.0); -let result = &ma + 1.0; // add scalar -let result = &result - 1.0; // subtract scalar -let result = &result * 2.0; // multiply by scalar -let result = &result / 2.0; // divide by scalar + // broadcast & reduce + let result: Matrix = &ma + 1.0; // add scalar + let result: Matrix = &result - 1.0; // subtract scalar + let result: Matrix = &result * 2.0; // multiply by scalar + let result: Matrix = &result / 2.0; // divide by scalar -let check = result.eq_elementwise(ma.clone()).all(); -assert!(check); + let check: bool = result.eq_elementwise(ma.clone()).all(); + assert!(check); -// The above math can also be written as: -let check = &(&(&(&(&ma + 1.0) - 1.0) * 2.0) / 2.0) - .eq_elementwise(ma) - .all(); -assert!(check); + // The above math can also be written as: + let check: bool = (&(&(&(&ma + 1.0) - 1.0) * 2.0) / 2.0) + .eq_elementwise(ma) + .all(); + assert!(check); ```