|
| 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 | +// MAIN // |
| 22 | + |
| 23 | +/** |
| 24 | +* Performs a reduction over a list of specified dimensions in an input ndarray and assigns results to a provided output ndarray. |
| 25 | +* |
| 26 | +* @private |
| 27 | +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function |
| 28 | +* @param {Array<Object>} arrays - ndarrays |
| 29 | +* @param {Function} strategy - input ndarray reshape strategy |
| 30 | +* @param {Options} opts - function options |
| 31 | +* @returns {void} |
| 32 | +* |
| 33 | +* @example |
| 34 | +* var Float64Array = require( '@stdlib/array/float64' ); |
| 35 | +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 36 | +* var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' ); |
| 37 | +* var structFactory = require( '@stdlib/array/struct-factory' ); |
| 38 | +* var ztest = require( '@stdlib/stats/base/ndarray/ztest' ); |
| 39 | +* |
| 40 | +* var ResultsArray = structFactory( Float64Results ); |
| 41 | +* |
| 42 | +* // Create data buffers: |
| 43 | +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 44 | +* var ybuf = new ResultsArray( 1 ); |
| 45 | +* |
| 46 | +* // Define the array shapes: |
| 47 | +* var xsh = [ 2, 2 ]; |
| 48 | +* var ysh = []; |
| 49 | +* |
| 50 | +* // Define the array strides: |
| 51 | +* var sx = [ 2, 1 ]; |
| 52 | +* var sy = [ 0 ]; |
| 53 | +* |
| 54 | +* // Define the index offsets: |
| 55 | +* var ox = 0; |
| 56 | +* var oy = 0; |
| 57 | +* |
| 58 | +* // Create an input ndarray-like object: |
| 59 | +* var x = { |
| 60 | +* 'dtype': 'float64', |
| 61 | +* 'data': xbuf, |
| 62 | +* 'shape': xsh, |
| 63 | +* 'strides': sx, |
| 64 | +* 'offset': ox, |
| 65 | +* 'order': 'row-major' |
| 66 | +* }; |
| 67 | +* |
| 68 | +* // Create an output ndarray-like object: |
| 69 | +* var y = { |
| 70 | +* 'dtype': Float64Results, |
| 71 | +* 'data': ybuf, |
| 72 | +* 'shape': ysh, |
| 73 | +* 'strides': sy, |
| 74 | +* 'offset': oy, |
| 75 | +* 'order': 'row-major' |
| 76 | +* }; |
| 77 | +* |
| 78 | +* // Create additional parameter ndarray-like objects: |
| 79 | +* var alternative = { |
| 80 | +* 'dtype': 'generic', |
| 81 | +* 'data': [ 'two-sided' ], |
| 82 | +* 'shape': ysh, |
| 83 | +* 'strides': [ 0 ], |
| 84 | +* 'offset': 0, |
| 85 | +* 'order': 'row-major' |
| 86 | +}; |
| 87 | +* var alpha = { |
| 88 | +* 'dtype': 'float64', |
| 89 | +* 'data': [ 0.05 ], |
| 90 | +* 'shape': ysh, |
| 91 | +* 'strides': [ 0 ], |
| 92 | +* 'offset': 0, |
| 93 | +* 'order': 'row-major' |
| 94 | +}; |
| 95 | +* var mu = { |
| 96 | +* 'dtype': 'float64', |
| 97 | +* 'data': [ 0.0 ], |
| 98 | +* 'shape': ysh, |
| 99 | +* 'strides': [ 0 ], |
| 100 | +* 'offset': 0, |
| 101 | +* 'order': 'row-major' |
| 102 | +}; |
| 103 | +* var sigma = { |
| 104 | +* 'dtype': 'float64', |
| 105 | +* 'data': [ 1.0 ], |
| 106 | +* 'shape': ysh, |
| 107 | +* 'strides': [ 0 ], |
| 108 | +* 'offset': 0, |
| 109 | +* 'order': 'row-major' |
| 110 | +* }; |
| 111 | +* |
| 112 | +* // Define a reshape strategy: |
| 113 | +* function strategy( x ) { |
| 114 | +* return { |
| 115 | +* 'dtype': x.dtype, |
| 116 | +* 'data': x.data, |
| 117 | +* 'shape': [ 4 ], |
| 118 | +* 'strides': [ 1 ], |
| 119 | +* 'offset': x.offset, |
| 120 | +* 'order': x.order |
| 121 | +* }; |
| 122 | +* } |
| 123 | +* |
| 124 | +* // Perform a reduction: |
| 125 | +* unary0d( ztest, [ x, y, alternative, alpha, mu, sigma ], strategy, {} ); |
| 126 | +* |
| 127 | +* var v = y.data.get( 0 ); |
| 128 | +* // returns <Float64Results> |
| 129 | +*/ |
| 130 | +function unary0d( fcn, arrays, strategy, opts ) { |
| 131 | + arrays[ 0 ] = strategy( arrays[ 0 ] ); |
| 132 | + fcn( arrays, opts ); |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +// EXPORTS // |
| 137 | + |
| 138 | +module.exports = unary0d; |
0 commit comments