Add arange method - #167
Conversation
marcelluethi
left a comment
There was a problem hiding this comment.
Thanks for adding it. While we could always use tabulate with fromArray to simulate it, the need arises frequently enough that it is worth adding it. I am wondering if we should then also support linspace?
| /** Creates a vector of evenly spaced values in the half-open interval `[start, stop)`, | ||
| * like `jnp.arange`. The extent of the axis is `ceil((stop - start) / step)`. | ||
| */ | ||
| def arange(stop: Int): Tensor1[L, Int32] = arange(0, stop) |
There was a problem hiding this comment.
I always found arange(7)to be unnecessary short and rather unreadable. arange(0, 7) makes it much more clear what is happening and is only 2 characters more to write. I know that this notation is deeply entrenched in Python, but do we really need to support it here? Maybe we should also mandate that the user provides the step explicitly arange(start = 0, stop = 7,step = 1)
| def arange(start: Int, stop: Int, step: Int): Tensor1[L, Int32] = Tensor1(axis, VType[Int32]).arange(start, stop, step) | ||
| def arange(stop: Long): Tensor1[L, Int64] = arange(0L, stop) | ||
| def arange(start: Long, stop: Long): Tensor1[L, Int64] = arange(start, stop, 1L) | ||
| def arange(start: Long, stop: Long, step: Long): Tensor1[L, Int64] = Tensor1(axis, VType[Int64]).arange(start, stop, step) |
There was a problem hiding this comment.
I wonder if it would not be better to have only one method arange(start, stop, step, vtype) instead of automatically derive the vtype from the arguments? What happens when I write arange(0, 5L, 1s)? Is the resulting type Int16, Int32 or Int64? Writing arange(0, 5, 1, vtype=Int32) would make this clear.
|
I updated the API to work with the Scala val t = Tensor1(Axis[A]).arange(0 until 4) // [0, 1, 2, 3], Int32
val t = Tensor1(Axis[A]).arange(0 to 4) // [0, 1, 2, 3, 4], Int32
val t = Tensor1(Axis[A]).arange(0 to 4 by 2) // [0, 2, 4], Int32
val t = Tensor1(Axis[A]).arange(4 to 0 by -1) // [4, 3, 2, 1, 0], Int32
val t = Tensor1(Axis[A]).arange(4 to 0 by -1, VType[Float32]) // [4, 3, 2, 1, 0], Float32Edit: |
|
Maybe we should rename |
|
@benikm91 I think |
|
I didn't think about floating-point numbers. But, doing some research, it maybe even better not to support this, as When using a non-integer step, such as 0.1, it is often better to use [numpy.linspace]in https://numpy.org/doc/stable/reference/generated/numpy.arange Also Scala had a Double and Float Range and removed it due to this reason: scala/bug#10781 So I suggest rename |
|
This sounds like a good solution to me. |
No description provided.