Skip to content

Add arange method - #167

Merged
benikm91 merged 3 commits into
dimwit-dev:mainfrom
benikm91:arange
Sep 22, 2026
Merged

benikm91 merged 3 commits into
dimwit-dev:mainfrom
benikm91:arange

Conversation

@benikm91

Copy link
Copy Markdown
Collaborator

No description provided.

@marcelluethi marcelluethi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@benikm91

benikm91 commented Sep 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

I updated the API to work with the Scala Range objects instead of having multiple arange methods. Defaults to Int32, but has a vtype parameter to overwrite:

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], Float32

Edit:
Regarding linspace: Yes, we will (probably) add it, but let's move this to another PR maybe with logspace etc.

@benikm91

Copy link
Copy Markdown
Collaborator Author

Maybe we should rename arange to fromRange

@marcelluethi

Copy link
Copy Markdown
Contributor

@benikm91 I think fromRange is great. A limitation is, however, that it only allows for integer values. Having arange would be useful for generating floating point values. So I think we should support both. Maybe we should even have a tabulate method as a third possibility.

@benikm91

Copy link
Copy Markdown
Collaborator Author

I didn't think about floating-point numbers. But, doing some research, it maybe even better not to support this, as arange seems to be known to be bad when used with floating-point numbers (due to precision); see

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 arange to fromRange and add a linspace method.

@marcelluethi

Copy link
Copy Markdown
Contributor

This sounds like a good solution to me.

@benikm91
benikm91 merged commit 09dfe4c into dimwit-dev:main Sep 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants