Trait ArrayCreateFrom

Source
pub trait ArrayCreateFrom<N: Numeric>
where Array<N>: Sized + Clone,
{ // Required methods fn diag(&self, k: Option<isize>) -> Result<Array<N>, ArrayError>; fn diagflat(&self, k: Option<isize>) -> Result<Array<N>, ArrayError>; fn tril(&self, k: Option<isize>) -> Result<Array<N>, ArrayError>; fn triu(&self, k: Option<isize>) -> Result<Array<N>, ArrayError>; fn vander( &self, n: Option<usize>, increasing: Option<bool>, ) -> Result<Self, ArrayError> where Self: Sized; }
Expand description

ArrayTrait - Array Create functions

Required Methods§

Source

fn diag(&self, k: Option<isize>) -> Result<Array<N>, ArrayError>

Extract a diagonal or construct a diagonal array

§Arguments
  • k - chosen diagonal. optional, defaults to 0
§Examples
use arr_rs::prelude::*;

let expected = array!(i32, [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]).unwrap();
let arr = Array::diag(&array!(i32, [1, 2, 3, 4]).unwrap(), None).unwrap();
assert_eq!(expected, arr);

let expected = array!(i32, [1, 2, 3, 4]).unwrap();
let arr = Array::diag(&array!(i32, [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]).unwrap(), None).unwrap();
assert_eq!(expected, arr);

let expected = array!(i32, [0, 0, 0]).unwrap();
let arr = Array::diag(&array!(i32, [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]).unwrap(), Some(1)).unwrap();
assert_eq!(expected, arr);
§Errors

may returns ArrayError

Source

fn diagflat(&self, k: Option<isize>) -> Result<Array<N>, ArrayError>

Construct a diagonal array for flattened input

§Arguments
  • data - input array
  • k - chosen diagonal. optional, defaults to 0
§Examples
use arr_rs::prelude::*;

let expected = array!(i32, [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]).unwrap();
let arr = Array::diagflat(&array!(i32, [1, 2, 3, 4]).unwrap(), None).unwrap();
assert_eq!(expected, arr);

let expected = array!(i32, [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]).unwrap();
let arr = Array::diagflat(&array!(i32, [[1, 2], [3, 4]]).unwrap(), None).unwrap();
assert_eq!(expected, arr);
§Errors

may returns ArrayError

Source

fn tril(&self, k: Option<isize>) -> Result<Array<N>, ArrayError>

Return a copy of an array with elements above the k-th diagonal zeroed. For arrays with ndim exceeding 2, tril will apply to the final two axes.

§Arguments
  • k - chosen diagonal. optional, defaults to 0
§Examples
use arr_rs::prelude::*;

let arr = array_arange!(i32, 1, 8).reshape(&[2, 4]).unwrap();
let expected = array!(i32, [[1, 0, 0, 0], [5, 6, 0, 0]]).unwrap();
assert_eq!(expected, arr.tril(None).unwrap());

let arr = array_arange!(i32, 1, 8).reshape(&[2, 2, 2]).unwrap();
let expected = array!(i32, [[[1, 0], [3, 4]], [[5, 0], [7, 8]]]).unwrap();
assert_eq!(expected, arr.tril(None).unwrap());
§Errors

may returns ArrayError

Source

fn triu(&self, k: Option<isize>) -> Result<Array<N>, ArrayError>

Return a copy of an array with elements below the k-th diagonal zeroed. For arrays with ndim exceeding 2, triu will apply to the final two axes.

§Arguments
  • k - chosen diagonal. optional, defaults to 0
§Examples
use arr_rs::prelude::*;

let arr = array_arange!(i32, 1, 8).reshape(&[2, 4]).unwrap();
let expected = array!(i32, [[1, 2, 3, 4], [0, 6, 7, 8]]).unwrap();
assert_eq!(expected, arr.triu(None).unwrap());

let arr = array_arange!(i32, 1, 8).reshape(&[2, 2, 2]).unwrap();
let expected = array!(i32, [[[1, 2], [0, 4]], [[5, 6], [0, 8]]]).unwrap();
assert_eq!(expected, arr.triu(None).unwrap());
§Errors

may returns ArrayError

Source

fn vander( &self, n: Option<usize>, increasing: Option<bool>, ) -> Result<Self, ArrayError>
where Self: Sized,

Generate a Vandermonde matrix

§Arguments
  • n - number of columns in the output. optional, by default square array is returned
  • increasing - order of the powers of the columns. optional, defaults to false if true, the powers increase from left to right, if false, they are reversed.
§Examples
use arr_rs::prelude::*;

let arr = array!(i32, [1, 2, 3, 4]).unwrap();
let expected = array!(i32, [[1, 1, 1, 1], [8, 4, 2, 1], [27, 9, 3, 1], [64, 16, 4, 1]]).unwrap();
assert_eq!(expected, arr.vander(None, Some(false)).unwrap());

let arr = array!(i32, [1, 2, 3, 4]).unwrap();
let expected = array!(i32, [[1, 1, 1, 1], [1, 2, 4, 8], [1, 3, 9, 27], [1, 4, 16, 64]]).unwrap();
assert_eq!(expected, arr.vander(None, Some(true)).unwrap());
§Errors

may returns ArrayError

Implementors§