Skip to content

Commit dcab5b7

Browse files
committed
feat: add nd kernels
1 parent b97de57 commit dcab5b7

File tree

7 files changed

+273
-10
lines changed

7 files changed

+273
-10
lines changed

lib/node_modules/@stdlib/ndarray/base/find/benchmark/benchmark.1d_columnmajor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ function createBenchmark( len, shape, xtype ) {
9292
b.tic();
9393
for ( i = 0; i < b.iterations; i++ ) {
9494
out = find( [ x, v ], clbk );
95-
if ( typeof out !== 'boolean' ) {
95+
if ( isnan( out ) ) {
9696
b.fail( 'should not return NaN' );
9797
}
9898
}
9999
b.toc();
100-
if ( !isnan( out ) ) {
100+
if ( isnan( out ) ) {
101101
b.fail( 'should not return NaN' );
102102
}
103103
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/ndarray/base/find/benchmark/benchmark.1d_rowmajor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ function createBenchmark( len, shape, xtype ) {
9393
for ( i = 0; i < b.iterations; i++ ) {
9494
out = find( [ x, sv ], clbk );
9595
if ( isnan( out ) ) {
96-
b.fail( 'should return NaN' );
96+
b.fail( 'should not return NaN' );
9797
}
9898
}
9999
b.toc();
100-
if ( !isnan( out ) ) {
101-
b.fail( 'should return NaN' );
100+
if ( isnan( out ) ) {
101+
b.fail( 'should not return NaN' );
102102
}
103103
b.pass( 'benchmark finished' );
104104
b.end();

lib/node_modules/@stdlib/ndarray/base/find/benchmark/benchmark.2d_blocked_columnmajor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function createBenchmark( len, shape, xtype ) {
9191
}
9292
b.toc();
9393
if ( isnan( out ) ) {
94-
b.fail( 'should return not return NaN' );
94+
b.fail( 'should not return NaN' );
9595
}
9696
b.pass( 'benchmark finished' );
9797
b.end();

lib/node_modules/@stdlib/ndarray/base/find/benchmark/benchmark.2d_blocked_rowmajor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ function createBenchmark( len, shape, xtype ) {
8686
for ( i = 0; i < b.iterations; i++ ) {
8787
out = find( x, 101, clbk );
8888
if ( isnan( out ) ) {
89-
b.fail( 'should return not return NaN' );
89+
b.fail( 'should not return NaN' );
9090
}
9191
}
9292
b.toc();
9393
if ( isnan( out ) ) {
94-
b.fail( 'should return not return NaN' );
94+
b.fail( 'should not return NaN' );
9595
}
9696
b.pass( 'benchmark finished' );
9797
b.end();

lib/node_modules/@stdlib/ndarray/base/find/benchmark/benchmark.2d_columnmajor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ function createBenchmark( len, shape, xtype ) {
8686
for ( i = 0; i < b.iterations; i++ ) {
8787
out = find( x, 101, clbk );
8888
if ( isnan( out ) ) {
89-
b.fail( 'should return not return NaN' );
89+
b.fail( 'should not return NaN' );
9090
}
9191
}
9292
b.toc();
9393
if ( isnan( out ) ) {
94-
b.fail( 'should return not return NaN' );
94+
b.fail( 'should not return NaN' );
9595
}
9696
b.pass( 'benchmark finished' );
9797
b.end();
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var numel = require( '@stdlib/ndarray/base/numel' );
24+
var vind2bind = require( '@stdlib/ndarray/base/vind2bind' );
25+
var ind2sub = require( '@stdlib/ndarray/base/ind2sub' );
26+
27+
28+
// VARIABLES //
29+
30+
var MODE = 'throw';
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* Returns the first element in an ndarray which pass a test implemented by a predicate function.
37+
*
38+
* @private
39+
* @param {Object} x - object containing ndarray meta data
40+
* @param {ndarrayLike} x.ref - reference to the original ndarray-like object
41+
* @param {string} x.dtype - data type
42+
* @param {Collection} x.data - data buffer
43+
* @param {NonNegativeIntegerArray} x.shape - dimensions
44+
* @param {IntegerArray} x.strides - stride lengths
45+
* @param {NonNegativeInteger} x.offset - index offset
46+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
47+
* @param {*} sentinelValue - sentinel value
48+
* @param {Function} predicate - predicate function
49+
* @param {*} thisArg - predicate function execution context
50+
* @returns {*} result
51+
*
52+
* @example
53+
* var Float64Array = require( '@stdlib/array/float64' );
54+
*
55+
* function predicate( value ) {
56+
* return value % 2.0 === 0.0;
57+
* }
58+
*
59+
* // Create a data buffer:
60+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
61+
*
62+
* // Define the shape of the input array:
63+
* var shape = [ 2, 2 ];
64+
*
65+
* // Define the array strides:
66+
* var sx = [ 4, 1 ];
67+
*
68+
* // Define the index offset:
69+
* var ox = 1;
70+
*
71+
* // Create the input ndarray-like object:
72+
* var x = {
73+
* 'ref': null,
74+
* 'dtype': 'float64',
75+
* 'data': xbuf,
76+
* 'shape': shape,
77+
* 'strides': sx,
78+
* 'offset': ox,
79+
* 'order': 'row-major'
80+
* };
81+
*
82+
* // Test elements:
83+
* var out = findnd( x, -1, predicate );
84+
* // returns 2.0
85+
*/
86+
function findnd( x, sentinelValue, predicate, thisArg ) {
87+
var xbuf;
88+
var ordx;
89+
var idx;
90+
var len;
91+
var sh;
92+
var sx;
93+
var ox;
94+
var ix;
95+
var i;
96+
97+
sh = x.shape;
98+
99+
// Compute the total number of elements over which to iterate:
100+
len = numel( sh );
101+
102+
// Cache a reference to the input ndarray data buffer:
103+
xbuf = x.data;
104+
105+
// Cache a reference to the stride array:
106+
sx = x.strides;
107+
108+
// Cache the index of the first indexed element:
109+
ox = x.offset;
110+
111+
// Cache the array order:
112+
ordx = x.order;
113+
114+
// Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory...
115+
for ( i = 0; i < len; i++ ) {
116+
ix = vind2bind( sh, sx, ox, ordx, i, MODE );
117+
idx = ind2sub( sh, sx, 0, ordx, i, MODE ); // return subscripts from the perspective of the ndarray view
118+
if ( predicate.call( thisArg, xbuf[ ix ], idx, x.ref ) ) {
119+
return xbuf[ ix ];
120+
}
121+
}
122+
return sentinelValue;
123+
}
124+
125+
126+
// EXPORTS //
127+
128+
module.exports = findnd;
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var numel = require( '@stdlib/ndarray/base/numel' );
24+
var vind2bind = require( '@stdlib/ndarray/base/vind2bind' );
25+
var ind2sub = require( '@stdlib/ndarray/base/ind2sub' );
26+
27+
28+
// VARIABLES //
29+
30+
var MODE = 'throw';
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* Returns the first element in an ndarray which pass a test implemented by a predicate function.
37+
*
38+
* @private
39+
* @param {Object} x - object containing ndarray meta data
40+
* @param {ndarrayLike} x.ref - reference to the original ndarray-like object
41+
* @param {string} x.dtype - data type
42+
* @param {Collection} x.data - data buffer
43+
* @param {NonNegativeIntegerArray} x.shape - dimensions
44+
* @param {IntegerArray} x.strides - stride lengths
45+
* @param {NonNegativeInteger} x.offset - index offset
46+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
47+
* @param {Array<Function>} x.accessors - data buffer accessors
48+
* @param {*} sentinelValue - sentinel value
49+
* @param {Function} predicate - predicate function
50+
* @param {*} thisArg - predicate function execution context
51+
* @returns {*} result
52+
*
53+
* @example
54+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
55+
* var accessors = require( '@stdlib/array/base/accessors' );
56+
*
57+
* function predicate( value ) {
58+
* return value % 2.0 === 0.0;
59+
* }
60+
*
61+
* // Create a data buffer:
62+
* var xbuf = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
63+
*
64+
* // Define the shape of the input array:
65+
* var shape = [ 2, 2 ];
66+
*
67+
* // Define the array strides:
68+
* var sx = [ 4, 1 ];
69+
*
70+
* // Define the index offset:
71+
* var ox = 0;
72+
*
73+
* // Create the input ndarray-like object:
74+
* var x = {
75+
* 'ref': null,
76+
* 'dtype': 'generic',
77+
* 'data': xbuf,
78+
* 'shape': shape,
79+
* 'strides': sx,
80+
* 'offset': ox,
81+
* 'order': 'row-major',
82+
* 'accessors': accessors( xbuf ).accessors
83+
* };
84+
*
85+
* // Test elements:
86+
* var out = findnd( x, -1, predicate );
87+
* // returns 2.0
88+
*/
89+
function findnd( x, sentinelValue, predicate, thisArg ) {
90+
var xbuf;
91+
var ordx;
92+
var idx;
93+
var len;
94+
var get;
95+
var sh;
96+
var sx;
97+
var ox;
98+
var ix;
99+
var i;
100+
101+
sh = x.shape;
102+
103+
// Compute the total number of elements over which to iterate:
104+
len = numel( sh );
105+
106+
// Cache a reference to the input ndarray data buffer:
107+
xbuf = x.data;
108+
109+
// Cache a reference to the stride array:
110+
sx = x.strides;
111+
112+
// Cache the index of the first indexed element:
113+
ox = x.offset;
114+
115+
// Cache the array order:
116+
ordx = x.order;
117+
118+
// Cache accessor:
119+
get = x.accessors[ 0 ];
120+
121+
// Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory...
122+
for ( i = 0; i < len; i++ ) {
123+
ix = vind2bind( sh, sx, ox, ordx, i, MODE );
124+
idx = ind2sub( sh, sx, 0, ordx, i, MODE ); // return subscripts from the perspective of the ndarray view
125+
if ( predicate.call( thisArg, get( xbuf, ix ), idx, x.ref ) ) {
126+
return get( xbuf, ix );
127+
}
128+
}
129+
return sentinelValue;
130+
}
131+
132+
133+
// EXPORTS //
134+
135+
module.exports = findnd;

0 commit comments

Comments
 (0)