8000 feat: add `ndarray/base/find` by headlessNode · Pull Request #7426 · stdlib-js/stdlib · GitHub
[go: up one dir, main page]

Skip to content

feat: add ndarray/base/find #7426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b97de57
feat: add ndarray/base/find
headlessNode Jun 20, 2025
dcab5b7
feat: add nd kernels
headlessNode Jun 20, 2025
66764f1
docs: fix return value
headlessNode Jun 20, 2025
12a4b61
fix: lint errors
headlessNode Jun 21, 2025
76a9ba1
fix: lint error
headlessNode Jun 21, 2025
44f5cba
fix: lint error
headlessNode Jun 21, 2025
fa9ef2a
fix: lint errors
headlessNode Jun 21, 2025
d650d02
bench: make consistent & worst case scenario
headlessNode Jun 21, 2025
2ca2802
chore: make consistent
headlessNode Jun 21, 2025
f7fe883
test: add tests upto 2d
headlessNode Jun 21, 2025
a776c3b
refactor: add sentinel cases
headlessNode Jun 25, 2025
c9734b5
test: add 3d tests
headlessNode Jun 25, 2025
79eb09a
refactor: use as sentinel
headlessNode Jun 25, 2025
c62bd4d
docs: apply code review suggestions
headlessNode Jul 2, 2025
5dc4859
bench: apply suggestions from code review
headlessNode Jul 2, 2025
6c4416b
refactor: apply suggestions from code review
headlessNode Jul 2, 2025
4768153
feat: add 4d kernels
headlessNode Jul 2, 2025
f2ee3dc
feat: add 5d kernels
headlessNode Jul 2, 2025
f75fe1b
feat: add 6d kernels
headlessNode Jul 2, 2025
85b845d
feat: add 7d kernels
headlessNode Jul 2, 2025
d7f8d34
feat: add 8d kernels
headlessNode Jul 2, 2025
2abe234
feat: add 9d kernels
headlessNode Jul 2, 2025
eecb8c9
feat: add 10d kernels
headlessNode Jul 2, 2025
a17f0a4
bench: add benchmarks upto 5d
headlessNode Jul 2, 2025
3a31376
bench: add 6d benchmarks
headlessNode Jul 2, 2025
1595c41
bench: add 7d benchmarks
headlessNode Jul 2, 2025
3d2d0cf
bench: add 8d benchmarks
headlessNode Jul 2, 2025
6cd22c0
bench: add 9d benchmarks
headlessNode Jul 2, 2025
48360d7
bench: add 10d benchmarks
headlessNode Jul 2, 2025
b55394c
bench: add 11d benchmarks
headlessNode Jul 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add 7d kernels
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
  • Loading branch information
headlessNode committed Jul 2, 2025
commit 85b845d4026ac2758663f34587de34e09c3c20dd
189 changes: 189 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/find/lib/7d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-disable max-depth */

'use strict';

// MODULES //

var strides2order = require( '@stdlib/ndarray/base/strides2order' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var reverse = require( '@stdlib/array/base/reverse' );
var take = require( '@stdlib/array/base/take-indexed' );


// MAIN //

/**
* Returns the first element in an ndarray which passes a test implemented by a predicate function.
*
* @private
* @param {Object} x - object containing ndarray meta data
* @param {ndarrayLike} x.ref - reference to the original ndarray-like object
* @param {string} x.dtype - data type
* @param {Collection} x.data - data buffer
* @param {NonNegativeIntegerArray} x.shape - dimensions
* @param {IntegerArray} x.strides - stride lengths
* @param {NonNegativeInteger} x.offset - index offset
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
* @param {*} sentinelValue - sentinel value
* @param {Function} predicate - predicate function
* @param {*} thisArg - predicate function execution context
* @returns {*} result
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* function predicate( value ) {
* return value % 2.0 === 0.0;
* }
*
* // Create a data buffer:
* 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 ] );
*
* // Define the shape of the input array:
* var shape = [ 1, 1, 1, 1, 3, 1, 2 ];
*
* // Define the array strides:
* var sx = [ 12, 12, 12, 12, 4, 4, 1 ];
*
* // Define the index offset:
* var ox = 1;
*
* // Create the input ndarray-like object:
* var x = {
* 'ref': null,
* 'dtype': 'float64',
* 'data': xbuf,
* 'shape': shape,
* 'strides': sx,
* 'offset': ox,
* 'order': 'row-major'
* };
*
* // Test elements:
* var out = find7d( x, NaN, predicate );
* // returns 2.0
*/
function find7d( x, sentinelValue, predicate, thisArg ) {
var xbuf;
var idx;
var dx0;
var dx1;
var dx2;
var dx3;
var dx4;
var dx5;
var dx6;
var sh;
var S0;
var S1;
var S2;
var S3;
var S4;
var S5;
var S6;
var sx;
var ix;
var i0;
var i1;
var i2;
var i3;
var i4;
var i5;
var i6;

// Note on variable naming convention: S#, dx#, i# where # corresponds to the loop number, with `0` being the innermost loop...

// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
sh = x.shape;
sx = x.strides;
idx = zeroTo( sh.length );
if ( strides2order( sx ) === 1 ) {
// For row-major ndarrays, the last dimensions have the fastest changing indices...
S0 = sh[ 6 ];
S1 = sh[ 5 ];
S2 = sh[ 4 ];
S3 = sh[ 3 ];
S4 = sh[ 2 ];
S5 = sh[ 1 ];
S6 = sh[ 0 ];
dx0 = sx[ 6 ]; // offset increment for innermost loop
dx1 = sx[ 5 ] - ( S0*sx[6] );
dx2 = sx[ 4 ] - ( S1*sx[5] );
dx3 = sx[ 3 ] - ( S2*sx[4] );
dx4 = sx[ 2 ] - ( S3*sx[3] );
dx5 = sx[ 1 ] - ( S4*sx[2] );
dx6 = sx[ 0 ] - ( S5*sx[1] ); // offset increment for outermost loop
} else { // order === 'column-major'
// For column-major ndarrays, the first dimensions have the fastest changing indices...
S0 = sh[ 0 ];
S1 = sh[ 1 ];
S2 = sh[ 2 ];
S3 = sh[ 3 ];
S4 = sh[ 4 ];
S5 = sh[ 5 ];
S6 = sh[ 6 ];
dx0 = sx[ 0 ]; // offset increment for innermost loop
dx1 = sx[ 1 ] - ( S0*sx[0] );
dx2 = sx[ 2 ] - ( S1*sx[1] );
dx3 = sx[ 3 ] - ( S2*sx[2] );
dx4 = sx[ 4 ] - ( S3*sx[3] );
dx5 = sx[ 5 ] - ( S4*sx[4] );
dx6 = sx[ 6 ] - ( S5*sx[5] ); // offset increment for outermost loop
idx = reverse( idx );
}
// Set a pointer to the first indexed element:
ix = x.offset;

// Cache a reference to the input ndarray buffer:
xbuf = x.data;

// Iterate over the ndarray dimensions...
for ( i6 = 0; i6 < S6; i6++ ) {
for ( i5 = 0; i5 < S5; i5++ ) {
for ( i4 = 0; i4 < S4; i4++ ) {
for ( i3 = 0; i3 < S3; i3++ ) {
for ( i2 = 0; i2 < S2; i2++ ) {
for ( i1 = 0; i1 < S1; i1++ ) {
for ( i0 = 0; i0 < S0; i0++ ) {
if ( predicate.call( thisArg, xbuf[ ix ], take( [ i6, i5, i4, i3, i2, i1, i0 ], idx ), x.ref ) ) { // eslint-disable-line max-len
return xbuf[ ix ];
}
ix += dx0;
}
ix += dx1;
}
ix += dx2;
}
ix += dx3;
}
ix += dx4;
}
ix += dx5;
}
ix += dx6;
}
return sentinelValue;
}


// EXPORTS //

module.exports = find7d;
196 changes: 196 additions 8000 & 0 deletions lib/node_modules/@stdlib/ndarray/base/find/lib/7d_accessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-disable max-depth */

'use strict';

// MODULES //

var strides2order = require( '@stdlib/ndarray/base/strides2order' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var reverse = require( '@stdlib/array/base/reverse' );
var take = require( '@stdlib/array/base/take-indexed' );


// MAIN //

/**
* Returns the first element in an ndarray which passes a test implemented by a predicate function.
*
* @private
* @param {Object} x - object containing ndarray meta data
* @param {ndarrayLike} x.ref - reference to the original ndarray-like object
* @param {string} x.dtype - data type
* @param {Collection} x.data - data buffer
* @param {NonNegativeIntegerArray} x.shape - dimensions
* @param {IntegerArray} x.strides - stride lengths
* @param {NonNegativeInteger} x.offset - index offset
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
* @param {Array<Function>} x.accessors - data buffer accessors
* @param {*} sentinelValue - sentinel value
* @param {Function} predicate - predicate function
* @param {*} thisArg - predicate function execution context
* @returns {*} result
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var accessors = require( '@stdlib/array/base/accessors' );
*
* function predicate( value ) {
* return value % 2.0 === 0.0;
* }
*
* // Create a data buffer:
* var xbuf = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
*
* // Define the shape of the input array:
* var shape = [ 1, 1, 1, 1, 2, 2, 2 ];
*
* // Define the array strides:
* var sx = [ 8, 8, 8, 8, 4, 2, 1 ];
*
* // Define the index offset:
* var ox = 0;
*
* // Create the input ndarray-like object:
* var x = {
* 'ref': null,
* 'dtype': 'generic',
* 'data': xbuf,
* 'shape': shape,
* 'strides': sx,
* 'offset': ox,
* 'order': 'row-major',
* 'accessors': accessors( xbuf ).accessors
* };
*
* // Test elements:
* var out = find7d( x, NaN, predicate );
* // returns 2.0
*/
function find7d( x, sentinelValue, predicate, thisArg ) {
var xbuf;
var idx;
var get;
var dx0;
var dx1;
var dx2;
var dx3;
var dx4;
var dx5;
var dx6;
var sh;
var S0;
var S1;
var S2;
var S3;
var S4;
var S5;
var S6;
var sx;
var ix;
var i0;
var i1;
var i2;
var i3;
var i4;
var i5;
var i6;

// Note on variable naming convention: S#, dx#, i# where # corresponds to the loop number, with `0` being the innermost loop...

// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
sh = x.shape;
sx = x.strides;
idx = zeroTo( sh.length );
if ( strides2order( sx ) === 1 ) {
// For row-major ndarrays, the last dimensions have the fastest changing indices...
S0 = sh[ 6 ];
S1 = sh[ 5 ];
S2 = sh[ 4 ];
S3 = sh[ 3 ];
S4 = sh[ 2 ];
S5 = sh[ 1 ];
S6 = sh[ 0 ];
dx0 = sx[ 6 ]; // offset increment for innermost loop
dx1 = sx[ 5 ] - ( S0*sx[6] );
dx2 = sx[ 4 ] - ( S1*sx[5] );
dx3 = sx[ 3 ] - ( S2*sx[4] );
dx4 = sx[ 2 ] - ( S3*sx[3] );
dx5 = sx[ 1 ] - ( S4*sx[2] );
dx6 = sx[ 0 ] - ( S5*sx[1] ); // offset increment for outermost loop
} else { // order === 'column-major'
// For column-major ndarrays, the first dimensions have the fastest changing indices...
S0 = sh[ 0 ];
S1 = sh[ 1 ];
S2 = sh[ 2 ];
S3 = sh[ 3 ];
S4 = sh[ 4 ];
S5 = sh[ 5 ];
S6 = sh[ 6 ];
dx0 = sx[ 0 ]; // offset increment for innermost loop
dx1 = sx[ 1 ] - ( S0*sx[0] );
dx2 = sx[ 2 ] - ( S1*sx[1] );
dx3 = sx[ 3 ] - ( S2*sx[2] );
dx4 = sx[ 4 ] - ( S3*sx[3] );
dx5 = sx[ 5 ] - ( S4*sx[4] );
dx6 = sx[ 6 ] - ( S5*sx[5] ); // offset increment for outermost loop
idx = reverse( idx );
}
// Set a pointer to the first indexed element:
ix = x.offset;

// Cache a reference to the input ndarray buffer:
xbuf = x.data;

// Cache accessor:
get = x.accessors[ 0 ];

// Iterate over the ndarray dimensions...
for ( i6 = 0; i6 < S6; i6++ ) {
for ( i5 = 0; i5 < S5; i5++ ) {
for ( i4 = 0; i4 < S4; i4++ ) {
for ( i3 = 0; i3 < S3; i3++ ) {
for ( i2 = 0; i2 < S2; i2++ ) {
for ( i1 = 0; i1 < S1; i1++ ) {
for ( i0 = 0; i0 < S0; i0++ ) {
if ( predicate.call( thisArg, get( xbuf, ix ), take( [ i6, i5, i4, i3, i2, i1, i0 ], idx ), x.ref ) ) { // eslint-disable-line max-len
return get( xbuf, ix );
}
ix += dx0;
}
ix += dx1;
}
ix += dx2;
}
ix += dx3;
}
ix += dx4;
}
ix += dx5;
}
ix += dx6;
}
return sentinelValue;
}


// EXPORTS //

module.exports = find7d;
Loading
Loading
0