Skip to content

Commit c401a2a

Browse files
committed
feat: add 6d kernel
--- 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 2b3907f commit c401a2a

File tree

1 file changed

+306
-0
lines changed
  • lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-to-struct/lib

1 file changed

+306
-0
lines changed
Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
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 copyIndexed = require( '@stdlib/array/base/copy-indexed' );
26+
var incrementOffsets = require( './increment_offsets.js' );
27+
var setViewOffsets = require( './set_view_offsets.js' );
28+
var offsets = require( './offsets.js' );
29+
30+
31+
// MAIN //
32+
33+
/**
34+
* Performs a reduction over an input ndarray and assigns results to a provided output ndarray.
35+
*
36+
* @private
37+
* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
38+
* @param {Array<Object>} arrays - ndarrays
39+
* @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
40+
* @param {IntegerArray} strides - loop dimension strides for the input ndarray
41+
* @param {boolean} isRowMajor - boolean indicating if provided arrays are in row-major order
42+
* @param {Function} strategy - input ndarray reshape strategy
43+
* @param {Options} opts - function options
44+
* @returns {void}
45+
*
46+
* @example
47+
* var Float64Array = require( '@stdlib/array/float64' );
48+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
49+
* var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
50+
* var structFactory = require( '@stdlib/array/struct-factory' );
51+
* var ztest = require( '@stdlib/stats/base/ndarray/ztest' );
52+
*
53+
* var ResultsArray = structFactory( Float64Results );
54+
*
55+
* // Create data buffers:
56+
* 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 ] );
57+
* var ybuf = new ResultsArray( 3 );
58+
*
59+
* // Define the array shapes:
60+
* var xsh = [ 1, 1, 1, 1, 1, 3, 2, 2 ];
61+
* var ysh = [ 1, 1, 1, 1, 1, 3 ];
62+
*
63+
* // Define the array strides:
64+
* var sx = [ 12, 12, 12, 12, 12, 4, 2, 1 ];
65+
* var sy = [ 3, 3, 3, 3, 3, 1 ];
66+
*
67+
* // Define the index offsets:
68+
* var ox = 0;
69+
* var oy = 0;
70+
*
71+
* // Create an input ndarray-like object:
72+
* var x = {
73+
* 'dtype': 'float64',
74+
* 'data': xbuf,
75+
* 'shape': xsh,
76+
* 'strides': sx,
77+
* 'offset': ox,
78+
* 'order': 'row-major'
79+
* };
80+
*
81+
* // Create an output ndarray-like object:
82+
* var y = {
83+
* 'dtype': Float64Results,
84+
* 'data': ybuf,
85+
* 'shape': ysh,
86+
* 'strides': sy,
87+
* 'offset': oy,
88+
* 'order': 'row-major'
89+
* };
90+
*
91+
* // Create additional parameter ndarray-like objects:
92+
* var alternative = {
93+
* 'dtype': 'generic',
94+
* 'data': [ 'two-sided' ],
95+
* 'shape': ysh,
96+
* 'strides': [ 0, 0, 0, 0, 0, 0 ],
97+
* 'offset': 0,
98+
* 'order': 'row-major'
99+
};
100+
* var alpha = {
101+
* 'dtype': 'float64',
102+
* 'data': [ 0.05 ],
103+
* 'shape': ysh,
104+
* 'strides': [ 0, 0, 0, 0, 0, 0 ],
105+
* 'offset': 0,
106+
* 'order': 'row-major'
107+
};
108+
* var mu = {
109+
* 'dtype': 'float64',
110+
* 'data': [ 0.0 ],
111+
* 'shape': ysh,
112+
* 'strides': [ 0, 0, 0, 0, 0, 0 ],
113+
* 'offset': 0,
114+
* 'order': 'row-major'
115+
};
116+
* var sigma = {
117+
* 'dtype': 'float64',
118+
* 'data': [ 1.0 ],
119+
* 'shape': ysh,
120+
* 'strides': [ 0, 0, 0, 0, 0, 0 ],
121+
* 'offset': 0,
122+
* 'order': 'row-major'
123+
* };
124+
*
125+
* // Initialize ndarray-like objects representing sub-array views:
126+
* var views = [
127+
* {
128+
* 'dtype': x.dtype,
129+
* 'data': x.data,
130+
* 'shape': [ 2, 2 ],
131+
* 'strides': [ 2, 1 ],
132+
* 'offset': x.offset,
133+
* 'order': x.order
134+
* },
135+
* {
136+
* 'dtype': y.dtype,
137+
* 'data': y.data,
138+
* 'shape': [],
139+
* 'strides': [ 0 ],
140+
* 'offset': y.offset,
141+
* 'order': y.order
142+
* },
143+
* {
144+
* 'dtype': alternative.dtype,
145+
* 'data': alternative.data,
146+
* 'shape': [],
147+
* 'strides': [ 0 ],
148+
* 'offset': alternative.offset,
149+
* 'order': alternative.order
150+
* },
151+
* {
152+
* 'dtype': alpha.dtype,
153+
* 'data': alpha.data,
154+
* 'shape': [],
155+
* 'strides': [ 0 ],
156+
* 'offset': alpha.offset,
157+
* 'order': alpha.order
158+
* },
159+
* {
160+
* 'dtype': mu.dtype,
161+
* 'data': mu.data,
162+
* 'shape': [],
163+
* 'strides': [ 0 ],
164+
* 'offset': mu.offset,
165+
* 'order': mu.order
166+
* },
167+
* {
168+
* 'dtype': sigma.dtype,
169+
* 'data': sigma.data,
170+
* 'shape': [],
171+
* 'strides': [ 0 ],
172+
* 'offset': sigma.offset,
173+
* 'order': sigma.order
174+
* }
175+
* ];
176+
*
177+
* // Define a reshape strategy:
178+
* function strategy( x ) {
179+
* return {
180+
* 'dtype': x.dtype,
181+
* 'data': x.data,
182+
* 'shape': [ 4 ],
183+
* 'strides': [ 1 ],
184+
* 'offset': x.offset,
185+
* 'order': x.order
186+
* };
187+
* }
188+
*
189+
* // Perform a reduction:
190+
* unary6d( ztest, [ x, y, alternative, alpha, mu, sigma ], views, [ 12, 12, 12, 12, 12, 4 ], true, strategy, {} );
191+
*
192+
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
193+
* // returns [ [ [ [ [ [ <Float64Results>, <Float64Results>, <Float64Results> ] ] ] ] ] ]
194+
*/
195+
function unary6d( fcn, arrays, views, strides, isRowMajor, strategy, opts ) {
196+
var dv0;
197+
var dv1;
198+
var dv2;
199+
var dv3;
200+
var dv4;
201+
var dv5;
202+
var sh;
203+
var S0;
204+
var S1;
205+
var S2;
206+
var S3;
207+
var S4;
208+
var S5;
209+
var sv;
210+
var iv;
211+
var i0;
212+
var i1;
213+
var i2;
214+
var i3;
215+
var i4;
216+
var i5;
217+
var v;
218+
var i;
219+
220+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
221+
222+
// Resolve the output ndarray and associated shape:
223+
sh = arrays[ 1 ].shape;
224+
225+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
226+
if ( isRowMajor ) {
227+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
228+
S0 = sh[ 5 ];
229+
S1 = sh[ 4 ];
230+
S2 = sh[ 3 ];
231+
S3 = sh[ 2 ];
232+
S4 = sh[ 1 ];
233+
S5 = sh[ 0 ];
234+
dv0 = [ strides[5] ]; // offset increment for innermost loop
235+
dv1 = [ strides[4] - ( S0*strides[5] ) ];
236+
dv2 = [ strides[3] - ( S1*strides[4] ) ];
237+
dv3 = [ strides[2] - ( S2*strides[3] ) ];
238+
dv4 = [ strides[1] - ( S3*strides[2] ) ];
239+
dv5 = [ strides[0] - ( S4*strides[1] ) ]; // offset increment for outermost loop
240+
for ( i = 1; i < arrays.length; i++ ) {
241+
sv = arrays[ i ].strides;
242+
dv0.push( sv[5] );
243+
dv1.push( sv[4] - ( S0*sv[5] ) );
244+
dv2.push( sv[3] - ( S1*sv[4] ) );
245+
dv3.push( sv[2] - ( S2*sv[3] ) );
246+
dv4.push( sv[1] - ( S3*sv[2] ) );
247+
dv5.push( sv[0] - ( S4*sv[1] ) );
248+
}
249+
} else { // order === 'column-major'
250+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
251+
S0 = sh[ 0 ];
252+
S1 = sh[ 1 ];
253+
S2 = sh[ 2 ];
254+
S3 = sh[ 3 ];
255+
S4 = sh[ 4 ];
256+
S5 = sh[ 5 ];
257+
dv0 = [ strides[0] ]; // offset increment for innermost loop
258+
dv1 = [ strides[1] - ( S0*strides[0] ) ];
259+
dv2 = [ strides[2] - ( S1*strides[1] ) ];
260+
dv3 = [ strides[3] - ( S2*strides[2] ) ];
261+
dv4 = [ strides[4] - ( S3*strides[3] ) ];
262+
dv5 = [ strides[5] - ( S4*strides[4] ) ]; // offset increment for outermost loop
263+
for ( i = 1; i < arrays.length; i++ ) {
264+
sv = arrays[ i ].strides;
265+
dv0.push( sv[0] );
266+
dv1.push( sv[1] - ( S0*sv[0] ) );
267+
dv2.push( sv[2] - ( S1*sv[1] ) );
268+
dv3.push( sv[3] - ( S2*sv[2] ) );
269+
dv4.push( sv[4] - ( S3*sv[3] ) );
270+
dv5.push( sv[5] - ( S4*sv[4] ) );
271+
}
272+
}
273+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
274+
iv = offsets( arrays );
275+
276+
// Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views:
277+
v = copyIndexed( views );
278+
279+
// Iterate over the non-reduced ndarray dimensions...
280+
for ( i5 = 0; i5 < S5; i5++ ) {
281+
for ( i4 = 0; i4 < S4; i4++ ) {
282+
for ( i3 = 0; i3 < S3; i3++ ) {
283+
for ( i2 = 0; i2 < S2; i2++ ) {
284+
for ( i1 = 0; i1 < S1; i1++ ) {
285+
for ( i0 = 0; i0 < S0; i0++ ) {
286+
setViewOffsets( views, iv );
287+
v[ 0 ] = strategy( views[ 0 ] );
288+
fcn( v, opts );
289+
incrementOffsets( iv, dv0 );
290+
}
291+
incrementOffsets( iv, dv1 );
292+
}
293+
incrementOffsets( iv, dv2 );
294+
}
295+
incrementOffsets( iv, dv3 );
296+
}
297+
incrementOffsets( iv, dv4 );
298+
}
299+
incrementOffsets( iv, dv5 );
300+
}
301+
}
302+
303+
304+
// EXPORTS //
305+
306+
module.exports = unary6d;

0 commit comments

Comments
 (0)