Skip to content

Commit f2ee3dc

Browse files
committed
feat: add 5d 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 ---
1 parent 4768153 commit f2ee3dc

File tree

4 files changed

+796
-0
lines changed

4 files changed

+796
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
/* eslint-disable max-depth */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var reverse = require( '@stdlib/array/base/reverse' );
28+
var take = require( '@stdlib/array/base/take-indexed' );
29+
30+
31+
// MAIN //
32+
33+
/**
34+
* Returns the first element in an ndarray which passes a test implemented by a predicate function.
35+
*
36+
* @private
37+
* @param {Object} x - object containing ndarray meta data
38+
* @param {ndarrayLike} x.ref - reference to the original ndarray-like object
39+
* @param {string} x.dtype - data type
40+
* @param {Collection} x.data - data buffer
41+
* @param {NonNegativeIntegerArray} x.shape - dimensions
42+
* @param {IntegerArray} x.strides - stride lengths
43+
* @param {NonNegativeInteger} x.offset - index offset
44+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
45+
* @param {*} sentinelValue - sentinel value
46+
* @param {Function} predicate - predicate function
47+
* @param {*} thisArg - predicate function execution context
48+
* @returns {*} result
49+
*
50+
* @example
51+
* var Float64Array = require( '@stdlib/array/float64' );
52+
*
53+
* function predicate( value ) {
54+
* return value % 2.0 === 0.0;
55+
* }
56+
*
57+
* // Create a data buffer:
58+
* 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 ] );
59+
*
60+
* // Define the shape of the input array:
61+
* var shape = [ 1, 1, 3, 1, 2 ];
62+
*
63+
* // Define the array strides:
64+
* var sx = [ 12, 12, 4, 4, 1 ];
65+
*
66+
* // Define the index offset:
67+
* var ox = 1;
68+
*
69+
* // Create the input ndarray-like object:
70+
* var x = {
71+
* 'ref': null,
72+
* 'dtype': 'float64',
73+
* 'data': xbuf,
74+
* 'shape': shape,
75+
* 'strides': sx,
76+
* 'offset': ox,
77+
* 'order': 'row-major'
78+
* };
79+
*
80+
* // Test elements:
81+
* var out = find5d( x, NaN, predicate );
82+
* // returns 2.0
83+
*/
84+
function find5d( x, sentinelValue, predicate, thisArg ) {
85+
var xbuf;
86+
var idx;
87+
var dx0;
88+
var dx1;
89+
var dx2;
90+
var dx3;
91+
var dx4;
92+
var sh;
93+
var S0;
94+
var S1;
95+
var S2;
96+
var S3;
97+
var S4;
98+
var sx;
99+
var ix;
100+
var i0;
101+
var i1;
102+
var i2;
103+
var i3;
104+
var i4;
105+
106+
// Note on variable naming convention: S#, dx#, i# where # corresponds to the loop number, with `0` being the innermost loop...
107+
108+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
109+
sh = x.shape;
110+
sx = x.strides;
111+
idx = zeroTo( sh.length );
112+
if ( strides2order( sx ) === 1 ) {
113+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
114+
S0 = sh[ 4 ];
115+
S1 = sh[ 3 ];
116+
S2 = sh[ 2 ];
117+
S3 = sh[ 1 ];
118+
S4 = sh[ 0 ];
119+
dx0 = sx[ 4 ]; // offset increment for innermost loop
120+
dx1 = sx[ 3 ] - ( S0*sx[4] );
121+
dx2 = sx[ 2 ] - ( S1*sx[3] );
122+
dx3 = sx[ 1 ] - ( S2*sx[2] );
123+
dx4 = sx[ 0 ] - ( S3*sx[1] ); // offset increment for outermost loop
124+
} else { // order === 'column-major'
125+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
126+
S0 = sh[ 0 ];
127+
S1 = sh[ 1 ];
128+
S2 = sh[ 2 ];
129+
S3 = sh[ 3 ];
130+
S4 = sh[ 4 ];
131+
dx0 = sx[ 0 ]; // offset increment for innermost loop
132+
dx1 = sx[ 1 ] - ( S0*sx[0] );
133+
dx2 = sx[ 2 ] - ( S1*sx[1] );
134+
dx3 = sx[ 3 ] - ( S2*sx[2] );
135+
dx4 = sx[ 4 ] - ( S3*sx[3] ); // offset increment for outermost loop
136+
idx = reverse( idx );
137+
}
138+
// Set a pointer to the first indexed element:
139+
ix = x.offset;
140+
141+
// Cache a reference to the input ndarray buffer:
142+
xbuf = x.data;
143+
144+
// Iterate over the ndarray dimensions...
145+
for ( i4 = 0; i4 < S4; i4++ ) {
146+
for ( i3 = 0; i3 < S3; i3++ ) {
147+
for ( i2 = 0; i2 < S2; i2++ ) {
148+
for ( i1 = 0; i1 < S1; i1++ ) {
149+
for ( i0 = 0; i0 < S0; i0++ ) {
150+
if ( predicate.call( thisArg, xbuf[ ix ], take( [ i4, i3, i2, i1, i0 ], idx ), x.ref ) ) { // eslint-disable-line max-len
151+
return xbuf[ ix ];
152+
}
153+
ix += dx0;
154+
}
155+
ix += dx1;
156+
}
157+
ix += dx2;
158+
}
159+
ix += dx3;
160+
}
161+
ix += dx4;
162+
}
163+
return sentinelValue;
164+
}
165+
166+
167+
// EXPORTS //
168+
169+
module.exports = find5d;
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
/* eslint-disable max-depth */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var reverse = require( '@stdlib/array/base/reverse' );
28+
var take = require( '@stdlib/array/base/take-indexed' );
29+
30+
31+
// MAIN //
32+
33+
/**
34+
* Returns the first element in an ndarray which passes a test implemented by a predicate function.
35+
*
36+
* @private
37+
* @param {Object} x - object containing ndarray meta data
38+
* @param {ndarrayLike} x.ref - reference to the original ndarray-like object
39+
* @param {string} x.dtype - data type
40+
* @param {Collection} x.data - data buffer
41+
* @param {NonNegativeIntegerArray} x.shape - dimensions
42+
* @param {IntegerArray} x.strides - stride lengths
43+
* @param {NonNegativeInteger} x.offset - index offset
44+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
45+
* @param {Array<Function>} x.accessors - data buffer accessors
46+
* @param {*} sentinelValue - sentinel value
47+
* @param {Function} predicate - predicate function
48+
* @param {*} thisArg - predicate function execution context
49+
* @returns {*} result
50+
*
51+
* @example
52+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
53+
* var accessors = require( '@stdlib/array/base/accessors' );
54+
*
55+
* function predicate( value ) {
56+
* return value % 2.0 === 0.0;
57+
* }
58+
*
59+
* // Create a data buffer:
60+
* var xbuf = toAccessorArray( [ 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 = [ 1, 1, 2, 2, 2 ];
64+
*
65+
* // Define the array strides:
66+
* var sx = [ 8, 8, 4, 2, 1 ];
67+
*
68+
* // Define the index offset:
69+
* var ox = 0;
70+
*
71+
* // Create the input ndarray-like object:
72+
* var x = {
73+
* 'ref': null,
74+
* 'dtype': 'generic',
75+
* 'data': xbuf,
76+
* 'shape': shape,
77+
* 'strides': sx,
78+
* 'offset': ox,
79+
* 'order': 'row-major',
80+
* 'accessors': accessors( xbuf ).accessors
81+
* };
82+
*
83+
* // Test elements:
84+
* var out = find5d( x, NaN, predicate );
85+
* // returns 2.0
86+
*/
87+
function find5d( x, sentinelValue, predicate, thisArg ) {
88+
var xbuf;
89+
var idx;
90+
var get;
91+
var dx0;
92+
var dx1;
93+
var dx2;
94+
var dx3;
95+
var dx4;
96+
var sh;
97+
var S0;
98+
var S1;
99+
var S2;
100+
var S3;
101+
var S4;
102+
var sx;
103+
var ix;
104+
var i0;
105+
var i1;
106+
var i2;
107+
var i3;
108+
var i4;
109+
110+
// Note on variable naming convention: S#, dx#, i# where # corresponds to the loop number, with `0` being the innermost loop...
111+
112+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
113+
sh = x.shape;
114+
sx = x.strides;
115+
idx = zeroTo( sh.length );
116+
if ( strides2order( sx ) === 1 ) {
117+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
118+
S0 = sh[ 4 ];
119+
S1 = sh[ 3 ];
120+
S2 = sh[ 2 ];
121+
S3 = sh[ 1 ];
122+
S4 = sh[ 0 ];
123+
dx0 = sx[ 4 ]; // offset increment for innermost loop
124+
dx1 = sx[ 3 ] - ( S0*sx[4] );
125+
dx2 = sx[ 2 ] - ( S1*sx[3] );
126+
dx3 = sx[ 1 ] - ( S2*sx[2] );
127+
dx4 = sx[ 0 ] - ( S3*sx[1] ); // offset increment for outermost loop
128+
} else { // order === 'column-major'
129+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
130+
S0 = sh[ 0 ];
131+
S1 = sh[ 1 ];
132+
S2 = sh[ 2 ];
133+
S3 = sh[ 3 ];
134+
S4 = sh[ 4 ];
135+
dx0 = sx[ 0 ]; // offset increment for innermost loop
136+
dx1 = sx[ 1 ] - ( S0*sx[0] );
137+
dx2 = sx[ 2 ] - ( S1*sx[1] );
138+
dx3 = sx[ 3 ] - ( S2*sx[2] );
139+
dx4 = sx[ 4 ] - ( S3*sx[3] ); // offset increment for outermost loop
140+
idx = reverse( idx );
141+
}
142+
// Set a pointer to the first indexed element:
143+
ix = x.offset;
144+
145+
// Cache a reference to the input ndarray buffer:
146+
xbuf = x.data;
147+
148+
// Cache accessor:
149+
get = x.accessors[ 0 ];
150+
151+
// Iterate over the ndarray dimensions...
152+
for ( i4 = 0; i4 < S4; i4++ ) {
153+
for ( i3 = 0; i3 < S3; i3++ ) {
154+
for ( i2 = 0; i2 < S2; i2++ ) {
155+
for ( i1 = 0; i1 < S1; i1++ ) {
156+
for ( i0 = 0; i0 < S0; i0++ ) {
157+
if ( predicate.call( thisArg, get( xbuf, ix ), take( [ i4, i3, i2, i1, i0 ], idx ), x.ref ) ) { // eslint-disable-line max-len
158+
return get( xbuf, ix );
159+
}
160+
ix += dx0;
161+
}
162+
ix += dx1;
163+
}
164+
ix += dx2;
165+
}
166+
ix += dx3;
167+
}
168+
ix += dx4;
169+
}
170+
return sentinelValue;
171+
}
172+
173+
174+
// EXPORTS //
175+
176+
module.exports = find5d;

0 commit comments

Comments
 (0)