Trait plotpy::AsVector[][src]

pub trait AsVector<'a, U: 'a> {
    fn vec_size(&self) -> usize;
fn vec_at(&self, i: usize) -> U; }
Expand description

Defines a trait to handle Vector-like data

Example

use plotpy::AsVector;
use russell_lab::Vector;

fn sum<'a, T, U>(array: &'a T) -> f64
where
    T: AsVector<'a, U>,
    U: 'a + Into<f64>,
{
    let mut res = 0.0;
    let m = array.vec_size();
    for i in 0..m {
        res += array.vec_at(i).into();
    }
    res
}

// heap-allocated 1D array (vector)
let x = vec![1.0, 2.0, 3.0];
assert_eq!(sum(&x), 6.0);

// heap-allocated 1D array (slice)
let y: &[f64] = &[10.0, 20.0, 30.0];
assert_eq!(sum(&y), 60.0);

// stack-allocated (fixed-size) 2D array
let z = [100.0, 200.0, 300.0];
assert_eq!(sum(&z), 600.0);

// using Vector
let w = Vector::from(&[5.0, 5.0, 5.0]);
assert_eq!(sum(&w), 15.0);

Required methods

Returns the size of the vector

Returns the value at index i

Implementations on Foreign Types

Defines a heap-allocated 1D array (vector)

Defines a heap-allocated 1D array (slice)

Defines a stack-allocated (fixed-size) 1D array

Handles Vector

Implementors