Struct Series

Source
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>

Source

pub fn new() -> Self

Create a new rolling series. All values are initialized to zero.

Source

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}
Source

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}
Source

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}
Source

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}
Source

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}
Source

pub fn norm(&self) -> R

Returns the latest value, normalized. If the standard derivation is zero, return zero.

Source

pub fn sum(&self) -> R

Returns the sum of all entries.

Trait Implementations§

Source§

impl<R: Clone + Real, const N: usize> Clone for Series<R, N>

Source§

fn clone(&self) -> Series<R, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: Debug + Real, const N: usize> Debug for Series<R, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R: Real, const N: usize> Default for Series<R, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<R: Real, const N: usize> From<[R; N]> for Series<R, N>

Source§

fn from(buf: [R; N]) -> Self

Converts to this type from the input type.
Source§

impl<R: Real, const N: usize> Index<usize> for Series<R, N>

Source§

type Output = R

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.