Support negative steps in arange. (#1218)

This commit is contained in:
Laurent Mazare
2023-10-30 08:40:54 +01:00
committed by GitHub
parent 174b208052
commit 5fc66bd4ba
2 changed files with 33 additions and 3 deletions

View File

@ -385,11 +385,21 @@ impl Tensor {
step: D,
device: &Device,
) -> Result<Self> {
if D::is_zero(&step) {
crate::bail!("step cannot be zero")
}
let mut data = vec![];
let mut current = start;
while current < end {
data.push(current);
current += step;
if step >= D::zero() {
while current < end {
data.push(current);
current += step;
}
} else {
while current > end {
data.push(current);
current += step;
}
}
let len = data.len();
Self::from_vec_impl(data, len, device, false)