Skip to content

Commit f65a6ef

Browse files
committed
feat: add 1d 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 c9b5f5e commit f65a6ef

File tree

1 file changed

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

1 file changed

+230
-0
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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 {Function} strategy - input ndarray reshape strategy
40+
* @param {Options} opts - function options
41+
* @returns {void}
42+
*
43+
* @example
44+
* var Float64Array = require( '@stdlib/array/float64' );
45+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
46+
* var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
47+
* var structFactory = require( '@stdlib/array/struct-factory' );
48+
* var ztest = require( '@stdlib/stats/base/ndarray/ztest' );
49+
*
50+
* var ResultsArray = structFactory( Float64Results );
51+
*
52+
* // Create data buffers:
53+
* 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 ] );
54+
* var ybuf = new ResultsArray( 3 );
55+
*
56+
* // Define the array shapes:
57+
* var xsh = [ 3, 2, 2 ];
58+
* var ysh = [ 3 ];
59+
*
60+
* // Define the array strides:
61+
* var sx = [ 4, 2, 1 ];
62+
* var sy = [ 1 ];
63+
*
64+
* // Define the index offsets:
65+
* var ox = 0;
66+
* var oy = 0;
67+
*
68+
* // Create an input ndarray-like object:
69+
* var x = {
70+
* 'dtype': 'float64',
71+
* 'data': xbuf,
72+
* 'shape': xsh,
73+
* 'strides': sx,
74+
* 'offset': ox,
75+
* 'order': 'row-major'
76+
* };
77+
*
78+
* // Create an output ndarray-like object:
79+
* var y = {
80+
* 'dtype': 'float64',
81+
* 'data': ybuf,
82+
* 'shape': ysh,
83+
* 'strides': sy,
84+
* 'offset': oy,
85+
* 'order': 'row-major'
86+
* };
87+
*
88+
* // Create additional parameter ndarray-like objects:
89+
* var alternative = {
90+
* 'dtype': 'generic',
91+
* 'data': [ 'two-sided' ],
92+
* 'shape': ysh,
93+
* 'strides': [ 0 ],
94+
* 'offset': 0,
95+
* 'order': 'row-major'
96+
};
97+
* var alpha = {
98+
* 'dtype': 'float64',
99+
* 'data': [ 0.05 ],
100+
* 'shape': ysh,
101+
* 'strides': [ 0 ],
102+
* 'offset': 0,
103+
* 'order': 'row-major'
104+
};
105+
* var mu = {
106+
* 'dtype': 'float64',
107+
* 'data': [ 0.0 ],
108+
* 'shape': ysh,
109+
* 'strides': [ 0 ],
110+
* 'offset': 0,
111+
* 'order': 'row-major'
112+
};
113+
* var sigma = {
114+
* 'dtype': 'float64',
115+
* 'data': [ 1.0 ],
116+
* 'shape': ysh,
117+
* 'strides': [ 0 ],
118+
* 'offset': 0,
119+
* 'order': 'row-major'
120+
* };
121+
*
122+
* // Initialize ndarray-like objects representing sub-array views:
123+
* var views = [
124+
* {
125+
* 'dtype': x.dtype,
126+
* 'data': x.data,
127+
* 'shape': [ 2, 2 ],
128+
* 'strides': [ 2, 1 ],
129+
* 'offset': x.offset,
130+
* 'order': x.order
131+
* },
132+
* {
133+
* 'dtype': y.dtype,
134+
* 'data': y.data,
135+
* 'shape': [],
136+
* 'strides': [ 0 ],
137+
* 'offset': y.offset,
138+
* 'order': y.order
139+
* },
140+
* {
141+
* 'dtype': alternative.dtype,
142+
* 'data': alternative.data,
143+
* 'shape': [],
144+
* 'strides': [ 0 ],
145+
* 'offset': alternative.offset,
146+
* 'order': alternative.order
147+
* },
148+
* {
149+
* 'dtype': alpha.dtype,
150+
* 'data': alpha.data,
151+
* 'shape': [],
152+
* 'strides': [ 0 ],
153+
* 'offset': alpha.offset,
154+
* 'order': alpha.order
155+
* },
156+
* {
157+
* 'dtype': mu.dtype,
158+
* 'data': mu.data,
159+
* 'shape': [],
160+
* 'strides': [ 0 ],
161+
* 'offset': mu.offset,
162+
* 'order': mu.order
163+
* },
164+
* {
165+
* 'dtype': sigma.dtype,
166+
* 'data': sigma.data,
167+
* 'shape': [],
168+
* 'strides': [ 0 ],
169+
* 'offset': sigma.offset,
170+
* 'order': sigma.order
171+
* }
172+
* ];
173+
*
174+
* // Define a reshape strategy:
175+
* function strategy( x ) {
176+
* return {
177+
* 'dtype': x.dtype,
178+
* 'data': x.data,
179+
* 'shape': [ 4 ],
180+
* 'strides': [ 1 ],
181+
* 'offset': x.offset,
182+
* 'order': x.order
183+
* };
184+
* }
185+
*
186+
* // Perform a reduction:
187+
* unary1d( ztest, [ x, y, alternative, alpha, mu, sigma ], views, [ 4 ], strategy, {} );
188+
*
189+
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
190+
* // returns [ <Float64Results>, <Float64Results>, <Float64Results> ]
191+
*/
192+
function unary1d( fcn, arrays, views, strides, strategy, opts ) {
193+
var dv0;
194+
var sh;
195+
var S0;
196+
var iv;
197+
var i0;
198+
var v;
199+
var i;
200+
201+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
202+
203+
// Resolve the output shape:
204+
sh = arrays[ 1 ].shape;
205+
206+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
207+
S0 = sh[ 0 ];
208+
dv0 = [ strides[0] ];
209+
for ( i = 1; i < arrays.length; i++ ) {
210+
dv0.push( arrays[i].strides[0] );
211+
}
212+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
213+
iv = offsets( arrays );
214+
215+
// 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:
216+
v = copyIndexed( views );
217+
218+
// Iterate over the non-reduced ndarray dimensions...
219+
for ( i0 = 0; i0 < S0; i0++ ) {
220+
setViewOffsets( views, iv );
221+
v[ 0 ] = strategy( views[ 0 ] );
222+
fcn( v, opts );
223+
incrementOffsets( iv, dv0 );
224+
}
225+
}
226+
227+
228+
// EXPORTS //
229+
230+
module.exports = unary1d;

0 commit comments

Comments
 (0)