pub struct Series<R: Real, const N: usize> { /* private fields */ }
Expand description
Implements a ring buffer of length N that continuously and efficiently computes useful information such as mean and variance, or the minimum and maximum values.
All operations, except initialization, take time O(1).
The buffer can be accessed using indexing, where rolling[0] is the latest value, rolling[1], is the value inserted before, and so on.
Implementations§
Source§impl<R: Real, const N: usize> Series<R, N>
impl<R: Real, const N: usize> Series<R, N>
Sourcepub fn insert(&mut self, new: R)
pub fn insert(&mut self, new: R)
Inserts a new value into the rolling series.
Examples found in repository?
examples/overview.rs (line 19)
3fn main() {
4 let mut rolling = Series::from([1.0, 2.0, 3.0]);
5 // The latest value is at index 0.
6 assert_eq!(rolling[0], 3.0);
7 assert_eq!(rolling[1], 2.0);
8 assert_eq!(rolling[2], 1.0);
9 println!("The most recent value is {}.", rolling.curr());
10 assert_eq!(rolling.curr(), rolling[0]);
11
12 println!("The mean is {}.", rolling.mean());
13 println!("The variance is {}.", rolling.var());
14 println!("The standard derivation is {}.", rolling.stdev());
15 println!("The mean is {}.", rolling.mean());
16
17 // When inserting a new value, the oldest value
18 // is removed and the values below are recomputed.
19 rolling.insert(3.0);
20 assert_eq!(rolling[0], 3.0);
21 assert_eq!(rolling[1], 3.0);
22 assert_eq!(rolling[2], 2.0);
23
24 println!("The new mean is {}.", rolling.mean());
25 println!("The new variance is {}.", rolling.var());
26 println!("The new standard derivation is {}.", rolling.stdev());
27 println!("The new mean is {}.", rolling.mean());
28}
Sourcepub fn mean(&self) -> R
pub fn mean(&self) -> R
Returns the mean, or average.
Examples found in repository?
examples/overview.rs (line 12)
3fn main() {
4 let mut rolling = Series::from([1.0, 2.0, 3.0]);
5 // The latest value is at index 0.
6 assert_eq!(rolling[0], 3.0);
7 assert_eq!(rolling[1], 2.0);
8 assert_eq!(rolling[2], 1.0);
9 println!("The most recent value is {}.", rolling.curr());
10 assert_eq!(rolling.curr(), rolling[0]);
11
12 println!("The mean is {}.", rolling.mean());
13 println!("The variance is {}.", rolling.var());
14 println!("The standard derivation is {}.", rolling.stdev());
15 println!("The mean is {}.", rolling.mean());
16
17 // When inserting a new value, the oldest value
18 // is removed and the values below are recomputed.
19 rolling.insert(3.0);
20 assert_eq!(rolling[0], 3.0);
21 assert_eq!(rolling[1], 3.0);
22 assert_eq!(rolling[2], 2.0);
23
24 println!("The new mean is {}.", rolling.mean());
25 println!("The new variance is {}.", rolling.var());
26 println!("The new standard derivation is {}.", rolling.stdev());
27 println!("The new mean is {}.", rolling.mean());
28}
Sourcepub fn var(&self) -> R
pub fn var(&self) -> R
Returns the variance.
Examples found in repository?
examples/overview.rs (line 13)
3fn main() {
4 let mut rolling = Series::from([1.0, 2.0, 3.0]);
5 // The latest value is at index 0.
6 assert_eq!(rolling[0], 3.0);
7 assert_eq!(rolling[1], 2.0);
8 assert_eq!(rolling[2], 1.0);
9 println!("The most recent value is {}.", rolling.curr());
10 assert_eq!(rolling.curr(), rolling[0]);
11
12 println!("The mean is {}.", rolling.mean());
13 println!("The variance is {}.", rolling.var());
14 println!("The standard derivation is {}.", rolling.stdev());
15 println!("The mean is {}.", rolling.mean());
16
17 // When inserting a new value, the oldest value
18 // is removed and the values below are recomputed.
19 rolling.insert(3.0);
20 assert_eq!(rolling[0], 3.0);
21 assert_eq!(rolling[1], 3.0);
22 assert_eq!(rolling[2], 2.0);
23
24 println!("The new mean is {}.", rolling.mean());
25 println!("The new variance is {}.", rolling.var());
26 println!("The new standard derivation is {}.", rolling.stdev());
27 println!("The new mean is {}.", rolling.mean());
28}
Sourcepub fn stdev(&self) -> R
pub fn stdev(&self) -> R
Returns the standard derivation.
Examples found in repository?
examples/overview.rs (line 14)
3fn main() {
4 let mut rolling = Series::from([1.0, 2.0, 3.0]);
5 // The latest value is at index 0.
6 assert_eq!(rolling[0], 3.0);
7 assert_eq!(rolling[1], 2.0);
8 assert_eq!(rolling[2], 1.0);
9 println!("The most recent value is {}.", rolling.curr());
10 assert_eq!(rolling.curr(), rolling[0]);
11
12 println!("The mean is {}.", rolling.mean());
13 println!("The variance is {}.", rolling.var());
14 println!("The standard derivation is {}.", rolling.stdev());
15 println!("The mean is {}.", rolling.mean());
16
17 // When inserting a new value, the oldest value
18 // is removed and the values below are recomputed.
19 rolling.insert(3.0);
20 assert_eq!(rolling[0], 3.0);
21 assert_eq!(rolling[1], 3.0);
22 assert_eq!(rolling[2], 2.0);
23
24 println!("The new mean is {}.", rolling.mean());
25 println!("The new variance is {}.", rolling.var());
26 println!("The new standard derivation is {}.", rolling.stdev());
27 println!("The new mean is {}.", rolling.mean());
28}
Sourcepub fn curr(&self) -> R
pub fn curr(&self) -> R
Returns the latest value.
Examples found in repository?
examples/overview.rs (line 9)
3fn main() {
4 let mut rolling = Series::from([1.0, 2.0, 3.0]);
5 // The latest value is at index 0.
6 assert_eq!(rolling[0], 3.0);
7 assert_eq!(rolling[1], 2.0);
8 assert_eq!(rolling[2], 1.0);
9 println!("The most recent value is {}.", rolling.curr());
10 assert_eq!(rolling.curr(), rolling[0]);
11
12 println!("The mean is {}.", rolling.mean());
13 println!("The variance is {}.", rolling.var());
14 println!("The standard derivation is {}.", rolling.stdev());
15 println!("The mean is {}.", rolling.mean());
16
17 // When inserting a new value, the oldest value
18 // is removed and the values below are recomputed.
19 rolling.insert(3.0);
20 assert_eq!(rolling[0], 3.0);
21 assert_eq!(rolling[1], 3.0);
22 assert_eq!(rolling[2], 2.0);
23
24 println!("The new mean is {}.", rolling.mean());
25 println!("The new variance is {}.", rolling.var());
26 println!("The new standard derivation is {}.", rolling.stdev());
27 println!("The new mean is {}.", rolling.mean());
28}
Trait Implementations§
impl<R: Copy + Real, const N: usize> Copy for Series<R, N>
Auto Trait Implementations§
impl<R, const N: usize> Freeze for Series<R, N>where
R: Freeze,
impl<R, const N: usize> RefUnwindSafe for Series<R, N>where
R: RefUnwindSafe,
impl<R, const N: usize> Send for Series<R, N>where
R: Send,
impl<R, const N: usize> Sync for Series<R, N>where
R: Sync,
impl<R, const N: usize> Unpin for Series<R, N>where
R: Unpin,
impl<R, const N: usize> UnwindSafe for Series<R, N>where
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more