Skip to content

Commit ad39d00

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 986f148 commit ad39d00

File tree

4 files changed

+892
-0
lines changed

4 files changed

+892
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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 incrementOffsets = require( './increment_offsets.js' );
25+
var setViewOffsets = require( './set_view_offsets.js' );
26+
var offsets = require( './offsets.js' );
27+
28+
29+
// MAIN //
30+
31+
/**
32+
* Performs a reduction over an input ndarray and assigns results to a provided output ndarray.
33+
*
34+
* @private
35+
* @param {Function} fcn - reduction function
36+
* @param {Array<Object>} arrays - ndarrays
37+
* @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
38+
* @param {IntegerArray} strides - loop dimension strides for the input ndarray
39+
* @param {Options} opts - function options
40+
* @returns {void}
41+
*
42+
* @example
43+
* var Float64Array = require( '@stdlib/array/float64' );
44+
* var filled = require( '@stdlib/array/base/filled' );
45+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
46+
* var base = require( '@stdlib/ndarray/base/every' );
47+
*
48+
* // Create data buffers:
49+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
50+
* var ybuf = filled( false, 3 );
51+
*
52+
* // Define the array shapes:
53+
* var xsh = [ 1, 1, 1, 3, 2, 2 ];
54+
* var ysh = [ 1, 1, 1, 3 ];
55+
*
56+
* // Define the array strides:
57+
* var sx = [ 12, 12, 12, 4, 2, 1 ];
58+
* var sy = [ 3, 3, 3, 1 ];
59+
*
60+
* // Define the index offsets:
61+
* var ox = 0;
62+
* var oy = 0;
63+
*
64+
* // Create an input ndarray-like object:
65+
* var x = {
66+
* 'dtype': 'float64',
67+
* 'data': xbuf,
68+
* 'shape': xsh,
69+
* 'strides': sx,
70+
* 'offset': ox,
71+
* 'order': 'row-major'
72+
* };
73+
*
74+
* // Create an output ndarray-like object:
75+
* var y = {
76+
* 'dtype': 'generic',
77+
* 'data': ybuf,
78+
* 'shape': ysh,
79+
* 'strides': sy,
80+
* 'offset': oy,
81+
* 'order': 'row-major'
82+
* };
83+
*
84+
* // Initialize ndarray-like objects representing sub-array views:
85+
* var views = [
86+
* {
87+
* 'dtype': x.dtype,
88+
* 'data': x.data,
89+
* 'shape': [ 2, 2 ],
90+
* 'strides': [ 2, 1 ],
91+
* 'offset': x.offset,
92+
* 'order': x.order
93+
* }
94+
* ];
95+
*
96+
* // Perform a reduction:
97+
* unary4d( base, [ x, y ], views, [ 12, 12, 12, 4 ], {} );
98+
*
99+
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
100+
* // returns [ [ [ [ true, false, true ] ] ] ]
101+
*/
102+
function unary4d( fcn, arrays, views, strides, opts ) {
103+
var ybuf;
104+
var dv0;
105+
var dv1;
106+
var dv2;
107+
var dv3;
108+
var sh;
109+
var S0;
110+
var S1;
111+
var S2;
112+
var S3;
113+
var sv;
114+
var iv;
115+
var i0;
116+
var i1;
117+
var i2;
118+
var i3;
119+
var y;
120+
var i;
121+
122+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
123+
124+
// Resolve the output ndarray and associated shape:
125+
y = arrays[ 1 ];
126+
sh = y.shape;
127+
128+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
129+
if ( isRowMajor( y.order ) ) {
130+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
131+
S0 = sh[ 3 ];
132+
S1 = sh[ 2 ];
133+
S2 = sh[ 1 ];
134+
S3 = sh[ 0 ];
135+
dv0 = [ strides[3] ]; // offset increment for innermost loop
136+
dv1 = [ strides[2] - ( S0*strides[3] ) ];
137+
dv2 = [ strides[1] - ( S1*strides[2] ) ];
138+
dv3 = [ strides[0] - ( S2*strides[1] ) ]; // offset increment for outermost loop
139+
for ( i = 1; i < arrays.length; i++ ) {
140+
sv = arrays[ i ].strides;
141+
dv0.push( sv[3] );
142+
dv1.push( sv[2] - ( S0*sv[3] ) );
143+
dv2.push( sv[1] - ( S1*sv[2] ) );
144+
dv3.push( sv[0] - ( S2*sv[1]) );
145+
}
146+
} else { // order === 'column-major'
147+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
148+
S0 = sh[ 0 ];
149+
S1 = sh[ 1 ];
150+
S2 = sh[ 2 ];
151+
S3 = sh[ 3 ];
152+
dv0 = [ strides[0] ]; // offset increment for innermost loop
153+
dv1 = [ strides[1] - ( S0*strides[0] ) ];
154+
dv2 = [ strides[2] - ( S1*strides[1] ) ];
155+
dv3 = [ strides[3] - ( S2*strides[2]) ]; // offset increment for outermost loop
156+
for ( i = 1; i < arrays.length; i++ ) {
157+
sv = arrays[ i ].strides;
158+
dv0.push( sv[0] );
159+
dv1.push( sv[1] - ( S0*sv[0] ) );
160+
dv2.push( sv[2] - ( S1*sv[1] ) );
161+
dv3.push( sv[3] - ( S2*sv[2] ) );
162+
}
163+
}
164+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
165+
iv = offsets( arrays );
166+
167+
// Cache a reference to the output ndarray buffer:
168+
ybuf = y.data;
169+
170+
// Iterate over the non-reduced ndarray dimensions...
171+
for ( i3 = 0; i3 < S3; i3++ ) {
172+
for ( i2 = 0; i2 < S2; i2++ ) {
173+
for ( i1 = 0; i1 < S1; i1++ ) {
174+
for ( i0 = 0; i0 < S0; i0++ ) {
175+
setViewOffsets( views, iv );
176+
ybuf[ iv[1] ] = fcn( views, opts );
177+
incrementOffsets( iv, dv0 );
178+
}
179+
incrementOffsets( iv, dv1 );
180+
}
181+
incrementOffsets( iv, dv2 );
182+
}
183+
incrementOffsets( iv, dv3 );
184+
}
185+
}
186+
187+
188+
// EXPORTS //
189+
190+
module.exports = unary4d;
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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 incrementOffsets = require( './increment_offsets.js' );
25+
var setViewOffsets = require( './set_view_offsets.js' );
26+
var offsets = require( './offsets.js' );
27+
28+
29+
// MAIN //
30+
31+
/**
32+
* Performs a reduction over an input ndarray and assigns results to a provided output ndarray.
33+
*
34+
* @private
35+
* @param {Function} fcn - reduction function
36+
* @param {Array<Object>} arrays - ndarrays
37+
* @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
38+
* @param {IntegerArray} strides - loop dimension strides for the input ndarray
39+
* @param {Options} opts - function options
40+
* @returns {void}
41+
*
42+
* @example
43+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
44+
* var accessors = require( '@stdlib/array/base/accessors' );
45+
* var Float64Array = require( '@stdlib/array/float64' );
46+
* var filled = require( '@stdlib/array/base/filled' );
47+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
48+
* var base = require( '@stdlib/ndarray/base/every' );
49+
*
50+
* // Create data buffers:
51+
* var xbuf = toAccessorArray( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ) );
52+
* var ybuf = toAccessorArray( filled( false, 3 ) );
53+
*
54+
* // Define the array shapes:
55+
* var xsh = [ 1, 1, 1, 3, 2, 2 ];
56+
* var ysh = [ 1, 1, 1, 3 ];
57+
*
58+
* // Define the array strides:
59+
* var sx = [ 12, 12, 12, 4, 2, 1 ];
60+
* var sy = [ 3, 3, 3, 1 ];
61+
*
62+
* // Define the index offsets:
63+
* var ox = 0;
64+
* var oy = 0;
65+
*
66+
* // Create an input ndarray-like object:
67+
* var x = {
68+
* 'dtype': 'float64',
69+
* 'data': xbuf,
70+
* 'shape': xsh,
71+
* 'strides': sx,
72+
* 'offset': ox,
73+
* 'order': 'row-major',
74+
* 'accessors': accessors( xbuf ).accessors
75+
* };
76+
*
77+
* // Create an output ndarray-like object:
78+
* var y = {
79+
* 'dtype': 'generic',
80+
* 'data': ybuf,
81+
* 'shape': ysh,
82+
* 'strides': sy,
83+
* 'offset': oy,
84+
* 'order': 'row-major',
85+
* 'accessors': accessors( ybuf ).accessors
86+
* };
87+
*
88+
* // Initialize ndarray-like objects representing sub-array views:
89+
* var views = [
90+
* {
91+
* 'dtype': x.dtype,
92+
* 'data': x.data,
93+
* 'shape': [ 2, 2 ],
94+
* 'strides': [ 2, 1 ],
95+
* 'offset': x.offset,
96+
* 'order': x.order
97+
* }
98+
* ];
99+
*
100+
* // Perform a reduction:
101+
* unary4d( base, [ x, y ], views, [ 12, 12, 12, 4 ], {} );
102+
*
103+
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
104+
* // returns [ [ [ [ true, false, true ] ] ] ]
105+
*/
106+
function unary4d( fcn, arrays, views, strides, opts ) {
107+
var ybuf;
108+
var set;
109+
var dv0;
110+
var dv1;
111+
var dv2;
112+
var dv3;
113+
var sh;
114+
var S0;
115+
var S1;
116+
var S2;
117+
var S3;
118+
var sv;
119+
var iv;
120+
var i0;
121+
var i1;
122+
var i2;
123+
var i3;
124+
var y;
125+
var i;
126+
127+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
128+
129+
// Resolve the output ndarray and associated shape:
130+
y = arrays[ 1 ];
131+
sh = y.shape;
132+
133+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
134+
if ( isRowMajor( y.order ) ) {
135+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
136+
S0 = sh[ 3 ];
137+
S1 = sh[ 2 ];
138+
S2 = sh[ 1 ];
139+
S3 = sh[ 0 ];
140+
dv0 = [ strides[3] ]; // offset increment for innermost loop
141+
dv1 = [ strides[2] - ( S0*strides[3] ) ];
142+
dv2 = [ strides[1] - ( S1*strides[2] ) ];
143+
dv3 = [ strides[0] - ( S2*strides[1] ) ]; // offset increment for outermost loop
144+
for ( i = 1; i < arrays.length; i++ ) {
145+
sv = arrays[ i ].strides;
146+
dv0.push( sv[3] );
147+
dv1.push( sv[2] - ( S0*sv[3] ) );
148+
dv2.push( sv[1] - ( S1*sv[2] ) );
149+
dv3.push( sv[0] - ( S2*sv[1]) );
150+
}
151+
} else { // order === 'column-major'
152+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
153+
S0 = sh[ 0 ];
154+
S1 = sh[ 1 ];
155+
S2 = sh[ 2 ];
156+
S3 = sh[ 3 ];
157+
dv0 = [ strides[0] ]; // offset increment for innermost loop
158+
dv1 = [ strides[1] - ( S0*strides[0] ) ];
159+
dv2 = [ strides[2] - ( S1*strides[1] ) ];
160+
dv3 = [ strides[3] - ( S2*strides[2]) ]; // offset increment for outermost loop
161+
for ( i = 1; i < arrays.length; i++ ) {
162+
sv = arrays[ i ].strides;
163+
dv0.push( sv[0] );
164+
dv1.push( sv[1] - ( S0*sv[0] ) );
165+
dv2.push( sv[2] - ( S1*sv[1] ) );
166+
dv3.push( sv[3] - ( S2*sv[2] ) );
167+
}
168+
}
169+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
170+
iv = offsets( arrays );
171+
172+
// Cache a reference to the output ndarray buffer:
173+
ybuf = y.data;
174+
175+
// Cache accessors:
176+
set = y.accessors[ 1 ];
177+
178+
// Iterate over the non-reduced ndarray dimensions...
179+
for ( i3 = 0; i3 < S3; i3++ ) {
180+
for ( i2 = 0; i2 < S2; i2++ ) {
181+
for ( i1 = 0; i1 < S1; i1++ ) {
182+
for ( i0 = 0; i0 < S0; i0++ ) {
183+
setViewOffsets( views, iv );
184+
set( ybuf, iv[ 1 ], fcn( views, opts ) );
185+
incrementOffsets( iv, dv0 );
186+
}
187+
incrementOffsets( iv, dv1 );
188+
}
189+
incrementOffsets( iv, dv2 );
190+
}
191+
incrementOffsets( iv, dv3 );
192+
}
193+
}
194+
195+
196+
// EXPORTS //
197+
198+
module.exports = unary4d;

0 commit comments

Comments
 (0)