1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
use crate::arrays::Array;
use crate::traits::{
errors::ArrayError,
types::{
numeric::Numeric,
tuple_numeric::Tuple2,
}
};
/// ArrayTrait - Array Broadcast functions
pub trait ArrayBroadcast<N: Numeric> where Self: Sized + Clone {
/// Broadcast an array to a new shape
///
/// # Arguments
///
/// * `other` - other array for broadcasting
///
/// # Examples
///
/// ```
/// use arr_rs::prelude::*;
///
/// let expected: Array<Tuple2<i32>> = Array::new(vec![
/// (1, 4), (1, 5), (1, 6),
/// (2, 4), (2, 5), (2, 6),
/// (3, 4), (3, 5), (3, 6)
/// ].into_iter().map(Tuple2::from_tuple).collect(), vec![3, 3]).unwrap();
///
/// let arr_1: Array<i32> = array!([[1], [2], [3]]).unwrap();
/// let arr_2: Array<i32> = array!([[4, 5, 6]]).unwrap();
///
/// let broadcast: Array<Tuple2<i32>> = arr_1.broadcast(&arr_2).unwrap();
/// assert_eq!(expected, broadcast);
/// ```
fn broadcast(&self, other: &Array<N>) -> Result<Array<Tuple2<N>>, ArrayError>;
/// Broadcast an array to a new shape
///
/// # Arguments
///
/// * `other` - other array for broadcasting
///
/// # Examples
///
/// ```
/// use arr_rs::prelude::*;
///
/// let expected: Array<i32> = Array::new(vec![1, 1, 1, 2, 2, 2, 3, 3, 3], vec![3, 3]).unwrap();
/// let arr_1: Array<i32> = array!([[1], [2], [3]]).unwrap();
///
/// let broadcast: Array<i32> = arr_1.broadcast_to(vec![3, 3]).unwrap();
/// assert_eq!(expected, broadcast);
/// ```
fn broadcast_to(&self, shape: Vec<usize>) -> Result<Array<N>, ArrayError>;
/// Broadcast a list of arrays to a common shape
///
/// # Arguments
///
/// * `arrays` - list of arrays for broadcasting
///
/// # Examples
///
/// ```
/// use arr_rs::prelude::*;
///
/// let expected: Vec<Array<i32>> = vec![
/// Array::new(vec![1, 1, 1, 2, 2, 2, 3, 3, 3], vec![3, 3]).unwrap(),
/// Array::new(vec![4, 5, 6, 4, 5, 6, 4, 5, 6], vec![3, 3]).unwrap(),
/// ];
/// let arr_1: Array<i32> = array!([[1], [2], [3]]).unwrap();
/// let arr_2: Array<i32> = array!([4, 5, 6]).unwrap();
///
/// let broadcast: Vec<Array<i32>> = Array::broadcast_arrays(vec![arr_1 ,arr_2]).unwrap();
/// assert_eq!(expected, broadcast);
/// ```
fn broadcast_arrays(arrays: Vec<Array<N>>) -> Result<Vec<Array<N>>, ArrayError>;
}