@stdlib/ndarray-base-binary-reduce-strided1d-dispatch-factory
v0.0.1
Published
Create a function for performing reduction on two input ndarrays.
Readme
binaryStrided1dDispatchFactory
Create a function for performing reduction on two input ndarrays.
Installation
npm install @stdlib/ndarray-base-binary-reduce-strided1d-dispatch-factoryUsage
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.
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
typesmust be twice the number of strided functions specified byfcns(i.e., for every pair of input ndarray data types, there must be a corresponding strided reduction function infcns). - 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:
binary( x, y[, ...args][, options] )
Performs a reduction on two input ndarrays.
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 <ndarray>[ -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.
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 <ndarray>
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.
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 <ndarray>[ -5.0 ]
var bool = ( out === z );
// returns trueThe 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:
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
assignmethod, the output ndarray is allowed to have any supported output data type.
Examples
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, 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, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
