mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 04:00:01 +00:00
fix README code
This commit is contained in:
parent
28d052af6a
commit
67d735d08e
52
README.md
52
README.md
@ -53,35 +53,37 @@ let dates: Vec<NaiveDate> =
|
|||||||
.list()
|
.list()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let col_names: Vec<String> = vec!["a".to_string(), "b".to_string()];
|
let col_names: Vec<String> = vec!["a".to_string(), "b".to_string()];
|
||||||
|
|
||||||
let ma: Matrix<f64> = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0, 4.0], vec![5.0, 6.0, 7.0, 8.0]]);
|
let ma: Matrix<f64> =
|
||||||
let mb: Matrix<f64> = Matrix::from_cols(vec![vec![4.0, 3.0, 2.0, 1.0], vec![8.0, 7.0, 6.0, 5.0]]);
|
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<f64> =
|
||||||
|
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<f64> = Frame::new(
|
let fa: Frame<f64> = Frame::new(
|
||||||
ma.clone(),
|
ma.clone(),
|
||||||
col_names.clone(),
|
Some(col_names.clone()),
|
||||||
Some(RowIndex::Date(dates.clone())),
|
Some(RowIndex::Date(dates.clone())),
|
||||||
);
|
);
|
||||||
let fb: Frame<f64> = Frame::new(mb, col_names, Some(RowIndex::Date(dates)));
|
let fb: Frame<f64> = Frame::new(mb, Some(col_names), Some(RowIndex::Date(dates)));
|
||||||
|
|
||||||
// Math that reads like math
|
// Math that reads like math
|
||||||
let result: Frame<f64> = &fa * &fb; // element‑wise multiply
|
let result: Frame<f64> = &fa * &fb; // element‑wise multiply
|
||||||
let total: f64 = result.sum_vertical().iter().sum::<f64>();
|
let total: f64 = result.sum_vertical().iter().sum::<f64>();
|
||||||
assert_eq!(total, 184.0);
|
assert_eq!(total, 184.0);
|
||||||
|
|
||||||
// broadcast & reduce
|
// broadcast & reduce
|
||||||
let result: Matrix<f64> = &ma + 1.0; // add scalar
|
let result: Matrix<f64> = &ma + 1.0; // add scalar
|
||||||
let result: Matrix<f64> = &result - 1.0; // subtract scalar
|
let result: Matrix<f64> = &result - 1.0; // subtract scalar
|
||||||
let result: Matrix<f64> = &result * 2.0; // multiply by scalar
|
let result: Matrix<f64> = &result * 2.0; // multiply by scalar
|
||||||
let result: Matrix<f64> = &result / 2.0; // divide by scalar
|
let result: Matrix<f64> = &result / 2.0; // divide by scalar
|
||||||
|
|
||||||
let check: bool = result.eq_elementwise(ma.clone()).all();
|
let check: bool = result.eq_elementwise(ma.clone()).all();
|
||||||
assert!(check);
|
assert!(check);
|
||||||
|
|
||||||
// The above math can also be written as:
|
// The above math can also be written as:
|
||||||
let check: bool = (&(&(&(&ma + 1.0) - 1.0) * 2.0) / 2.0)
|
let check: bool = (&(&(&(&ma + 1.0) - 1.0) * 2.0) / 2.0)
|
||||||
.eq_elementwise(ma)
|
.eq_elementwise(ma)
|
||||||
.all();
|
.all();
|
||||||
assert!(check);
|
assert!(check);
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user