Skip to content

Commit

Permalink
Issue 606 fix (#607)
Browse files Browse the repository at this point in the history
* Fix for Issue #606

This fixes the error:
> arraymancer\tensor\init_cpu.nim(199, 23) Error: expression 'T(1)' has no type (or is ambiguous)

Which is triggered by the following code:

```nim
let numItems = 3
let start = 2
let v = @[10, 11, 12, 13, 14, 15].toTensor
let w = v[start +. arange(numItems)]
echo w
```

* Add test for issue #606 (Using arange within a [] expression fails)

---------

Co-authored-by: Angel Ezquerra <[email protected]>
  • Loading branch information
AngelEzquerra and AngelEzquerraAtKeysight authored Oct 20, 2023
1 parent 6fd4df3 commit 2fa5040
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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

0 comments on commit 2fa5040

Please sign in to comment.