Skip to content

Commit 8daca0d

Browse files
committed
chore: apply requested changes
1 parent 7230913 commit 8daca0d

9 files changed

Lines changed: 121 additions & 138 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/where/README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,24 @@ var Float64Array = require( '@stdlib/array/float64' );
4747
var Uint8Array = require( '@stdlib/array/uint8' );
4848

4949
// Create data buffers:
50-
var cbuf = new Uint8Array( [ 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1 ] );
51-
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
52-
var ybuf = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0, -9.0, -10.0, -11.0, -12.0 ] );
50+
var cbuf = new Uint8Array( [ 1, 0, 0, 1, 0, 1 ] );
51+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
52+
var ybuf = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
5353
var obuf = new Float64Array( 6 );
5454

5555
// Define the shape of the input and output arrays:
5656
var shape = [ 3, 1, 2 ];
5757

5858
// Define the array strides:
59-
var sc = [ 4, 4, 1 ];
60-
var sx = [ 4, 4, 1 ];
61-
var sy = [ 4, 4, 1 ];
59+
var sc = [ 2, 2, 1 ];
60+
var sx = [ 2, 2, 1 ];
61+
var sy = [ 2, 2, 1 ];
6262
var so = [ 2, 2, 1 ];
6363

6464
// Define the index offsets:
65-
var oc = 1;
66-
var ox = 1;
67-
var oy = 1;
65+
var oc = 0;
66+
var ox = 0;
67+
var oy = 0;
6868
var oo = 0;
6969

7070
// Create the input and output ndarrays:
@@ -105,7 +105,7 @@ var out = {
105105
where( [ condition, x, y, out ] );
106106

107107
console.log( out.data );
108-
// => <Float64Array>[ -2.0, -3.0, 6.0, 7.0, -10.0, -11.0 ]
108+
// => <Float64Array>[ 1.0, -2.0, -3.0, 4.0, -5.0, 6.0 ]
109109
```
110110

111111
The function accepts the following arguments:
@@ -131,7 +131,6 @@ Each provided ndarray should be an object with the following properties:
131131

132132
- `condition` ndarray must be a `boolean` or `uint8` ndarray.
133133
- `condition`, `x`, `y`, and `out` ndarrays must have the same shape.
134-
- `x` and `y` must have the same data type.
135134
- The function **mutates** the input ndarrays shapes and strides if necessary.
136135
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before conditionally assigning elements in order to achieve better performance.
137136

lib/node_modules/@stdlib/ndarray/base/where/docs/repl.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
{{alias}}( arrays )
3-
Applies a condition to elements in two input ndarrays
4-
and assigns results to elements in an output ndarray.
3+
Applies a condition to elements in two input ndarrays and assigns results
4+
to elements in an output ndarray.
55

66
Each provided "ndarray" should be an object with the following properties:
77

lib/node_modules/@stdlib/ndarray/base/where/docs/types/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import { ndarray } from '@stdlib/types/ndarray';
3030
* @throws arrays must have the same number of dimensions
3131
* @throws arrays must have the same shape
3232
* @throws {Error} condition array must be a boolean or uint8 ndarray
33-
* @throws {Error} x and y ndarrays must have the same dtype
3433
*
3534
* @example
3635
* var Float64Array = require( '@stdlib/array/float64' );

lib/node_modules/@stdlib/ndarray/base/where/lib/main.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19+
/* eslint-disable max-lines */
20+
1921
'use strict';
2022

2123
// MODULES //
@@ -25,6 +27,7 @@ var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floatin
2527
var isRealDataType = require( '@stdlib/ndarray/base/assert/is-real-data-type' );
2628
var isComplexArray = require( '@stdlib/array/base/assert/is-complex-typed-array' );
2729
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
30+
var isUint8Array = require( '@stdlib/assert/is-uint8array' );
2831
var iterationOrder = require( '@stdlib/ndarray/base/iteration-order' );
2932
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
3033
var anyIsEntryIn = require( '@stdlib/array/base/any-is-entry-in' );
@@ -148,10 +151,11 @@ var COMPLEX_TO_REAL = { // WARNING: this table needs to be manually updated if w
148151
* @param {ndarrayLike} x - first ndarray
149152
* @param {ndarrayLike} y - second ndarray
150153
* @param {ndarrayLike} z - third ndarray
154+
* @param {ndarrayLike} w - fourth ndarray
151155
* @returns {boolean} boolean indicating whether an ndarray data buffer implements the accessor protocol
152156
*/
153-
function hasAccessors( x, y, z ) {
154-
return anyIsEntryIn( [ x, y, z ], 'accessorProtocol', true );
157+
function hasAccessors( x, y, z, w ) {
158+
return anyIsEntryIn( [ x, y, z, w ], 'accessorProtocol', true );
155159
}
156160

157161
/**
@@ -224,7 +228,6 @@ function complex2real( x ) {
224228
* @throws {Error} arrays must have the same number of dimensions
225229
* @throws {Error} arrays must have the same shape
226230
* @throws {Error} condition array must be a boolean or uint8 ndarray
227-
* @throws {Error} x and y ndarrays must have the same dtype
228231
* @returns {void}
229232
*
230233
* @example
@@ -292,7 +295,7 @@ function complex2real( x ) {
292295
* console.log( out.data );
293296
* // => <Float64Array>[ -2.0, -3.0, 6.0, 7.0, -10.0, -11.0 ]
294297
*/
295-
function where( arrays ) {
298+
function where( arrays ) { // eslint-disable-line max-statements
296299
var ndims;
297300
var cmmv;
298301
var xmmv;
@@ -330,12 +333,18 @@ function where( arrays ) {
330333
y = ndarray2object( arrays[ 2 ] );
331334
o = ndarray2object( arrays[ 3 ] );
332335

333-
if ( !isBooleanArray( c.data ) && c.dtype !== 'bool' && c.dtype !== 'uint8') {
334-
throw new Error( format( 'invalid arguments. Condition array must be a boolean or uint8 ndarray. c.dtype=%s', c.dtype ) );
336+
// Verify that the input and output arrays have the same number of dimensions...
337+
shc = c.shape;
338+
shx = x.shape;
339+
shy = y.shape;
340+
sho = o.shape;
341+
ndims = shc.length;
342+
if ( ndims !== shx.length || ndims !== shy.length || ndims !== sho.length ) { // eslint-disable-line max-len
343+
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 ) );
335344
}
336345

337-
if ( x.dtype !== y.dtype ) {
338-
throw new Error( format( 'invalid arguments. Input arrays must have the same data type. x.dtype=%s, y.dtype=%s', x.dtype, y.dtype ) );
346+
if ( !isBooleanArray( c.data ) && !isUint8Array( c.data ) ) {
347+
throw new Error( format( 'invalid arguments. Condition array must be a boolean or uint8 ndarray. c.dtype=%s', c.dtype ) );
339348
}
340349

341350
// Always reinterpret condition array to uint8
@@ -344,11 +353,11 @@ function where( arrays ) {
344353
}
345354

346355
// Check for known array types which can be reinterpreted for better iteration performance...
347-
if ( isBooleanArray( x.data ) && isBooleanArray( y.data ) && isBooleanArray( o.data ) ) {
356+
if ( isBooleanArray( x.data ) && isBooleanArray( y.data ) && isBooleanArray( o.data ) ) { // eslint-disable-line max-len
348357
x = boolean2uint8( x );
349358
y = boolean2uint8( y );
350359
o = boolean2uint8( o );
351-
} else if ( isComplexArray( x.data ) && isComplexArray( y.data ) && isComplexArray( o.data ) ) {
360+
} else if ( isComplexArray( x.data ) && isComplexArray( y.data ) && isComplexArray( o.data ) ) { // eslint-disable-line max-len
352361
x = complex2real( x );
353362
y = complex2real( y );
354363
o = complex2real( o );
@@ -357,21 +366,23 @@ function where( arrays ) {
357366
c.strides.push( 0 ); // broadcast
358367
}
359368
// Determine whether we are casting a real data type to a complex data type and we need to use a specialized accessor (note: we don't support the other way, complex-to-real, as this is not an allowed (mostly) safe cast; note: we cannot create a specialized view for assigning only real components, as the imaginary component for each element in `y` also needs to be set to zero and while we could perform two passes, it's not clear it's worth the effort)...
360-
else if ( (isRealDataType( x.dtype ) && isRealDataType( y.dtype ) ) && isComplexDataType( o.dtype ) ) {
361-
x.accessorProtocol = true;
362-
y.accessorProtocol = true;
363-
x.accessors[ 0 ] = castReturn( x.accessors[ 0 ], 2, complexCtors( String( o.dtype ) ) ); // eslint-disable-line max-len
364-
y.accessors[ 0 ] = castReturn( y.accessors[ 0 ], 2, complexCtors( String( o.dtype ) ) ); // eslint-disable-line max-len
369+
else if ( isComplexDataType( o.dtype ) ) {
370+
if ( isRealDataType( x.dtype )) {
371+
x.accessorProtocol = true;
372+
x.accessors[ 0 ] = castReturn( x.accessors[ 0 ], 2, complexCtors( String( o.dtype ) ) ); // eslint-disable-line max-len
373+
}
374+
if ( isRealDataType( y.dtype ) ) {
375+
y.accessorProtocol = true;
376+
y.accessors[ 0 ] = castReturn( y.accessors[ 0 ], 2, complexCtors( String( o.dtype ) ) ); // eslint-disable-line max-len
377+
}
365378
}
366-
// Verify that the input and output arrays have the same number of dimensions...
379+
367380
shc = c.shape;
368381
shx = x.shape;
369382
shy = y.shape;
370383
sho = o.shape;
371384
ndims = shc.length;
372-
if ( ndims !== shx.length || ndims !== shy.length || ndims !== sho.length ) {
373-
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 ) );
374-
}
385+
375386
// Determine whether we can avoid iteration altogether...
376387
if ( ndims === 0 ) {
377388
if ( hasAccessors( c, x, y, o ) ) {
@@ -439,15 +450,15 @@ function where( arrays ) {
439450

440451
// Determine whether we can avoid blocked iteration...
441452
ord = strides2order( sc );
442-
if ( ioc !== 0 && iox !== 0 && ioy !== 0 && ioo !== 0 && ord === strides2order( sx ) && ord === strides2order( sy ) && ord === strides2order( so ) ) {
453+
if ( ioc !== 0 && iox !== 0 && ioy !== 0 && ioo !== 0 && ord === strides2order( sx ) && ord === strides2order( sy ) && ord === strides2order( so ) ) { // eslint-disable-line max-len
443454
// Determine the minimum and maximum linear indices which are accessible by the array views:
444455
cmmv = minmaxViewBufferIndex( shc, sc, c.offset );
445456
xmmv = minmaxViewBufferIndex( shx, sx, x.offset );
446457
ymmv = minmaxViewBufferIndex( shy, sy, y.offset );
447458
ommv = minmaxViewBufferIndex( sho, so, o.offset );
448459

449460
// Determine whether we can ignore shape (and strides) and treat the ndarrays as linear one-dimensional strided arrays...
450-
if ( len === ( cmmv[1]-cmmv[0]+1 ) && len === ( xmmv[1]-xmmv[0]+1 ) && len === ( ymmv[1]-ymmv[0]+1 ) && len === ( ommv[1]-ommv[0]+1 ) ) {
461+
if ( len === ( cmmv[1]-cmmv[0]+1 ) && len === ( xmmv[1]-xmmv[0]+1 ) && len === ( ymmv[1]-ymmv[0]+1 ) && len === ( ommv[1]-ommv[0]+1 ) ) { // eslint-disable-line max-len
451462
// Note: the above is equivalent to @stdlib/ndarray/base/assert/is-contiguous, but in-lined so we can retain computed values...
452463
if ( ioc === 1 ) {
453464
oc = cmmv[ 0 ];

lib/node_modules/@stdlib/ndarray/base/where/test/test.0d.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' );
2525
var isSameAccessorArray = require( '@stdlib/assert/is-same-accessor-array' );
2626
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2727
var Float64Array = require( '@stdlib/array/float64' );
28+
var Uint8Array = require( '@stdlib/array/uint8' );
2829
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
2930
var ndarray = require( '@stdlib/ndarray/ctor' );
3031
var where = require( './../lib' );
@@ -38,7 +39,7 @@ tape( 'main export is a function', function test( t ) {
3839
t.end();
3940
});
4041

41-
tape( 'the function assigns elements from n 0-dimensional input ndarray to an output ndarray', function test( t ) {
42+
tape( 'the function assigns elements from a 0-dimensional input ndarray to an output ndarray', function test( t ) {
4243
var condition;
4344
var expected;
4445
var out;
@@ -66,13 +67,13 @@ tape( 'the function assigns elements from n 0-dimensional input ndarray to an ou
6667
t.end();
6768
});
6869

69-
tape( 'the function assigns elements from n 0-dimensional input ndarray to an output ndarray (accessors)', function test( t ) {
70+
tape( 'the function assigns elements from a 0-dimensional input ndarray to an output ndarray (accessors)', function test( t ) {
7071
var condition;
7172
var out;
7273
var x;
7374
var y;
7475

75-
condition = ndarray( 'uint8', toAccessorArray( [ 1 ] ), [], [ 0 ], 0, 'row-major' );
76+
condition = ndarray( 'uint8', new Uint8Array( [ 1 ]), [], [ 0 ], 0, 'row-major' );
7677
x = ndarray( 'generic', toAccessorArray( [ 1.0 ] ), [], [ 0 ], 0, 'row-major' );
7778
y = ndarray( 'generic', toAccessorArray( [ -1.0 ] ), [], [ 0 ], 0, 'row-major' );
7879
out = ndarray( 'generic', toAccessorArray( [ 0.0 ] ), [], [ 0 ], 0, 'row-major' );

lib/node_modules/@stdlib/ndarray/base/where/test/test.1d.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ tape( 'the function conditionally assigns elements from a 1-dimensional input nd
173173
var x;
174174
var y;
175175

176-
cbuf = [
176+
cbuf = new Uint8Array([
177177
0,
178178
0,
179179
0,
180180
0
181-
];
181+
]);
182182
xbuf = [
183183
1.0,
184184
2.0,
@@ -191,7 +191,7 @@ tape( 'the function conditionally assigns elements from a 1-dimensional input nd
191191
-3.0,
192192
-4.0
193193
];
194-
condition = ndarray( 'uint8', toAccessorArray( cbuf ), [ 4 ], [ 1 ], 0, 'row-major' );
194+
condition = ndarray( 'uint8', cbuf, [ 4 ], [ 1 ], 0, 'row-major' );
195195
x = ndarray( 'generic', toAccessorArray( xbuf ), [ 4 ], [ 1 ], 0, 'row-major' );
196196
y = ndarray( 'generic', toAccessorArray( ybuf ), [ 4 ], [ 1 ], 0, 'row-major' );
197197
out = ndarray( 'generic', toAccessorArray( zeros( 4, 'generic' ) ), [ 4 ], [ 1 ], 0, 'row-major' );

0 commit comments

Comments
 (0)