Skip to content

Commit 85b845d

Browse files
committed
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 ---
1 parent f75fe1b commit 85b845d

File tree

4 files changed

+912
-0
lines changed

4 files changed

+912
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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, 1, 1, 3, 1, 2 ];
62+
*
63+
* // Define the array strides:
64+
* var sx = [ 12, 12, 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 = find7d( x, NaN, predicate );
82+
* // returns 2.0
83+
*/
84+
function find7d( 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 dx5;
93+
var dx6;
94+
var sh;
95+
var S0;
96+
var S1;
97+
var S2;
98+
var S3;
99+
var S4;
100+
var S5;
101+
var S6;
102+
var sx;
103+
var ix;
104+
var i0;
105+
var i1;
106+
var i2;
107+
var i3;
108+
var i4;
109+
var i5;
110+
var i6;
111+
112+
// Note on variable naming convention: S#, dx#, i# where # corresponds to the loop number, with `0` being the innermost loop...
113+
114+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
115+
sh = x.shape;
116+
sx = x.strides;
117+
idx = zeroTo( sh.length );
118+
if ( strides2order( sx ) === 1 ) {
119+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
120+
S0 = sh[ 6 ];
121+
S1 = sh[ 5 ];
122+
S2 = sh[ 4 ];
123+
S3 = sh[ 3 ];
124+
S4 = sh[ 2 ];
125+
S5 = sh[ 1 ];
126+
S6 = sh[ 0 ];
127+
dx0 = sx[ 6 ]; // offset increment for innermost loop
128+
dx1 = sx[ 5 ] - ( S0*sx[6] );
129+
dx2 = sx[ 4 ] - ( S1*sx[5] );
130+
dx3 = sx[ 3 ] - ( S2*sx[4] );
131+
dx4 = sx[ 2 ] - ( S3*sx[3] );
132+
dx5 = sx[ 1 ] - ( S4*sx[2] );
133+
dx6 = sx[ 0 ] - ( S5*sx[1] ); // offset increment for outermost loop
134+
} else { // order === 'column-major'
135+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
136+
S0 = sh[ 0 ];
137+
S1 = sh[ 1 ];
138+
S2 = sh[ 2 ];
139+
S3 = sh[ 3 ];
140+
S4 = sh[ 4 ];
141+
S5 = sh[ 5 ];
142+
S6 = sh[ 6 ];
143+
dx0 = sx[ 0 ]; // offset increment for innermost loop
144+
dx1 = sx[ 1 ] - ( S0*sx[0] );
145+
dx2 = sx[ 2 ] - ( S1*sx[1] );
146+
dx3 = sx[ 3 ] - ( S2*sx[2] );
147+
dx4 = sx[ 4 ] - ( S3*sx[3] );
148+
dx5 = sx[ 5 ] - ( S4*sx[4] );
149+
dx6 = sx[ 6 ] - ( S5*sx[5] ); // offset increment for outermost loop
150+
idx = reverse( idx );
151+
}
152+
// Set a pointer to the first indexed element:
153+
ix = x.offset;
154+
155+
// Cache a reference to the input ndarray buffer:
156+
xbuf = x.data;
157+
158+
// Iterate over the ndarray dimensions...
159+
for ( i6 = 0; i6 < S6; i6++ ) {
160+
for ( i5 = 0; i5 < S5; i5++ ) {
161+
for ( i4 = 0; i4 < S4; i4++ ) {
162+
for ( i3 = 0; i3 < S3; i3++ ) {
163+
for ( i2 = 0; i2 < S2; i2++ ) {
164+
for ( i1 = 0; i1 < S1; i1++ ) {
165+
for ( i0 = 0; i0 < S0; i0++ ) {
166+
if ( predicate.call( thisArg, xbuf[ ix ], take( [ i6, i5, i4, i3, i2, i1, i0 ], idx ), x.ref ) ) { // eslint-disable-line max-len
167+
return xbuf[ ix ];
168+
}
169+
ix += dx0;
170+
}
171+
ix += dx1;
172+
}
173+
ix += dx2;
174+
}
175+
ix += dx3;
176+
}
177+
ix += dx4;
178+
}
179+
ix += dx5;
180+
}
181+
ix += dx6;
182+
}
183+
return sentinelValue;
184+
}
185+
186+
187+
// EXPORTS //
188+
189+
module.exports = find7d;
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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, 1, 1, 2, 2, 2 ];
64+
*
65+
* // Define the array strides:
66+
* var sx = [ 8, 8, 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 = find7d( x, NaN, predicate );
85+
* // returns 2.0
86+
*/
87+
function find7d( 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 dx5;
97+
var dx6;
98+
var sh;
99+
var S0;
100+
var S1;
101+
var S2;
102+
var S3;
103+
var S4;
104+
var S5;
105+
var S6;
106+
var sx;
107+
var ix;
108+
var i0;
109+
var i1;
110+
var i2;
111+
var i3;
112+
var i4;
113+
var i5;
114+
var i6;
115+
116+
// Note on variable naming convention: S#, dx#, i# where # corresponds to the loop number, with `0` being the innermost loop...
117+
118+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
119+
sh = x.shape;
120+
sx = x.strides;
121+
idx = zeroTo( sh.length );
122+
if ( strides2order( sx ) === 1 ) {
123+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
124+
S0 = sh[ 6 ];
125+
S1 = sh[ 5 ];
126+
S2 = sh[ 4 ];
127+
S3 = sh[ 3 ];
128+
S4 = sh[ 2 ];
129+
S5 = sh[ 1 ];
130+
S6 = sh[ 0 ];
131+
dx0 = sx[ 6 ]; // offset increment for innermost loop
132+
dx1 = sx[ 5 ] - ( S0*sx[6] );
133+
dx2 = sx[ 4 ] - ( S1*sx[5] );
134+
dx3 = sx[ 3 ] - ( S2*sx[4] );
135+
dx4 = sx[ 2 ] - ( S3*sx[3] );
136+
dx5 = sx[ 1 ] - ( S4*sx[2] );
137+
dx6 = sx[ 0 ] - ( S5*sx[1] ); // offset increment for outermost loop
138+
} else { // order === 'column-major'
139+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
140+
S0 = sh[ 0 ];
141+
S1 = sh[ 1 ];
142+
S2 = sh[ 2 ];
143+
S3 = sh[ 3 ];
144+
S4 = sh[ 4 ];
145+
S5 = sh[ 5 ];
146+
S6 = sh[ 6 ];
147+
dx0 = sx[ 0 ]; // offset increment for innermost loop
148+
dx1 = sx[ 1 ] - ( S0*sx[0] );
149+
dx2 = sx[ 2 ] - ( S1*sx[1] );
150+
dx3 = sx[ 3 ] - ( S2*sx[2] );
151+
dx4 = sx[ 4 ] - ( S3*sx[3] );
152+
dx5 = sx[ 5 ] - ( S4*sx[4] );
153+
dx6 = sx[ 6 ] - ( S5*sx[5] ); // offset increment for outermost loop
154+
idx = reverse( idx );
155+
}
156+
// Set a pointer to the first indexed element:
157+
ix = x.offset;
158+
159+
// Cache a reference to the input ndarray buffer:
160+
xbuf = x.data;
161+
162+
// Cache accessor:
163+
get = x.accessors[ 0 ];
164+
165+
// Iterate over the ndarray dimensions...
166+
for ( i6 = 0; i6 < S6; i6++ ) {
167+
for ( i5 = 0; i5 < S5; i5++ ) {
168+
for ( i4 = 0; i4 < S4; i4++ ) {
169+
for ( i3 = 0; i3 < S3; i3++ ) {
170+
for ( i2 = 0; i2 < S2; i2++ ) {
171+
for ( i1 = 0; i1 < S1; i1++ ) {
172+
for ( i0 = 0; i0 < S0; i0++ ) {
173+
if ( predicate.call( thisArg, get( xbuf, ix ), take( [ i6, i5, i4, i3, i2, i1, i0 ], idx ), x.ref ) ) { // eslint-disable-line max-len
174+
return get( xbuf, ix );
175+
}
176+
ix += dx0;
177+
}
178+
ix += dx1;
179+
}
180+
ix += dx2;
181+
}
182+
ix += dx3;
183+
}
184+
ix += dx4;
185+
}
186+
ix += dx5;
187+
}
188+
ix += dx6;
189+
}
190+
return sentinelValue;
191+
}
192+
193+
194+
// EXPORTS //
195+
196+
module.exports = find7d;

0 commit comments

Comments
 (0)