Skip to content

Commit 34ed9af

Browse files
committed
feat: add ndarray/base/unary-reduce-strided1d-dispatch-by
--- 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: skipped - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 970b3be commit 34ed9af

File tree

15 files changed

+2859
-0
lines changed

15 files changed

+2859
-0
lines changed
Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
<!-- lint disable maximum-heading-length -->
22+
23+
# UnaryStrided1dDispatchBy
24+
25+
> Constructor for performing a reduction on an input ndarray according to a callback function.
26+
27+
<section class="usage">
28+
29+
## Usage
30+
31+
```javascript
32+
var UnaryStrided1dDispatchBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-by' );
33+
```
34+
35+
#### UnaryStrided1dDispatchBy( table, idtypes, odtypes, policies )
36+
37+
Constructor for performing a reduction on an input ndarray according to a callback function.
38+
39+
```javascript
40+
var base = require( '@stdlib/stats/base/ndarray/max-by' );
41+
42+
var table = {
43+
'default': base
44+
};
45+
46+
var dtypes = [ 'float64', 'float32', 'generic' ];
47+
var policies = {
48+
'output': 'same',
49+
'casting': 'none'
50+
};
51+
52+
var unary = new UnaryStrided1dDispatchBy( table, [ dtypes ], dtypes, policies );
53+
```
54+
55+
The constructor has the following parameters:
56+
57+
- **table**: strided reduction function dispatch table. Must have the following properties:
58+
59+
- **default**: default strided reduction function which should be invoked when provided ndarrays have data types which do not have a corresponding specialized implementation.
60+
61+
A dispatch table may have the following additional properties:
62+
63+
- **types**: one-dimensional list of ndarray data types describing specialized input ndarray argument signatures. Only the input ndarray argument data types should be specified. Output ndarray and additional input ndarray argument data types should be omitted and are not considered during dispatch. The length of `types` must equal the number of strided functions specified by `fcns` (i.e., for every input ndarray data type, there must be a corresponding strided reduction function in `fcns`).
64+
- **fcns**: list of strided reduction functions which are specific to specialized input ndarray argument signatures.
65+
66+
- **idtypes**: list containing lists of supported input data types for each input ndarray argument.
67+
68+
- **odtypes**: list of supported output data types.
69+
70+
- **policies**: dispatch policies. Must have the following properties:
71+
72+
- **output**: output data type [policy][@stdlib/ndarray/output-dtype-policies].
73+
- **casting**: input ndarray casting [policy][@stdlib/ndarray/input-casting-policies].
74+
75+
#### UnaryStrided1dDispatchBy.prototype.apply( x\[, ...args]\[, options], clbk\[, thisArg] )
76+
77+
Performs a reduction on a provided input ndarray according to a callback function.
78+
79+
```javascript
80+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
81+
var base = require( '@stdlib/stats/base/ndarray/max-by' );
82+
83+
var table = {
84+
'default': base
85+
};
86+
87+
var dtypes = [ 'float64', 'float32', 'generic' ];
88+
var policies = {
89+
'output': 'same',
90+
'casting': 'none'
91+
};
92+
93+
var unary = new UnaryStrided1dDispatchBy( table, [ dtypes ], dtypes, policies );
94+
95+
var xbuf = [ -1.0, 2.0, -3.0 ];
96+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
97+
98+
function clbk( v ) {
99+
return v * 2.0;
100+
}
101+
102+
var y = unary.apply( x, clbk );
103+
// returns <ndarray>
104+
105+
var v = y.get();
106+
// returns 4.0
107+
```
108+
109+
The method has the following parameters:
110+
111+
- **x**: input ndarray.
112+
- **...args**: additional input ndarray arguments (_optional_).
113+
- **options**: function options (_optional_).
114+
- **clbk**: callback function.
115+
- **thisArg**: callback function execution context (_optional_).
116+
117+
The method accepts the following options:
118+
119+
- **dims**: list of dimensions over which to perform a reduction.
120+
- **dtype**: output ndarray data type. Setting this option, overrides the output data type policy.
121+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions. Default: `false`.
122+
123+
By default, the method returns an ndarray having a data type determined by the output data type policy. To override the default behavior, set the `dtype` option.
124+
125+
```javascript
126+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
127+
var base = require( '@stdlib/stats/base/ndarray/max-by' );
128+
var getDType = require( '@stdlib/ndarray/dtype' );
129+
130+
var table = {
131+
'default': base
132+
};
133+
134+
var dtypes = [ 'float64', 'float32', 'generic' ];
135+
var policies = {
136+
'output': 'same',
137+
'casting': 'none'
138+
};
139+
140+
var unary = new UnaryStrided1dDispatchBy( table, [ dtypes ], dtypes, policies );
141+
142+
var xbuf = [ -1.0, 2.0, -3.0 ];
143+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
144+
145+
function clbk( v ) {
146+
return v * 2.0;
147+
}
148+
149+
var opts = {
150+
'dtype': 'float64'
151+
};
152+
var y = unary.apply( x, opts, clbk );
153+
// returns <ndarray>
154+
155+
var dt = getDType( y );
156+
// returns 'float64'
157+
```
158+
159+
#### UnaryStrided1dDispatchBy.prototype.assign( x\[, ...args], out\[, options], clbk\[, thisArg] )
160+
161+
Performs a reduction on a provided input ndarray according to a callback function and assigns results to a provided output ndarray.
162+
163+
```javascript
164+
var base = require( '@stdlib/stats/base/ndarray/max-by' );
165+
var dtypes = require( '@stdlib/ndarray/dtypes' );
166+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
167+
168+
var idt = dtypes();
169+
var odt = dtypes( 'real_and_generic' );
170+
var policies = {
171+
'output': 'same',
172+
'casting': 'none'
173+
};
174+
175+
var table = {
176+
'default': base
177+
};
178+
var unary = new UnaryStrided1dDispatchBy( table, [ idt ], odt, policies );
179+
180+
var xbuf = [ -1.0, 2.0, -3.0 ];
181+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
182+
183+
var ybuf = [ 0.0 ];
184+
var y = new ndarray( 'generic', ybuf, [], [ 0 ], 0, 'row-major' );
185+
186+
function clbk( v ) {
187+
return v * 2.0;
188+
}
189+
190+
var out = unary.assign( x, y, clbk );
191+
// returns <ndarray>
192+
193+
var v = out.get();
194+
// returns 4.0
195+
196+
var bool = ( out === y );
197+
// returns true
198+
```
199+
200+
The method has the following parameters:
201+
202+
- **x**: input ndarray.
203+
- **args**: additional input ndarray arguments (_optional_).
204+
- **out**: output ndarray.
205+
- **options**: function options (_optional_).
206+
- **clbk**: callback function.
207+
- **thisArg**: callback function execution context (_optional_).
208+
209+
The method accepts the following options:
210+
211+
- **dims**: list of dimensions over which to perform a reduction.
212+
213+
</section>
214+
215+
<!-- /.usage -->
216+
217+
<section class="notes">
218+
219+
## Notes
220+
221+
- A strided reduction function should have the following signature:
222+
223+
```text
224+
f( arrays, clbk, thisArg )
225+
```
226+
227+
where
228+
229+
- **arrays**: array containing an input ndarray, followed by any additional ndarray arguments.
230+
- **clbk**: callback function.
231+
- **thisArg**: callback function execution context.
232+
233+
- The output data type policy only applies to the `apply` method. For the `assign` method, the output ndarray is allowed to have any supported output data type.
234+
235+
</section>
236+
237+
<!-- /.notes -->
238+
239+
<section class="examples">
240+
241+
## Examples
242+
243+
<!-- eslint no-undef: "error" -->
244+
245+
```javascript
246+
var base = require( '@stdlib/stats/base/ndarray/max-by' );
247+
var filledarrayBy = require( '@stdlib/array/filled-by' );
248+
var uniform = require( '@stdlib/random/base/uniform' );
249+
var dtypes = require( '@stdlib/ndarray/dtypes' );
250+
var dtype = require( '@stdlib/ndarray/dtype' );
251+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
252+
var ndarray = require( '@stdlib/ndarray/ctor' );
253+
var UnaryStrided1dDispatchBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-by' );
254+
255+
// Define the supported input and output data types:
256+
var idt = dtypes();
257+
var odt = dtypes( 'real_and_generic' );
258+
259+
// Define dispatch policies:
260+
var policies = {
261+
'output': 'same',
262+
'casting': 'none'
263+
};
264+
265+
// Define a dispatch table:
266+
var table = {
267+
'default': base
268+
};
269+
270+
// Create an interface for performing a reduction:
271+
var maxBy = new UnaryStrided1dDispatchBy( table, [ idt ], odt, policies );
272+
273+
// Define a function for creating an object with a random value:
274+
function random() {
275+
return {
276+
'value': uniform( -1.0, 1.0 )
277+
};
278+
}
279+
280+
// Generate an array of random numbers:
281+
var xbuf = filledarrayBy( 100, 'generic', random );
282+
283+
// Wrap in an ndarray:
284+
var x = new ndarray( 'generic', xbuf, [ 10, 10 ], [ 10, 1 ], 0, 'row-major' );
285+
286+
// Define an accessor function:
287+
function accessor( v ) {
288+
return v.value * 100.0;
289+
}
290+
291+
// Perform a reduction:
292+
var opts = {
293+
'dims': [ 0 ]
294+
};
295+
var y = maxBy.apply( x, opts, accessor );
296+
297+
// Resolve the output array data type:
298+
var dt = dtype( y );
299+
console.log( dt );
300+
301+
// Print the results:
302+
console.log( ndarray2array( y ) );
303+
```
304+
305+
</section>
306+
307+
<!-- /.examples -->
308+
309+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
310+
311+
<section class="related">
312+
313+
</section>
314+
315+
<!-- /.related -->
316+
317+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
318+
319+
<section class="links">
320+
321+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
322+
323+
[@stdlib/ndarray/input-casting-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/input-casting-policies
324+
325+
</section>
326+
327+
<!-- /.links -->

0 commit comments

Comments
 (0)