pub fn fill_diagonal<T: Clone>(
array: &mut Array<T>,
val: T,
wrap: bool,
) -> Result<()>Expand description
Fill the main diagonal of the given array of any dimensionality.
For an array a with a.ndim >= 2, the diagonal is the list of
locations with indices a[i, ..., i] all identical. This function
modifies the input array in-place, it does not return a value.
§Arguments
array- Array whose diagonal is to be filled, it gets modified in-placeval- Value to be written on the diagonalwrap- For tall matrices in NumPy version 1.13, the diagonal “wraps” after N columns. This behavior is deprecated, but you can specify wrap=true to continue using it.
§Examples
use numrs2::prelude::*;
use numrs2::array_ops::diagonal::fill_diagonal;
// Fill diagonal of a 3x3 matrix
let mut a = Array::zeros(&[3, 3]);
fill_diagonal(&mut a, 5, false).expect("operation should succeed");
assert_eq!(a.to_vec(), vec![5, 0, 0, 0, 5, 0, 0, 0, 5]);
// Fill diagonal of a 4x3 matrix
let mut b = Array::zeros(&[4, 3]);
fill_diagonal(&mut b, 7, false).expect("operation should succeed");
assert_eq!(b.to_vec(), vec![7, 0, 0, 0, 7, 0, 0, 0, 7, 0, 0, 0]);
// Fill diagonal of a 3D array
let mut c = Array::zeros(&[3, 3, 3]);
fill_diagonal(&mut c, 1, false).expect("operation should succeed");
// Only elements where all indices are equal get filled
let expected = vec![
1, 0, 0, 0, 0, 0, 0, 0, 0, // c[0,:,:]
0, 0, 0, 0, 1, 0, 0, 0, 0, // c[1,:,:]
0, 0, 0, 0, 0, 0, 0, 0, 1 // c[2,:,:]
];
assert_eq!(c.to_vec(), expected);