pub fn diag(matrix: Array) -> Result<Array, Box<EvalAltResult>>
Expand description
This function can be used in two distinct ways.
- If the argument is an 2-D array,
diag
returns an array containing the diagonal of the array. - If the argument is a 1-D array,
diag
returns a matrix containing the argument along the diagonal and zeros elsewhere.
let matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]];
let d = diag(matrix);
assert_eq(d, [1, 5, 9]);
let diagonal = [1.0, 2.0, 3.0];
let matrix = diag(diagonal);
assert_eq(matrix, [[1.0, 0.0, 0.0],
[0.0, 2.0, 0.0],
[0.0, 0.0, 3.0]]);