[][src]Module peroxide::structure::vector

Extra tools for Vec<f64>

  • There are two ways to print vector

    • Original way: print!("{:?}", a);
    • Peroxide way: a.print(); - Round-off to fourth digit
    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = vec![2f64.sqrt()];
        a.print(); // [1.4142]
    }

Syntactic sugar for Vec<f64>

  • There is useful macro for Vec<f64>

  • For R, there is c

    # R
    a = c(1,2,3,4)
    
  • For Peroxide, there is c!

    // Rust
    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
    }

From ranges to Vector

  • For R, there is seq to declare sequence.

    # R
    a = seq(1, 4, 1)
    print(a)
    # [1] 1 2 3 4
    
  • For peroxide, there is seq to declare sequence.

    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = seq(1, 4, 1);
        a.print();
        // [1, 2, 3, 4]
    }

Vector Operation

  • There are some vector-wise operations

    • add(&self, other: Vec<f64>) -> Vec<f64>
    • sub(&self, other: Vec<f64>) -> Vec<f64>
    • mul(&self, other: Vec<f64>) -> Vec<f64>
    • div(&self, other: Vec<f64>) -> Vec<f64>
    • dot(&self, other: Vec<f64>) -> f64
    • norm(&self) -> f64
    • sum(&self) -> f64
    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
        let b = c!(4,3,2,1);
    
        a.add(&b).print();
        a.sub(&b).print();
        a.mul(&b).print();
        a.div(&b).print();
        a.dot(&b).print();
        a.norm().print();
        a.sum().print();
    
        // [5, 5, 5, 5]
        // [-3, -1, 1, 3]
        // [4, 6, 6, 4]
        // [0.25, 0.6667, 1.5, 4]
        // 20
        // 5.477225575051661 // sqrt(30)
        // 10
    }
  • If you want to find more flexible operations, see Redox

Concatenation

There are two concatenation operations.

  • cat(T, Vec<T>) -> Vec<f64>

  • concat(Vec<T>, Vec<T>) -> Vec<T>

    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
        cat(0f64, a.clone()).print();
        // [0, 1, 2, 3, 4]
    
        let b = c!(5,6,7,8);
        concat(a, b).print();
        // [1, 2, 3, 4, 5, 6, 7, 8]
    }

Conversion to Matrix

There are two ways to convert vector to matrix.

  • to_matrix(&self) -> Matrix : Vector to column matrix

  • transpose(&self) -> Matrix : Vector to row matrix

    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
        let m_col = matrix(c!(1,2,3,4), 4, 1, Col); // (4,1) Matrix
        assert_eq!(a.to_matrix(), m_col);
    
        let m_row = matrix(c!(1,2,3,4), 1, 4, Row); // (1,4) Matrix
        assert_eq!(a.transpose(), m_row);
    }

Functional Programming {#functional}

FP for Vector

  • There are some functional programming tools for Vec<f64>

fmap

  • fmap is syntactic sugar for map

  • But different to original map - Only f64 -> f64 allowed.

    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
    
        // Original rust
        a.clone()
            .into_iter()
            .map(|x| x + 1f64)
            .collect::<Vec<f64>>()
            .print();
            // [2, 3, 4, 5]
    
        // fmap in Peroxide
        a.fmap(|x| x + 1f64).print();
        // [2, 3, 4, 5]
    }

reduce

  • reduce is syntactic sugar for fold

    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
    
        // Original rust
        a.clone()
            .into_iter()
            .fold(0f64, |x, y| x + y)
            .print(); // 10
    
        // reduce in Peroxide
        a.reduce(0f64, |x, y| x + y).print(); // 10
    }

zip_with

  • zip_with is composed of zip & map

    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
        let b = c!(5,6,7,8);
    
        // Original rust
        a.clone()
            .into_iter()
            .zip(&b)
            .map(|(x, y)| x + *y)
            .collect::<Vec<f64>>().print();
            // [6, 8, 10, 12]
    
        // zip_with in Peroxide
        zip_with(|x, y| x + y, &a, &b).print();
        // [6, 8, 10, 12]
    }

filter

  • filter is just syntactic sugar for filter

    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
        a.filter(|x| x > 2f64).print();
        // [3, 4]
    }

take & skip

  • take is syntactic sugar for take

    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
        a.take(2).print();
        // [1, 2]
    }
  • skip is syntactic sugar for skip

    extern crate peroxide;
    use peroxide::*;
    
    fn main() {
        let a = c!(1,2,3,4);
        a.skip(2).print();
        // [3, 4]
    }

Traits

Algorithm

Some algorithms for Vector

FPVector

Functional Programming tools for Vector

VecOps

Convenient Vector Operation trait

Functions

map
reduce
zip_with

Type Definitions

Vector