Skip to content

Commit 4e16eb2

Browse files
committed
feat: add 5d 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 266d60a commit 4e16eb2

File tree

1 file changed

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

1 file changed

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

0 commit comments

Comments
 (0)