Skip to content

Commit 142e477

Browse files
committed
feat: add factory function
--- 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 a752e25 commit 142e477

File tree

1 file changed

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

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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 isFunction = require( '@stdlib/assert/is-function' );
24+
var format = require( '@stdlib/string/format' );
25+
var reduce = require( './main.js' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Return a function for performing a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function and assigning results to a provided output ndarray.
32+
*
33+
* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
34+
* @throws {TypeError} first argument must be a function
35+
* @returns {Function} function for performing a reduction
36+
*
37+
* @example
38+
* var Float64Array = require( '@stdlib/array/float64' );
39+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
40+
* var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
41+
* var structFactory = require( '@stdlib/array/struct-factory' );
42+
* var base = require( '@stdlib/stats/base/ndarray/ztest' );
43+
*
44+
* var ResultsArray = structFactory( Float64Results );
45+
*
46+
* // Create data buffers:
47+
* 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 ] );
48+
* var ybuf = new ResultsArray( 3 );
49+
*
50+
* // Define the array shapes:
51+
* var xsh = [ 1, 3, 2, 2 ];
52+
* var ysh = [ 1, 3 ];
53+
*
54+
* // Define the array strides:
55+
* var sx = [ 12, 4, 2, 1 ];
56+
* var sy = [ 3, 1 ];
57+
*
58+
* // Define the index offsets:
59+
* var ox = 0;
60+
* var oy = 0;
61+
*
62+
* // Create an input ndarray-like object:
63+
* var x = {
64+
* 'dtype': 'float64',
65+
* 'data': xbuf,
66+
* 'shape': xsh,
67+
* 'strides': sx,
68+
* 'offset': ox,
69+
* 'order': 'row-major'
70+
* };
71+
*
72+
* // Create an output ndarray-like object:
73+
* var y = {
74+
* 'dtype': Float64Results,
75+
* 'data': ybuf,
76+
* 'shape': ysh,
77+
* 'strides': sy,
78+
* 'offset': oy,
79+
* 'order': 'row-major'
80+
* };
81+
*
82+
* // Create additional parameter ndarray-like objects:
83+
* var alternative = {
84+
* 'dtype': 'generic',
85+
* 'data': [ 'two-sided' ],
86+
* 'shape': ysh,
87+
* 'strides': [ 0, 0 ],
88+
* 'offset': 0,
89+
* 'order': 'row-major'
90+
};
91+
* var alpha = {
92+
* 'dtype': 'float64',
93+
* 'data': [ 0.05 ],
94+
* 'shape': ysh,
95+
* 'strides': [ 0, 0 ],
96+
* 'offset': 0,
97+
* 'order': 'row-major'
98+
};
99+
* var mu = {
100+
* 'dtype': 'float64',
101+
* 'data': [ 0.0 ],
102+
* 'shape': ysh,
103+
* 'strides': [ 0, 0 ],
104+
* 'offset': 0,
105+
* 'order': 'row-major'
106+
};
107+
* var sigma = {
108+
* 'dtype': 'float64',
109+
* 'data': [ 1.0 ],
110+
* 'shape': ysh,
111+
* 'strides': [ 0, 0 ],
112+
* 'offset': 0,
113+
* 'order': 'row-major'
114+
* };
115+
*
116+
* // Create a function for performing a reduction over subarrays:
117+
* var ztest = factory( base );
118+
* // returns <Function>
119+
*
120+
* // Perform a reduction:
121+
* ztest( [ x, y, alternative, alpha, mu, sigma ], [ 2, 3 ] );
122+
*
123+
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
124+
* // returns [ [ <Float64Results>, <Float64Results>, <Float64Results> ] ]
125+
*/
126+
function factory( fcn ) {
127+
if ( !isFunction( fcn ) ) {
128+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
129+
}
130+
return reducer;
131+
132+
/**
133+
* Performs a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function and assigns results to a provided output ndarray.
134+
*
135+
* @private
136+
* @param {ArrayLikeObject<Object>} arrays - array-like object containing ndarrays
137+
* @param {IntegerArray} dims - list of dimensions over which to perform a reduction
138+
* @param {Options} [options] - function options
139+
* @returns {void}
140+
*/
141+
function reducer( arrays, dims, options ) {
142+
var opts;
143+
if ( arguments.length > 2 ) {
144+
opts = options;
145+
} else {
146+
opts = {};
147+
}
148+
return reduce( fcn, arrays, dims, opts );
149+
}
150+
}
151+
152+
153+
// EXPORTS //
154+
155+
module.exports = factory;

0 commit comments

Comments
 (0)