feat: add ndarray/base/where#10659
Conversation
Coverage Report
The above coverage report was generated for the changes in this PR. |
44fccf8 to
8fa22ff
Compare
8fa22ff to
2df0faf
Compare
7eaac76 to
5b71c61
Compare
cc80ed0 to
c2816cb
Compare
c2816cb to
b672f66
Compare
5e38042 to
f470119
Compare
| // Verify that the input and output arrays have the same number of dimensions... | ||
| shc = c.shape; | ||
| shx = x.shape; | ||
| shy = y.shape; | ||
| sho = o.shape; | ||
| ndims = shc.length; | ||
| if ( ndims !== shx.length || ndims !== shy.length || ndims !== sho.length ) { | ||
| throw new Error( format( 'invalid arguments. Arrays must have the same number of dimensions (i.e., same rank). ndims(c) == %d. ndims(x) == %d. ndims(y) == %d. ndims(o) == %d.', shc.length, shx.length, shy.length, sho.length ) ); | ||
| } |
There was a problem hiding this comment.
@LoayAhmed304 If someone passes arrays with mismatched shapes but valid dtypes, the function will do unnecessary work (reinterpreting arrays) before eventually throwing.
There was a problem hiding this comment.
Done. Moved shapes validation to the top.
I believe I took it from assign here, but we're dealing with 4 arrays here not 2, so maybe the unnecessary work will affect performance here.
There was a problem hiding this comment.
Note that this will cause re-assignment of shapes again after the reinterpretation, since reinterpreting complex ndarrays mutates the shape.
8daca0d to
0c3a27c
Compare
Co-authored-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Signed-off-by: Loay Ahmed <loayahmed304@gmail.com>
|
Note that tests will have updated cases to cover the varying |
| var dc0; | ||
| var dx0; | ||
| var dy0; | ||
| var do0; |
There was a problem hiding this comment.
| var dc0; | |
| var dx0; | |
| var dy0; | |
| var do0; | |
| var dc; | |
| var dx; | |
| var dy; | |
| var do; |
Applies to below and 1d.js as well.
There was a problem hiding this comment.
do is a reserved keyword. I'll use dout instead, but will keep it do# for the rest of the kernels.
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **arrays**: array-like object containing three input ndarrays and one output ndarray. |
There was a problem hiding this comment.
Not sure if this is okay since in the main description 'a condition' and 'two input ndarrays' is used.
There was a problem hiding this comment.
If you decide to change, it would apply to repl and index.d.ts file as well.
| // broadcast extra dimension along last axis for the condition array for complex compatibility. | ||
| stride = shape2strides( shape, order ); | ||
| cshape = shape.slice(); | ||
| cshape.push( 1 ); | ||
| cstrides = stride.slice(); | ||
| cstrides.push( 0 ); |
There was a problem hiding this comment.
Why is this required here? condition becomes 3d while all other are 2d. What happens if the condition is 2d like others? Is the output affected?
There was a problem hiding this comment.
This is what main.js does when dealing with complex input ndarrays, and actually this is something I wanted to discuss.
// main.js
if ( isComplexArray( x.data ) && isComplexArray( y.data ) && isComplexArray( o.data ) ) {
x = complex2real( x );
y = complex2real( y );
o = complex2real( o );
c.shape.push( 2 ); // real and imaginary components
c.strides.push( 0 ); // broadcast
// ....Currently what I'm doing is something similar to what complex2real does when interpreting the input complex ndarray, which manipulates the shape and stride after reinterpretation. So this basically does the same to the condition array, so each element in the condition ndarray compares both the real and imaginary parts. Hence for complex, the condition ndarray buffer can be half the size of the complex buffer. Like this:
// test.js
var cbuf = new BooleanArray([
true,
false,
false,
true
]);
var xbuf = new Complex128Array([
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0
]);
// ....There was a problem hiding this comment.
However I believe this will break when having 1 complex input ndarray and 1 real input ndarray, since I was assuming both will have the same dtype.
|
@LoayAhmed304 Would good to check all the files against the above comments, especially the dimensional kernels. |
f06d2db to
fb2a747
Compare
Resolves #{{TODO: add issue number}}.
Description
This pull request:
wherefunction for ndarrays, which applies a condition to elements in two input ndarrays and assign results to elements in an output ndarray.Related Issues
This pull request has the following related issues:
Questions
No.
Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
{{TODO: add disclosure if applicable}}
@stdlib-js/reviewers