diff --git a/src/arraymancer/tensor/init_cpu.nim b/src/arraymancer/tensor/init_cpu.nim index 4e7cd7f8..7533aa4a 100644 --- a/src/arraymancer/tensor/init_cpu.nim +++ b/src/arraymancer/tensor/init_cpu.nim @@ -196,11 +196,20 @@ proc arange*[T: SomeNumber](start, stop, step: T): Tensor[T] {.noinit.} = template arange*[T: SomeNumber](stop: T): Tensor[T] = # Error messages of templates are very poor - arange(T(0), stop, T(1)) + # While it seems that we could do `arange(T(0), stop, T(1))` + # that does not work well (https://github.com/mratsim/Arraymancer/issues/606) + # Instead we must do this: + type TT = typeof(stop) + const zero = TT(0) + const one = TT(1) + arange(zero, stop, one) template arange*[T: SomeNumber](start, stop: T): Tensor[T] = # Error messages of templates are very poor - arange(start, stop, T(1)) + type TT = typeof(stop) + const one = TT(1) + arange(start, stop, one) + template randomTensorCpu[T](t: Tensor[T], shape: varargs[int], max_or_range: typed): untyped = var size: int diff --git a/tests/tensor/test_init.nim b/tests/tensor/test_init.nim index ec61c329..cfc50b04 100644 --- a/tests/tensor/test_init.nim +++ b/tests/tensor/test_init.nim @@ -157,6 +157,13 @@ proc main() = block: let t = arange(1.0,2.5,0.5) check: t == [1.0,1.5,2.0].toTensor() + # Test for Issue #606 + block: + let numItems = 3 + let start = 2 + let v = @[10, 11, 12, 13, 14, 15].toTensor + let w = v[start +. arange(numItems)] + check: w == [12, 13, 14].toTensor() test "Random tensor": # Check that randomTensor doesn't silently convert float32 to float64