Skip to content

Commit 9b65ecc

Browse files
committed
feat: add 6d 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 4f0a8df commit 9b65ecc

File tree

4 files changed

+882
-0
lines changed

4 files changed

+882
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
* Tests whether at least `n` elements in an ndarray pass 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 {PositiveInteger} n - number of elements
46+
* @param {Function} predicate - predicate function
47+
* @param {*} thisArg - predicate function execution context
48+
* @returns {boolean} boolean indicating whether at least `n` elements pass a test
49+
*
50+
* @example
51+
* var Float64Array = require( '@stdlib/array/float64' );
52+
*
53+
* function predicate( value ) {
54+
* return value > 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, 3, 1, 2 ];
62+
*
63+
* // Define the array strides:
64+
* var sx = [ 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 = some6d( x, 4, predicate );
82+
* // returns true
83+
*/
84+
function some6d( x, n, predicate, thisArg ) {
85+
var count;
86+
var xbuf;
87+
var idx;
88+
var dx0;
89+
var dx1;
90+
var dx2;
91+
var dx3;
92+
var dx4;
93+
var dx5;
94+
var sh;
95+
var S0;
96+
var S1;
97+
var S2;
98+
var S3;
99+
var S4;
100+
var S5;
101+
var sx;
102+
var ix;
103+
var i0;
104+
var i1;
105+
var i2;
106+
var i3;
107+
var i4;
108+
var i5;
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[ 5 ];
119+
S1 = sh[ 4 ];
120+
S2 = sh[ 3 ];
121+
S3 = sh[ 2 ];
122+
S4 = sh[ 1 ];
123+
S5 = sh[ 0 ];
124+
dx0 = sx[ 5 ]; // offset increment for innermost loop
125+
dx1 = sx[ 4 ] - ( S0*sx[5] );
126+
dx2 = sx[ 3 ] - ( S1*sx[4] );
127+
dx3 = sx[ 2 ] - ( S2*sx[3] );
128+
dx4 = sx[ 1 ] - ( S3*sx[2] );
129+
dx5 = sx[ 0 ] - ( S4*sx[1] ); // offset increment for outermost loop
130+
} else { // order === 'column-major'
131+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
132+
S0 = sh[ 0 ];
133+
S1 = sh[ 1 ];
134+
S2 = sh[ 2 ];
135+
S3 = sh[ 3 ];
136+
S4 = sh[ 4 ];
137+
S5 = sh[ 5 ];
138+
dx0 = sx[ 0 ]; // offset increment for innermost loop
139+
dx1 = sx[ 1 ] - ( S0*sx[0] );
140+
dx2 = sx[ 2 ] - ( S1*sx[1] );
141+
dx3 = sx[ 3 ] - ( S2*sx[2] );
142+
dx4 = sx[ 4 ] - ( S3*sx[3] );
143+
dx5 = sx[ 5 ] - ( S4*sx[4] ); // offset increment for outermost loop
144+
idx = reverse( idx );
145+
}
146+
// Set a pointer to the first indexed element:
147+
ix = x.offset;
148+
149+
// Cache a reference to the input ndarray buffer:
150+
xbuf = x.data;
151+
152+
// Initialize a counter:
153+
count = 0;
154+
155+
// Iterate over the ndarray dimensions...
156+
for ( i5 = 0; i5 < S5; i5++ ) {
157+
for ( i4 = 0; i4 < S4; i4++ ) {
158+
for ( i3 = 0; i3 < S3; i3++ ) {
159+
for ( i2 = 0; i2 < S2; i2++ ) {
160+
for ( i1 = 0; i1 < S1; i1++ ) {
161+
for ( i0 = 0; i0 < S0; i0++ ) {
162+
if ( predicate.call( thisArg, xbuf[ ix ], take( [ i5, i4, i3, i2, i1, i0 ], idx ), x.ref ) ) { // eslint-disable-line max-len
163+
count += 1;
164+
if ( count === n ) {
165+
return true;
166+
}
167+
}
168+
ix += dx0;
169+
}
170+
ix += dx1;
171+
}
172+
ix += dx2;
173+
}
174+
ix += dx3;
175+
}
176+
ix += dx4;
177+
}
178+
ix += dx5;
179+
}
180+
return false;
181+
}
182+
183+
184+
// EXPORTS //
185+
186+
module.exports = some6d;
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
* Tests whether at least `n` elements in an ndarray pass 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 {PositiveInteger} n - number of elements
46+
* @param {Function} predicate - predicate function
47+
* @param {*} thisArg - predicate function execution context
48+
* @returns {boolean} boolean indicating whether at least `n` elements pass a test
49+
*
50+
* @example
51+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
52+
* var accessors = require( '@stdlib/array/base/accessors' );
53+
* var Float64Array = require( '@stdlib/array/float64' );
54+
*
55+
* function predicate( value ) {
56+
* return value > 0.0;
57+
* }
58+
*
59+
* // Create a data buffer:
60+
* var xbuf = toAccessorArray( 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 ] ) );
61+
*
62+
* // Define the shape of the input array:
63+
* var shape = [ 1, 1, 1, 3, 1, 2 ];
64+
*
65+
* // Define the array strides:
66+
* var sx = [ 12, 12, 12, 4, 4, 1 ];
67+
*
68+
* // Define the index offset:
69+
* var ox = 1;
70+
*
71+
* // Create the input ndarray-like object:
72+
* var x = {
73+
* 'ref': null,
74+
* 'dtype': 'float64',
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 = some6d( x, 4, predicate );
85+
* // returns true
86+
*/
87+
function some6d( x, n, predicate, thisArg ) {
88+
var count;
89+
var xbuf;
90+
var idx;
91+
var get;
92+
var dx0;
93+
var dx1;
94+
var dx2;
95+
var dx3;
96+
var dx4;
97+
var dx5;
98+
var sh;
99+
var S0;
100+
var S1;
101+
var S2;
102+
var S3;
103+
var S4;
104+
var S5;
105+
var sx;
106+
var ix;
107+
var i0;
108+
var i1;
109+
var i2;
110+
var i3;
111+
var i4;
112+
var i5;
113+
114+
// Note on variable naming convention: S#, dx#, i# where # corresponds to the loop number, with `0` being the innermost loop...
115+
116+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
117+
sh = x.shape;
118+
sx = x.strides;
119+
idx = zeroTo( sh.length );
120+
if ( strides2order( sx ) === 1 ) {
121+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
122+
S0 = sh[ 5 ];
123+
S1 = sh[ 4 ];
124+
S2 = sh[ 3 ];
125+
S3 = sh[ 2 ];
126+
S4 = sh[ 1 ];
127+
S5 = sh[ 0 ];
128+
dx0 = sx[ 5 ]; // offset increment for innermost loop
129+
dx1 = sx[ 4 ] - ( S0*sx[5] );
130+
dx2 = sx[ 3 ] - ( S1*sx[4] );
131+
dx3 = sx[ 2 ] - ( S2*sx[3] );
132+
dx4 = sx[ 1 ] - ( S3*sx[2] );
133+
dx5 = sx[ 0 ] - ( S4*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+
dx0 = sx[ 0 ]; // offset increment for innermost loop
143+
dx1 = sx[ 1 ] - ( S0*sx[0] );
144+
dx2 = sx[ 2 ] - ( S1*sx[1] );
145+
dx3 = sx[ 3 ] - ( S2*sx[2] );
146+
dx4 = sx[ 4 ] - ( S3*sx[3] );
147+
dx5 = sx[ 5 ] - ( S4*sx[4] ); // offset increment for outermost loop
148+
idx = reverse( idx );
149+
}
150+
// Set a pointer to the first indexed element:
151+
ix = x.offset;
152+
153+
// Cache a reference to the input ndarray buffer:
154+
xbuf = x.data;
155+
156+
// Cache accessor:
157+
get = x.accessors[ 0 ];
158+
159+
// Initialize a counter:
160+
count = 0;
161+
162+
// Iterate over the ndarray dimensions...
163+
for ( i5 = 0; i5 < S5; i5++ ) {
164+
for ( i4 = 0; i4 < S4; i4++ ) {
165+
for ( i3 = 0; i3 < S3; i3++ ) {
166+
for ( i2 = 0; i2 < S2; i2++ ) {
167+
for ( i1 = 0; i1 < S1; i1++ ) {
168+
for ( i0 = 0; i0 < S0; i0++ ) {
169+
if ( predicate.call( thisArg, get( xbuf, ix ), take( [ i5, i4, i3, i2, i1, i0 ], idx ), x.ref ) ) { // eslint-disable-line max-len
170+
count += 1;
171+
if ( count === n ) {
172+
return true;
173+
}
174+
}
175+
ix += dx0;
176+
}
177+
ix += dx1;
178+
}
179+
ix += dx2;
180+
}
181+
ix += dx3;
182+
}
183+
ix += dx4;
184+
}
185+
ix += dx5;
186+
}
187+
return false;
188+
}
189+
190+
191+
// EXPORTS //
192+
193+
module.exports = some6d;

0 commit comments

Comments
 (0)