Skip to content

Commit 6f4f47e

Browse files
committed
feat: add 3d 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 e152a98 commit 6f4f47e

File tree

1 file changed

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

1 file changed

+268
-0
lines changed
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
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, 3, 2, 2 ];
59+
* var ysh = [ 1, 1, 3 ];
60+
*
61+
* // Define the array strides:
62+
* var sx = [ 12, 12, 4, 2, 1 ];
63+
* var sy = [ 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 ],
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 ],
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 ],
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 ],
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+
* unary3d( ztest, [ x, y, alternative, alpha, mu, sigma ], views, [ 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 unary3d( fcn, arrays, views, strides, isRowMajor, strategy, opts ) {
194+
var dv0;
195+
var dv1;
196+
var dv2;
197+
var sh;
198+
var S0;
199+
var S1;
200+
var S2;
201+
var sv;
202+
var iv;
203+
var i0;
204+
var i1;
205+
var i2;
206+
var v;
207+
var i;
208+
209+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
210+
211+
// Resolve the output shape:
212+
sh = arrays[ 1 ].shape;
213+
214+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
215+
if ( isRowMajor ) {
216+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
217+
S0 = sh[ 2 ];
218+
S1 = sh[ 1 ];
219+
S2 = sh[ 0 ];
220+
dv0 = [ strides[2] ]; // offset increment for innermost loop
221+
dv1 = [ strides[1] - ( S0*strides[2] ) ];
222+
dv2 = [ strides[0] - ( S1*strides[1] ) ]; // offset increment for outermost loop
223+
for ( i = 1; i < arrays.length; i++ ) {
224+
sv = arrays[ i ].strides;
225+
dv0.push( sv[2] );
226+
dv1.push( sv[1] - ( S0*sv[2] ) );
227+
dv2.push( sv[0] - ( S1*sv[1] ) );
228+
}
229+
} else { // order === 'column-major'
230+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
231+
S0 = sh[ 0 ];
232+
S1 = sh[ 1 ];
233+
S2 = sh[ 2 ];
234+
dv0 = [ strides[0] ]; // offset increment for innermost loop
235+
dv1 = [ strides[1] - ( S0*strides[0] ) ];
236+
dv2 = [ strides[2] - ( S1*strides[1] ) ]; // offset increment for outermost loop
237+
for ( i = 1; i < arrays.length; i++ ) {
238+
sv = arrays[ i ].strides;
239+
dv0.push( sv[0] );
240+
dv1.push( sv[1] - ( S0*sv[0] ) );
241+
dv2.push( sv[2] - ( S1*sv[1] ) );
242+
}
243+
}
244+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
245+
iv = offsets( arrays );
246+
247+
// 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:
248+
v = copyIndexed( views );
249+
250+
// Iterate over the non-reduced ndarray dimensions...
251+
for ( i2 = 0; i2 < S2; i2++ ) {
252+
for ( i1 = 0; i1 < S1; i1++ ) {
253+
for ( i0 = 0; i0 < S0; i0++ ) {
254+
setViewOffsets( views, iv );
255+
v[ 0 ] = strategy( views[ 0 ] );
256+
fcn( v, opts );
257+
incrementOffsets( iv, dv0 );
258+
}
259+
incrementOffsets( iv, dv1 );
260+
}
261+
incrementOffsets( iv, dv2 );
262+
}
263+
}
264+
265+
266+
// EXPORTS //
267+
268+
module.exports = unary3d;

0 commit comments

Comments
 (0)