v icon indicating copy to clipboard operation
v copied to clipboard

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

Open paucarre opened this issue 8 months ago • 0 comments

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 avatar Jun 01 '24 16:06 paucarre