https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by
Constructor for performing a reduction on an input ndarray according to a callback function.
https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by
apply array call javascript ndarray node node-js nodejs reduce reduction stdlib strided tools vector
Last synced: about 2 months ago
JSON representation
Constructor for performing a reduction on an input ndarray according to a callback function.
- Host: GitHub
- URL: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by
- Owner: stdlib-js
- License: apache-2.0
- Created: 2025-06-06T09:19:32.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-06T11:25:05.000Z (about 1 year ago)
- Last Synced: 2025-07-23T02:48:10.227Z (11 months ago)
- Topics: apply, array, call, javascript, ndarray, node, node-js, nodejs, reduce, reduction, stdlib, strided, tools, vector
- Language: JavaScript
- Homepage: https://github.com/stdlib-js/stdlib
- Size: 932 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Citation: CITATION.cff
- Security: SECURITY.md
Awesome Lists containing this project
README
About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
# UnaryStrided1dDispatchBy
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
> Constructor for performing a reduction on an input ndarray according to a callback function.
## Installation
```bash
npm install @stdlib/ndarray-base-unary-reduce-strided1d-dispatch-by
```
Alternatively,
- To load the package in a website via a `script` tag without installation and bundlers, use the [ES Module][es-module] available on the [`esm`][esm-url] branch (see [README][esm-readme]).
- If you are using Deno, visit the [`deno`][deno-url] branch (see [README][deno-readme] for usage intructions).
- For use in Observable, or in browser/node environments, use the [Universal Module Definition (UMD)][umd] build available on the [`umd`][umd-url] branch (see [README][umd-readme]).
The [branches.md][branches-url] file summarizes the available branches and displays a diagram illustrating their relationships.
To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
## Usage
```javascript
var UnaryStrided1dDispatchBy = require( '@stdlib/ndarray-base-unary-reduce-strided1d-dispatch-by' );
```
#### UnaryStrided1dDispatchBy( table, idtypes, odtypes, policies )
Constructor for performing a reduction on an input ndarray according to a callback function.
```javascript
var base = require( '@stdlib/stats-base-ndarray-max-by' );
var table = {
'default': base
};
var dtypes = [ 'float64', 'float32', 'generic' ];
var policies = {
'output': 'same',
'casting': 'none'
};
var unary = new UnaryStrided1dDispatchBy( table, [ dtypes ], dtypes, policies );
```
The constructor has the following parameters:
- **table**: strided reduction function dispatch table. Must have the following properties:
- **default**: default strided reduction function which should be invoked when provided ndarrays have data types which do not have a corresponding specialized implementation.
A dispatch table may have the following additional properties:
- **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`).
- **fcns**: list of strided reduction functions which are specific to specialized input ndarray argument signatures.
- **idtypes**: list containing lists of supported input data types for each input ndarray argument.
- **odtypes**: list of supported output data types.
- **policies**: dispatch policies. Must have the following properties:
- **output**: output data type [policy][@stdlib/ndarray/output-dtype-policies].
- **casting**: input ndarray casting [policy][@stdlib/ndarray/input-casting-policies].
#### UnaryStrided1dDispatchBy.prototype.apply( x\[, ...args]\[, options], clbk\[, thisArg] )
Performs a reduction on a provided input ndarray according to a callback function.
```javascript
var ndarray = require( '@stdlib/ndarray-base-ctor' );
var base = require( '@stdlib/stats-base-ndarray-max-by' );
var table = {
'default': base
};
var dtypes = [ 'float64', 'float32', 'generic' ];
var policies = {
'output': 'same',
'casting': 'none'
};
var unary = new UnaryStrided1dDispatchBy( table, [ dtypes ], dtypes, policies );
var xbuf = [ -1.0, 2.0, -3.0 ];
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
function clbk( v ) {
return v * 2.0;
}
var y = unary.apply( x, clbk );
// returns
var v = y.get();
// returns 4.0
```
The method has the following parameters:
- **x**: input ndarray.
- **...args**: additional input ndarray arguments (_optional_).
- **options**: function options (_optional_).
- **clbk**: callback function.
- **thisArg**: callback function execution context (_optional_).
The method accepts the following options:
- **dims**: list of dimensions over which to perform a reduction.
- **dtype**: output ndarray data type. Setting this option overrides the output data type policy.
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions. Default: `false`.
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.
```javascript
var ndarray = require( '@stdlib/ndarray-base-ctor' );
var base = require( '@stdlib/stats-base-ndarray-max-by' );
var getDType = require( '@stdlib/ndarray-dtype' );
var table = {
'default': base
};
var dtypes = [ 'float64', 'float32', 'generic' ];
var policies = {
'output': 'same',
'casting': 'none'
};
var unary = new UnaryStrided1dDispatchBy( table, [ dtypes ], dtypes, policies );
var xbuf = [ -1.0, 2.0, -3.0 ];
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
function clbk( v ) {
return v * 2.0;
}
var opts = {
'dtype': 'float64'
};
var y = unary.apply( x, opts, clbk );
// returns
var dt = String( getDType( y ) );
// returns 'float64'
```
#### UnaryStrided1dDispatchBy.prototype.assign( x\[, ...args], out\[, options], clbk\[, thisArg] )
Performs a reduction on a provided input ndarray according to a callback function and assigns results to a provided output ndarray.
```javascript
var base = require( '@stdlib/stats-base-ndarray-max-by' );
var dtypes = require( '@stdlib/ndarray-dtypes' );
var ndarray = require( '@stdlib/ndarray-base-ctor' );
var idt = dtypes( 'real_and_generic' );
var odt = idt;
var policies = {
'output': 'same',
'casting': 'none'
};
var table = {
'default': base
};
var unary = new UnaryStrided1dDispatchBy( table, [ idt ], odt, policies );
var xbuf = [ -1.0, 2.0, -3.0 ];
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
var ybuf = [ 0.0 ];
var y = new ndarray( 'generic', ybuf, [], [ 0 ], 0, 'row-major' );
function clbk( v ) {
return v * 2.0;
}
var out = unary.assign( x, y, clbk );
// returns
var v = out.get();
// returns 4.0
var bool = ( out === y );
// returns true
```
The method has the following parameters:
- **x**: input ndarray.
- **args**: additional input ndarray arguments (_optional_).
- **out**: output ndarray.
- **options**: function options (_optional_).
- **clbk**: callback function.
- **thisArg**: callback function execution context (_optional_).
The method accepts the following options:
- **dims**: list of dimensions over which to perform a reduction.
## Notes
- A strided reduction function should have the following signature:
```text
f( arrays, clbk, thisArg )
```
where
- **arrays**: array containing an input ndarray, followed by any additional ndarray arguments.
- **clbk**: callback function.
- **thisArg**: callback function execution context.
- 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.
## Examples
```javascript
var base = require( '@stdlib/stats-base-ndarray-max-by' );
var filledarrayBy = require( '@stdlib/array-filled-by' );
var uniform = require( '@stdlib/random-base-uniform' );
var dtypes = require( '@stdlib/ndarray-dtypes' );
var dtype = require( '@stdlib/ndarray-dtype' );
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var ndarray = require( '@stdlib/ndarray-ctor' );
var UnaryStrided1dDispatchBy = require( '@stdlib/ndarray-base-unary-reduce-strided1d-dispatch-by' );
// Define the supported input and output data types:
var idt = dtypes( 'real_and_generic' );
var odt = idt;
// Define dispatch policies:
var policies = {
'output': 'same',
'casting': 'none'
};
// Define a dispatch table:
var table = {
'default': base
};
// Create an interface for performing a reduction:
var maxBy = new UnaryStrided1dDispatchBy( table, [ idt ], odt, policies );
// Define a function for creating an object with a random value:
function random() {
return {
'value': uniform( -1.0, 1.0 )
};
}
// Generate an array of random numbers:
var xbuf = filledarrayBy( 100, 'generic', random );
// Wrap in an ndarray:
var x = new ndarray( 'generic', xbuf, [ 10, 10 ], [ 10, 1 ], 0, 'row-major' );
// Define an accessor function:
function accessor( v ) {
return v.value * 100.0;
}
// Perform a reduction:
var opts = {
'dims': [ 0 ]
};
var y = maxBy.apply( x, opts, accessor );
// Resolve the output array data type:
var dt = dtype( y );
console.log( dt );
// Print the results:
console.log( ndarray2array( y ) );
```
* * *
## Notice
This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
#### Community
[![Chat][chat-image]][chat-url]
---
## Copyright
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
[npm-image]: http://img.shields.io/npm/v/@stdlib/ndarray-base-unary-reduce-strided1d-dispatch-by.svg
[npm-url]: https://npmjs.org/package/@stdlib/ndarray-base-unary-reduce-strided1d-dispatch-by
[test-image]: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/actions/workflows/test.yml/badge.svg?branch=main
[test-url]: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/actions/workflows/test.yml?query=branch:main
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/main.svg
[coverage-url]: https://codecov.io/github/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by?branch=main
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
[chat-url]: https://stdlib.zulipchat.com
[stdlib]: https://github.com/stdlib-js/stdlib
[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
[umd]: https://github.com/umdjs/umd
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
[deno-url]: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/tree/deno
[deno-readme]: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/blob/deno/README.md
[umd-url]: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/tree/umd
[umd-readme]: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/blob/umd/README.md
[esm-url]: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/tree/esm
[esm-readme]: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/blob/esm/README.md
[branches-url]: https://github.com/stdlib-js/ndarray-base-unary-reduce-strided1d-dispatch-by/blob/main/branches.md
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/ndarray-output-dtype-policies
[@stdlib/ndarray/input-casting-policies]: https://github.com/stdlib-js/ndarray-input-casting-policies