Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 606 fix #607

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/arraymancer/tensor/init_cpu.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/tensor/test_init.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading