npm package: @stdlib/ndarray-base-binary-reduce-strided1d-dispatch
Description: Constructor for performing a reduction on two input ndarrays.
Author: The Stdlib Authors
Latest version: 0.1.1

<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

<!-- lint disable max-heading-length -->


<details>
  <summary>
    About stdlib...
  </summary>
  <p>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.</p>
  <p>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.</p>
  <p>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.</p>
  <p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
</details>

# BinaryStrided1dDispatch

[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->

> Constructor for performing a reduction on two input ndarrays.

<section class="installation">

## Installation

```bash
npm install @stdlib/ndarray-base-binary-reduce-strided1d-dispatch
```

</section>

<section class="usage">

## Usage

```javascript
var BinaryStrided1dDispatch = require( '@stdlib/ndarray-base-binary-reduce-strided1d-dispatch' );
```

#### BinaryStrided1dDispatch( table, idtypes, odtypes, policies )

Constructor for performing a 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 = new BinaryStrided1dDispatch( table, [ dtypes, 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 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].

#### BinaryStrided1dDispatch.prototype.apply( x, y\[, ...args]\[, options] )

Performs a reduction on two provided 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 = new BinaryStrided1dDispatch( 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.apply( x, y );
// returns <ndarray>[ -5.0 ]
```

The method has the following parameters:

-   **x**: first input ndarray.
-   **y**: second input ndarray.
-   **...args**: additional input ndarray arguments (_optional_).
-   **options**: function options (_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/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 = new BinaryStrided1dDispatch( 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.apply( x, y, {
    'dtype': 'float64'
});
// returns <ndarray>

var dt = String( getDType( z ) );
// returns 'float64'
```

#### BinaryStrided1dDispatch.prototype.assign( x, y\[, ...args], out\[, options] )

Performs a reduction on two provided 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 = new BinaryStrided1dDispatch( 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 <ndarray>[ -5.0 ]

var bool = ( out === z );
// returns true
```

The method has the following parameters:

-   **x**: first input ndarray.
-   **x**: 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.

</section>

<!-- /.usage -->

<section class="notes">

## 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 `apply` method. For the `assign` method, the output ndarray is allowed to have any supported output data type.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```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 BinaryStrided1dDispatch = require( '@stdlib/ndarray-base-binary-reduce-strided1d-dispatch' );

// 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 = new BinaryStrided1dDispatch( 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.apply( 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 ) );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->


<section class="main-repo" >

* * *

## 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]

---

## License

See [LICENSE][stdlib-license].


## Copyright

Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].

</section>

<!-- /.stdlib -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[npm-image]: http://img.shields.io/npm/v/@stdlib/ndarray-base-binary-reduce-strided1d-dispatch.svg
[npm-url]: https://npmjs.org/package/@stdlib/ndarray-base-binary-reduce-strided1d-dispatch

[test-image]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/actions/workflows/test.yml/badge.svg?branch=v0.1.1
[test-url]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/actions/workflows/test.yml?query=branch:v0.1.1

[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/main.svg
[coverage-url]: https://codecov.io/github/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch?branch=main

<!--

[dependencies-image]: https://img.shields.io/david/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch.svg
[dependencies-url]: https://david-dm.org/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/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/tree/deno
[deno-readme]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/blob/deno/README.md
[umd-url]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/tree/umd
[umd-readme]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/blob/umd/README.md
[esm-url]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/tree/esm
[esm-readme]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/blob/esm/README.md
[branches-url]: https://github.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/blob/main/branches.md

[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/ndarray-base-binary-reduce-strided1d-dispatch/main/LICENSE

[@stdlib/ndarray/output-dtype-policies]: https://www.npmjs.com/package/@stdlib/ndarray-output-dtype-policies

[@stdlib/ndarray/input-casting-policies]: https://www.npmjs.com/package/@stdlib/ndarray-input-casting-policies

</section>

<!-- /.links -->