nicolaspanel / numjs

Like NumPy, in JavaScript
MIT License
2.4k stars 184 forks source link

can one access the TypedArray buffer after `.pick` or `.slice`? #145

Open fangq opened 8 months ago

fangq commented 8 months ago

this is not a bug, but a question

I am wondering if it is possible to access the underlying TypedArray buffer after .pick or .slice? I noticed that calling .selection.data always returns the full array, not the selected subset.

one need arises from sorting each row of a 2-D matrix. I can achieve this by first calling .tolist() and then call Array.sort

var c = nj.array([[2, 3, 4, 10],[5,6,7,9],[8, 3, 1, 12]]).T;
for(let i=0; i<6; i++) {
  console.log(c.pick(i).tolist().sort((a,b) => a-b));
}

although I would like to do the sorting directly on the TypedArray buffer. is this possible?

also, what is the best way to copy one row/column from one matrix to another? I tried matrix1.pick(i).assign(matrix2.pick(j)), however, it does not seems to actually copy the value.