Skip to content

Commit b32596a

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 f092715 commit b32596a

File tree

4 files changed

+792
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)