pub fn triu<T, C>(
_ctx: &mut C,
tensor: &Tensor<T>,
diagonal: isize,
) -> Result<Tensor<T>>where
T: Scalar,
C: TensorMetadataContextFor,Expand description
Extract the upper triangular part of a matrix (or batch of matrices).
Elements below the diagonal-th diagonal are set to zero. The context
parameter enables future GPU-backend dispatch.
The diagonal parameter controls which diagonal is the boundary:
0is the main diagonal- positive values move above the main diagonal
- negative values move below the main diagonal
§Errors
Returns an error if the tensor has fewer than 2 dimensions or if backend dispatch fails.
§Examples
ⓘ
use tenferro_device::LogicalMemorySpace;
use tenferro_prims::{tensor_ops, CpuContext};
use tenferro_tensor::{MemoryOrder, Tensor};
let mut ctx = CpuContext::new(1);
let a = Tensor::<f64>::ones(
&[3, 3],
LogicalMemorySpace::MainMemory,
MemoryOrder::ColumnMajor,
).unwrap();
let upper = tensor_ops::triu(&mut ctx, &a, 0).unwrap();
assert_eq!(upper.dims(), &[3, 3]);