Skip to content

Add arange method - #167

Open
benikm91 wants to merge 2 commits into
dimwit-dev:mainfrom
benikm91:arange
Open

benikm91 wants to merge 2 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.

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