Expand description
Extra tools for Vec<f64>
§Print 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] }
- Original way:
§Syntactic sugar for Vec<f64>
-
There is useful macro for
Vec<f64>
-
For
R
, there isc
# R a = c(1,2,3,4)
-
For
Peroxide
, there isc!
// 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 isseq
to declare sequence.# R a = seq(1, 4, 1) print(a) # [1] 1 2 3 4
-
For
peroxide
, there isseq
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 toRedox<T: Vector>
red()
: MakesRedox<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 formap
-
But different to original
map
- Onlyf64 -> 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 forfold
#[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 ofzip
&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 forfilter
#[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 fortake
#[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 forskip
#[macro_use] extern crate peroxide; use peroxide::fuga::*; fn main() { let a = c!(1,2,3,4); a.skip(2).print(); // [3, 4] }
Functions§
- Explicit version of
map
- Explicit version of
map
in parallel - Explicit version of
zip_with
in parallel - Explicit version of
reduce
- Explicit version of
zip_with