Skip to content

Commit f092715

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

File tree

4 files changed

+730
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)