Skip to content

Commit eecb8c9

Browse files
committed
feat: add 10d 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 2abe234 commit eecb8c9

File tree

4 files changed

+1086
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)