Skip to content

Commit 4768153

Browse files
committed
feat: add 4d 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 6c4416b commit 4768153

File tree

9 files changed

+739
-5
lines changed

9 files changed

+739
-5
lines changed

lib/node_modules/@stdlib/ndarray/base/find/lib/2d_blocked.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var reverse = require( '@stdlib/array/base/reverse' );
2929
// MAIN //
3030

3131
/**
32-
* Returns the first element in an ndarray which passes a test implemented by a predicate function.
32+
* Returns the first element in an ndarray which passes a test implemented by a predicate function via loop blocking.
3333
*
3434
* @private
3535
* @param {Object} x - object containing ndarray meta data

lib/node_modules/@stdlib/ndarray/base/find/lib/2d_blocked_accessors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var reverse = require( '@stdlib/array/base/reverse' );
2929
// MAIN //
3030

3131
/**
32-
* Returns the first element in an ndarray which passes a test implemented by a predicate function.
32+
* Returns the first element in an ndarray which passes a test implemented by a predicate function via loop blocking.
3333
*
3434
* @private
3535
* @param {Object} x - object containing ndarray meta data

lib/node_modules/@stdlib/ndarray/base/find/lib/3d.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ var take = require( '@stdlib/array/base/take-indexed' );
7676
* };
7777
*
7878
* // Test elements:
79-
* var out = find3d( x, NaN, predicate );
79+
* var out = find3d( x, NaN, predicate );
8080
* // returns 2.0
8181
*/
8282
function find3d( x, sentinelValue, predicate, thisArg ) {

lib/node_modules/@stdlib/ndarray/base/find/lib/3d_blocked.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var reverse = require( '@stdlib/array/base/reverse' );
3131
// MAIN //
3232

3333
/**
34-
* Returns the first element in an ndarray which passes a test implemented by a predicate function.
34+
* Returns the first element in an ndarray which passes a test implemented by a predicate function via loop blocking.
3535
*
3636
* @private
3737
* @param {Object} x - object containing ndarray meta data

lib/node_modules/@stdlib/ndarray/base/find/lib/3d_blocked_accessors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var reverse = require( '@stdlib/array/base/reverse' );
3131
// MAIN //
3232

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

0 commit comments

Comments
 (0)