Trait arr_rs::traits::create::ArrayCreate
source · pub trait ArrayCreate<N: Numeric>where
Self: Sized + Clone,{
Show 21 methods
// Required methods
fn new(elements: Vec<N>, shape: Vec<usize>) -> Result<Self, ArrayError>;
fn single(element: N) -> Result<Self, ArrayError>;
fn flat(elements: Vec<N>) -> Result<Self, ArrayError>;
fn rand(shape: Vec<usize>) -> Result<Self, ArrayError>;
fn empty() -> Result<Self, ArrayError>;
fn eye(
n: usize,
m: Option<usize>,
k: Option<usize>
) -> Result<Self, ArrayError>;
fn identity(dim: usize) -> Result<Self, ArrayError>;
fn zeros(shape: Vec<usize>) -> Result<Self, ArrayError>;
fn zeros_like(other: &Self) -> Result<Self, ArrayError>;
fn ones(shape: Vec<usize>) -> Result<Self, ArrayError>;
fn ones_like(other: &Self) -> Result<Self, ArrayError>;
fn full(shape: Vec<usize>, fill_value: N) -> Result<Self, ArrayError>;
fn full_like(other: &Self, fill_value: N) -> Result<Self, ArrayError>;
fn arange(start: N, stop: N, step: Option<N>) -> Result<Self, ArrayError>;
fn linspace(
start: N,
stop: N,
num: Option<usize>,
endpoint: Option<bool>
) -> Result<Self, ArrayError>;
fn linspace_a(
start: &Self,
stop: &Self,
num: Option<usize>,
endpoint: Option<bool>
) -> Result<Self, ArrayError>;
fn logspace(
start: N,
stop: N,
num: Option<usize>,
endpoint: Option<bool>,
base: Option<usize>
) -> Result<Self, ArrayError>;
fn logspace_a(
start: &Self,
stop: &Self,
num: Option<usize>,
endpoint: Option<bool>,
base: Option<&Array<usize>>
) -> Result<Self, ArrayError>;
fn geomspace(
start: N,
stop: N,
num: Option<usize>,
endpoint: Option<bool>
) -> Result<Self, ArrayError>;
fn geomspace_a(
start: &Self,
stop: &Self,
num: Option<usize>,
endpoint: Option<bool>
) -> Result<Self, ArrayError>;
fn tri(
n: usize,
m: Option<usize>,
k: Option<isize>
) -> Result<Self, ArrayError>;
}Expand description
ArrayTrait - Array Create functions
Required Methods§
sourcefn new(elements: Vec<N>, shape: Vec<usize>) -> Result<Self, ArrayError>
fn new(elements: Vec<N>, shape: Vec<usize>) -> Result<Self, ArrayError>
Creates new array
Arguments
elements- vector representing array elementsshape- vector representing array elements
Examples
use arr_rs::prelude::*;
let arr = Array::new(vec![1, 2, 3, 4], vec![4]).unwrap();
assert_eq!("[1, 2, 3, 4]", format!("{arr}"));
let arr = Array::new(vec![1, 2, 3, 4], vec![2, 2]).unwrap();
assert_eq!("[[1, 2], [3, 4]]", format!("{arr}"));
assert_eq!("[[1, 2], \n [3, 4]]", format!("{arr:#}"));sourcefn single(element: N) -> Result<Self, ArrayError>
fn single(element: N) -> Result<Self, ArrayError>
sourcefn flat(elements: Vec<N>) -> Result<Self, ArrayError>
fn flat(elements: Vec<N>) -> Result<Self, ArrayError>
sourcefn empty() -> Result<Self, ArrayError>
fn empty() -> Result<Self, ArrayError>
Creates new empty array
Examples
use arr_rs::prelude::*;
assert!(Array::<i32>::empty().is_empty().unwrap());sourcefn eye(n: usize, m: Option<usize>, k: Option<usize>) -> Result<Self, ArrayError>
fn eye(n: usize, m: Option<usize>, k: Option<usize>) -> Result<Self, ArrayError>
Creates new 2d array with ones on the diagonal and zeros elsewhere
Arguments
n- number of rowsm- number of columns. optional, defaulted to nk- index of diagonal. optional, defaulted to 0
Examples
use arr_rs::prelude::*;
assert_eq!(array!([[1, 0, 0], [0, 1, 0]]), Array::<i32>::eye(2, Some(3), Some(0)));
assert_eq!(array!([[0, 1, 0], [0, 0, 1]]), Array::<i32>::eye(2, Some(3), Some(1)));sourcefn identity(dim: usize) -> Result<Self, ArrayError>
fn identity(dim: usize) -> Result<Self, ArrayError>
sourcefn zeros_like(other: &Self) -> Result<Self, ArrayError>
fn zeros_like(other: &Self) -> Result<Self, ArrayError>
sourcefn ones_like(other: &Self) -> Result<Self, ArrayError>
fn ones_like(other: &Self) -> Result<Self, ArrayError>
sourcefn full_like(other: &Self, fill_value: N) -> Result<Self, ArrayError>
fn full_like(other: &Self, fill_value: N) -> Result<Self, ArrayError>
sourcefn arange(start: N, stop: N, step: Option<N>) -> Result<Self, ArrayError>
fn arange(start: N, stop: N, step: Option<N>) -> Result<Self, ArrayError>
sourcefn linspace(
start: N,
stop: N,
num: Option<usize>,
endpoint: Option<bool>
) -> Result<Self, ArrayError>
fn linspace( start: N, stop: N, num: Option<usize>, endpoint: Option<bool> ) -> Result<Self, ArrayError>
Creates new array of numbers evenly spaced over a specified interval
Arguments
start- start of intervalstop- end of intervalnum- number of samples to generate. optional, defaults to 50endpoint- determines ifstopshould be included in returned data. optional, defaults to true
Examples
use arr_rs::prelude::*;
assert_eq!(array!([0., 2.5, 5.]), Array::<f64>::linspace(0., 5., Some(3), None));sourcefn linspace_a(
start: &Self,
stop: &Self,
num: Option<usize>,
endpoint: Option<bool>
) -> Result<Self, ArrayError>
fn linspace_a( start: &Self, stop: &Self, num: Option<usize>, endpoint: Option<bool> ) -> Result<Self, ArrayError>
Creates new array of numbers evenly spaced over a specified interval
Arguments
start- start of intervalstop- end of intervalnum- number of samples to generate. optional, defaults to 50endpoint- determines ifstopshould be included in returned data. optional, defaults to true
Examples
use arr_rs::prelude::*;
let arr: Array<f64> = Array::linspace_a(&array!([0., 2.]).unwrap(), &array!([4., 6.]).unwrap(), Some(3), None).unwrap();
assert_eq!(array!([[0., 2.], [2., 4.], [4., 6.]]).unwrap(), arr);
let arr: Array<f64> = Array::linspace_a(&array!([[0., 2.], [2., 4.]]).unwrap(), &array!([[4., 6.], [6., 8.]]).unwrap(), Some(3), None).unwrap();
assert_eq!(array!([[[0., 2.], [2., 4.]], [[2., 4.], [4., 6.]], [[4., 6.], [6., 8.]]]).unwrap(), arr);sourcefn logspace(
start: N,
stop: N,
num: Option<usize>,
endpoint: Option<bool>,
base: Option<usize>
) -> Result<Self, ArrayError>
fn logspace( start: N, stop: N, num: Option<usize>, endpoint: Option<bool>, base: Option<usize> ) -> Result<Self, ArrayError>
Creates new array of numbers evenly spaced on a log scale over a specified interval
Arguments
start- start of intervalstop- end of intervalnum- number of samples to generate. optional, defaults to 50endpoint- determines ifstopshould be included in returned data. optional, defaults to truebase- the base of the log space. optional, defaults to 10
Examples
use arr_rs::prelude::*;
assert_eq!(array!([1., 316.22776601683796, 100000.]), Array::<f64>::logspace(0., 5., Some(3), None, None));sourcefn logspace_a(
start: &Self,
stop: &Self,
num: Option<usize>,
endpoint: Option<bool>,
base: Option<&Array<usize>>
) -> Result<Self, ArrayError>
fn logspace_a( start: &Self, stop: &Self, num: Option<usize>, endpoint: Option<bool>, base: Option<&Array<usize>> ) -> Result<Self, ArrayError>
Creates new array of numbers evenly spaced on a log scale over a specified interval
Arguments
start- start of intervalstop- end of intervalnum- number of samples to generate. optional, defaults to 50endpoint- determines ifstopshould be included in returned data. optional, defaults to truebase- the base of the log space. optional, defaults to 10
Examples
use arr_rs::prelude::*;
let arr: Array<f64> = Array::logspace_a(&array!([0., 2.]).unwrap(), &array!([4., 6.]).unwrap(), Some(3), None, None).unwrap();
assert_eq!(array!([[1., 100.], [100., 10000.], [10000., 1000000.]]).unwrap(), arr);
let arr: Array<f64> = Array::logspace_a(&array!([[0., 2.], [2., 4.]]).unwrap(), &array!([[4., 6.], [6., 8.]]).unwrap(), Some(3), None, None).unwrap();
assert_eq!(array!([[[1., 100.], [100., 10000.]], [[100., 10000.], [10000., 1000000.]], [[10000., 1000000.], [1000000., 100000000.]]]).unwrap(), arr);sourcefn geomspace(
start: N,
stop: N,
num: Option<usize>,
endpoint: Option<bool>
) -> Result<Self, ArrayError>
fn geomspace( start: N, stop: N, num: Option<usize>, endpoint: Option<bool> ) -> Result<Self, ArrayError>
Creates new array of numbers evenly spaced on a log scale (a geometric progression) over a specified interval
Arguments
start- start of intervalstop- end of intervalnum- number of samples to generate. optional, defaults to 50endpoint- determines ifstopshould be included in returned data. optional, defaults to true
Examples
use arr_rs::prelude::*;
let expected: Array<f64> = array!([1., 10., 100., 1000.]).unwrap();
let arr: Array<f64> = Array::geomspace(1., 1000., Some(4), None).unwrap();
assert_eq!(format!("{expected:.4}"), format!("{arr:.4}"));sourcefn geomspace_a(
start: &Self,
stop: &Self,
num: Option<usize>,
endpoint: Option<bool>
) -> Result<Self, ArrayError>
fn geomspace_a( start: &Self, stop: &Self, num: Option<usize>, endpoint: Option<bool> ) -> Result<Self, ArrayError>
Creates new array of numbers evenly spaced on a log scale (a geometric progression) over a specified interval
Arguments
start- start of intervalstop- end of intervalnum- number of samples to generate. optional, defaults to 50endpoint- determines ifstopshould be included in returned data. optional, defaults to true
Examples
use arr_rs::prelude::*;
let expected: Array<f64> = array!([[1., 10.], [10., 100.], [100., 1000.], [1000., 10000.]]).unwrap();
let arr: Array<f64> = Array::geomspace_a(&array!([1., 10.]).unwrap(), &array!([1000., 10000.]).unwrap(), Some(4), None).unwrap();
assert_eq!(format!("{expected:.4}"), format!("{arr:.4}"));
let expected: Array<f64> = array!([[[1., 10.], [10., 100.]], [[10., 100.], [100., 1000.]], [[100., 1000.], [1000., 10000.]]]).unwrap();
let arr: Array<f64> = Array::geomspace_a(&array!([[1., 10.], [10., 100.]]).unwrap(), &array!([[100., 1000.], [1000., 10000.]]).unwrap(), Some(3), None).unwrap();
assert_eq!(format!("{expected:.4}"), format!("{arr:.4}"));sourcefn tri(n: usize, m: Option<usize>, k: Option<isize>) -> Result<Self, ArrayError>
fn tri(n: usize, m: Option<usize>, k: Option<isize>) -> Result<Self, ArrayError>
Construct an array with ones at and below the given diagonal and zeros elsewhere
Arguments
n- number of rowsm- number of columns. optional, defaults tonk- chosen diagonal. optional, defaults to 0
Examples
use arr_rs::prelude::*;
let expected: Array<i32> = array!([[1, 0], [1, 1]]).unwrap();
assert_eq!(expected, Array::<i32>::tri(2, Some(2), None).unwrap());
let expected: Array<i32> = array!([[1, 0, 0], [1, 1, 0], [1, 1, 1]]).unwrap();
assert_eq!(expected, Array::<i32>::tri(3, Some(3), None).unwrap());