Module peroxide::structure::vector

source ·
Expand description

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::fuga::*;
    
    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
    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    fn main() {
        let a = c!(1,2,3,4);
    }

§From ranges to Vec<f64>

  • 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::fuga::*;
    
    fn main() {
        let a = seq(1, 4, 1);
        a.print();
        // [1, 2, 3, 4]
    }

§Vec<f64> Operation

There are two ways to do vector operations.

  • Use functional programming tools
  • Use redox

Here, I explain second method - for functional programming, see below.

To use redox, you only need to understand two things - ox(), red().

  • ox() : Makes vector to Redox<T: Vector>
  • red() : Makes Redox<T: Vector> to vector.
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = c!(1, 2, 3);
    assert_eq!((a.ox() * 2f64 - 1f64).red(), c!(1f64, 3f64, 5f64));
}

§Concatenation

There are two concatenation operations.

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

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

    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    fn main() {
        let a = c!(1,2,3,4);
        cat(0f64, &a).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 Vec<f64> to Matrix.

  • into(self) -> Matrix : Vec<f64> to column matrix

    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    fn main() {
        let a = c!(1,2,3,4);
        let a_col: Matrix = a.into();
        let m_col = matrix(c!(1,2,3,4), 4, 1, Col); // (4,1) Matrix
        assert_eq!(a_col, m_col);
    
        let m_row = matrix(c!(1,2,3,4), 1, 4, Row); // (1,4) Matrix
        assert_eq!(a_col.t(), m_row);
    }

§Functional Programming {#functional}

§FP for Vec<f64>

  • 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.

    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    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

    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    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

    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    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

    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    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

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

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

Functions§