https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory
Create a function for performing reduction on two input ndarrays.
https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory
apply array factory fcn func function javascript ndarray node node-js nodejs reduce reduction stdlib strided vector
Last synced: 17 days ago
JSON representation
Create a function for performing reduction on two input ndarrays.
- Host: GitHub
- URL: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory
- Owner: stdlib-js
- License: apache-2.0
- Created: 2025-09-13T23:41:54.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2026-04-11T04:02:50.000Z (about 2 months ago)
- Last Synced: 2026-04-11T05:32:04.663Z (about 2 months ago)
- Topics: apply, array, factory, fcn, func, function, javascript, ndarray, node, node-js, nodejs, reduce, reduction, stdlib, strided, vector
- Language: JavaScript
- Homepage: https://github.com/stdlib-js/stdlib
- Size: 3.92 MB
- 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
- Notice: NOTICE
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!
# binaryStrided1dDispatchFactory
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
> Create a function for performing reduction on two input ndarrays.
## Installation
```bash
npm install @stdlib/ndarray-base-binary-reduce-strided1d-dispatch-factory
```
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 binaryStrided1dDispatchFactory = require( '@stdlib/ndarray-base-binary-reduce-strided1d-dispatch-factory' );
```
#### binaryStrided1dDispatchFactory( table, idtypes, odtypes, policies )
Returns a function for performing reduction on two input ndarrays.
```javascript
var base = require( '@stdlib/blas-base-ndarray-gdot' );
var table = {
'default': base
};
var dtypes = [ 'float64', 'float32', 'generic' ];
var policies = {
'output': 'promoted',
'casting': 'promoted'
};
var binary = binaryStrided1dDispatchFactory( table, [ dtypes, dtypes ], dtypes, policies );
```
The function 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 be twice the number of strided functions specified by `fcns` (i.e., for every pair of input ndarray data types, 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].
#### binary( x, y\[, ...args]\[, options] )
Performs a reduction on two input ndarrays.
```javascript
var ndarray = require( '@stdlib/ndarray-base-ctor' );
var base = require( '@stdlib/blas-base-ndarray-gdot' );
var table = {
'default': base
};
var dtypes = [ 'float64', 'float32', 'generic' ];
var policies = {
'output': 'promoted',
'casting': 'promoted'
};
var binary = binaryStrided1dDispatchFactory( table, [ dtypes, dtypes ], dtypes, policies );
var xbuf = [ 4.0, 2.0, -3.0, 5.0, -1.0 ];
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
var ybuf = [ 2.0, 6.0, -1.0, -4.0, 8.0 ];
var y = new ndarray( 'generic', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
var z = binary( x, y );
// returns [ -5.0 ]
```
The function has the following parameters:
- **x**: first input ndarray.
- **y**: second input ndarray.
- **...args**: additional input ndarray arguments (_optional_).
- **options**: function options (_optional_).
The function 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 function 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/blas-base-ndarray-gdot' );
var getDType = require( '@stdlib/ndarray-dtype' );
var table = {
'default': base
};
var dtypes = [ 'float64', 'float32', 'generic' ];
var policies = {
'output': 'promoted',
'casting': 'promoted'
};
var binary = binaryStrided1dDispatchFactory( table, [ dtypes, dtypes ], dtypes, policies );
var xbuf = [ 4.0, 2.0, -3.0, 5.0, -1.0 ];
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
var ybuf = [ 2.0, 6.0, -1.0, -4.0, 8.0 ];
var y = new ndarray( 'generic', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
var z = binary( x, y, {
'dtype': 'float64'
});
// returns
var dt = String( getDType( z ) );
// returns 'float64'
```
#### binary.assign( x, y\[, ...args], out\[, options] )
Performs a reduction on two input ndarrays and assigns results to a provided output ndarray.
```javascript
var base = require( '@stdlib/blas-base-ndarray-gdot' );
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': 'promoted',
'casting': 'promoted'
};
var table = {
'default': base
};
var binary = binaryStrided1dDispatchFactory( table, [ idt, idt ], odt, policies );
var xbuf = [ 4.0, 2.0, -3.0, 5.0, -1.0 ];
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
var ybuf = [ 2.0, 6.0, -1.0, -4.0, 8.0 ];
var y = new ndarray( 'generic', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
var zbuf = [ 0.0 ];
var z = new ndarray( 'generic', zbuf, [], [ 0 ], 0, 'row-major' );
var out = binary.assign( x, y, z );
// returns [ -5.0 ]
var bool = ( out === z );
// returns true
```
The method has the following parameters:
- **x**: first input ndarray.
- **y**: second input ndarray.
- **args**: additional input ndarray arguments (_optional_).
- **out**: output ndarray.
- **options**: function options (_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 )
```
where
- **arrays**: array containing two input ndarrays, followed by any additional ndarray arguments.
- The output data type policy only applies to the function returned by the main function. For the `assign` method, the output ndarray is allowed to have any supported output data type.
## Examples
```javascript
var ddot = require( '@stdlib/blas-base-ndarray-ddot' );
var sdot = require( '@stdlib/blas-base-ndarray-sdot' );
var base = require( '@stdlib/blas-base-ndarray-gdot' );
var uniform = require( '@stdlib/random-array-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 binaryStrided1dDispatchFactory = require( '@stdlib/ndarray-base-binary-reduce-strided1d-dispatch-factory' );
// Define the supported input and output data types:
var idt = dtypes( 'real_and_generic' );
var odt = dtypes( 'real_and_generic' );
// Define dispatch policies:
var policies = {
'output': 'promoted',
'casting': 'promoted'
};
// Define a dispatch table:
var table = {
'types': [
'float64', 'float64', // input data types
'float32', 'float32' // input data types
],
'fcns': [
ddot,
sdot
],
'default': base
};
// Create an interface for performing a reduction:
var dot = binaryStrided1dDispatchFactory( table, [ idt, idt ], odt, policies );
// Generate arrays of random numbers:
var xbuf = uniform( 100, -1.0, 1.0, {
'dtype': 'generic'
});
var ybuf = uniform( 100, -1.0, 1.0, {
'dtype': 'generic'
});
// Wrap in ndarrays:
var x = new ndarray( 'generic', xbuf, [ 10, 10 ], [ 10, 1 ], 0, 'row-major' );
var y = new ndarray( 'generic', ybuf, [ 10, 10 ], [ 10, 1 ], 0, 'row-major' );
// Perform a reduction:
var z = dot( x, y, {
'dims': [ 0 ]
});
// Resolve the output array data type:
var dt = dtype( z );
console.log( String( dt ) );
// Print the results:
console.log( ndarray2array( z ) );
```
* * *
## 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-binary-reduce-strided1d-dispatch-factory.svg
[npm-url]: https://npmjs.org/package/@stdlib/ndarray-base-binary-reduce-strided1d-dispatch-factory
[test-image]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory/actions/workflows/test.yml/badge.svg?branch=main
[test-url]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory/actions/workflows/test.yml?query=branch:main
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory/main.svg
[coverage-url]: https://codecov.io/github/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory?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-binary-reduce-strided1d-dispatch-factory/tree/deno
[deno-readme]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory/blob/deno/README.md
[umd-url]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory/tree/umd
[umd-readme]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory/blob/umd/README.md
[esm-url]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory/tree/esm
[esm-readme]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory/blob/esm/README.md
[branches-url]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch-factory/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