stdlib-js / stdlib

✨ Standard library for JavaScript and Node.js. ✨
https://stdlib.io
Apache License 2.0
4.33k stars 441 forks source link

[RFC]: achieve ndarray API parity with built-in JavaScript arrays #2656

Open kgryte opened 1 month ago

kgryte commented 1 month ago

Description

This RFC proposes achieving ndarray API parity with built-in JavaScript arrays. Built-in JavaScript Array and TypedArray objects have a number of methods for searching, manipulating, sorting, and transforming array data.

The goal of this RFC is to add functional APIs providing equivalent functionality for ndarrays. By providing these APIs, stdlib can offer a powerful toolset using a similar vocabulary and interface design as existing art for working with ndarrays. This should help reduce the barrier to ndarray adoption and encourage their more widespread use.

Note, however, that ndarrays have considerable additional complexity due to their multi-dimensional nature, and, in particular, element-wise iteration requires specialized kernels for handling non-contiguous underlying data.

There does exist precedent in stdlib for such kernels (e.g., ndarray/base/assign, ndarray/base/nullary, and ndarray/base/unary). Those packages also provide C APIs which may or may not be relevant to the functional APIs proposed in this RFC.

What follows is an initial list of Array.prototype.* methods and notes regarding whether an equivalent already exists or what constraints we need to consider when designing ndarray equivalent packages.

Top-level: ndarray/*

Base: ndarray/base/*

Related Issues

None.

Questions

No.

Other

No.

Checklist

kgryte commented 1 month ago

cc @headlessNode

SarthakPaandey commented 1 month ago

I want to work on this issue @kgryte and could you elaborate on the specific goals of aligning ndarray APIs with JavaScript arrays? How will this benefit users and developers?

kgryte commented 1 month ago

How will this benefit users and developers?

This is already addressed in the OP. Second paragraph.

SarthakPaandey commented 1 month ago

Yup! Starting work on this!

kgryte commented 1 month ago

@SarthakPaandey Before you begin, it would be best to communicate what you're planning to work on. Some of the above routines are easier than others and should be addressed first. Reductions require R&D and should only be worked on once we've determined the best approach.

SarthakPaandey commented 1 month ago

"Understood, @kgryte. I'll prioritize the routines that are comparatively easier and communicate my plan before starting. I'll defer work on reductions until we've established the best approach. Thanks for the guidance!

headlessNode commented 1 month ago

@kgryte I have started to work on the map method. I plan to submit PR for it when I've implemented the kernels for it up to 3d. This way, it would be possible to get feedback from you earlier and it would be easier for me to work on the feedback. Does that seem reasonable?

kgryte commented 1 month ago

@headlessNode That makes sense. I am currently working on for-each, which is related.

kgryte commented 1 month ago

@headlessNode I suggest working a "base" implementation of map first. For @stdlib/ndarray/base/map, we can assume that an output array is provided, similar to ndarray/base/unary. In fact, unary is a good reference for map. The main difference being the support of a thisArg and no C implementation.

headlessNode commented 1 month ago

@kgryte The thisArg in our case would be similar to how it is handled in the Array.prototype.map right? e.g.


//Array.prototype.map
let numbers = [1, 2, 3];
let obj = { multiplier: 2 };

numbers.map(function(num) {
  return num * this.multiplier;
}, obj);```
kgryte commented 1 month ago

@headlessNode Yes. As an example, see https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/any-by.

kgryte commented 1 month ago

@headlessNode Pushed up a POC implementation of for-each: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/for-each

headlessNode commented 1 month ago

Thanks!

headlessNode commented 4 weeks ago

Heads up. I have started to work on ndarray/base/fill

kgryte commented 4 weeks ago

Sounds good, @headlessNode! Thanks for the heads up!