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

Matlab/Pytorch/Numpy syntax for multidimensional array indexing and slicing #21628

Open
2 tasks
paucarre opened this issue Jun 1, 2024 · 0 comments
Open
2 tasks
Labels
Feature Request This issue is made to request a feature. Type: Specification Everything related to the formal specification of how V should behave Type: Syntax The issue/PR is related to the V syntax

Comments

@paucarre
Copy link

paucarre commented Jun 1, 2024

Describe the feature

A way to emulate multidimensonal array indexing and slicing like matlab, pytorch or numpy. This will allow egineering, statistics, machine learning, deep learning and other numerical work to be performed in a compact and readable way, making it more accessible.

Currently VLT is supposed to be the ongoing work on this regard already planned.
I find the way tensors are sliced to be really cumbersome, hard to understand, with unnecessary boilerplate code. I believe this problem stems from V language syntax limitations.

Use Case

For instance, taken form vlt
The code

c.apply(fn (x int, i []int) int {
	return x * 2
})

Could be converted into:

c = c * 2

Another example, the code from vlt:
The code:

slice2 := t.slice_hilo([3], [])!

Could be converted into:

t2 = t[3:, :]

Another example, the code from vlt:
The code:

mut slice1 := t.slice_hilo([1, 3], [3, 5])!

Could be converted into:

t1 = t[1:3, 3:5]

Proposed Solution

Add additional syntax to V language to allow indexing and silicng of multidimensional arrays that have been widely successful in multiple languages and frameworks for decades ( matlab, numpy ) and are the cornerstone for deep learning and other highly desirable numerical computing workflows.

V already has support for syntax sugar for unidimensional arrays. A multidimensional tensor could have similar syntax sugar. For instance:

t1 = t[1..3, 3..5]
t2 = t[3.. , ..]

The syntax shall allow expressions like:

t2  = (t[3.. , ..] * 0.5) + (t[..3 , ..] ** 2)
loss  = t2.sum(axis=1).mean(axis=0)

Potentially, the langauge should allow the implementaiton of these operations to be performed by different devices ( CPU, GPU).
This also means it is likely out of bounds to be disabled or at least disabled by default if that allows better preformance.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

0.4.6

Environment details (OS name and version, etc.)

Linux Ubuntu 22.04.1

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@paucarre paucarre added the Feature Request This issue is made to request a feature. label Jun 1, 2024
@spytheman spytheman added Type: Specification Everything related to the formal specification of how V should behave Type: Syntax The issue/PR is related to the V syntax labels Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is made to request a feature. Type: Specification Everything related to the formal specification of how V should behave Type: Syntax The issue/PR is related to the V syntax
Projects
None yet
Development

No branches or pull requests

2 participants