Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<!--

@license Apache-2.0

Copyright (c) 2026 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.

-->

# dispatch

> Apply a quaternary callback to elements in four input ndarrays and assign results to elements in an output ndarray.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var dispatch = require( '@stdlib/ndarray/base/kernels/generic/quaternary/dispatch' );
```

#### dispatch( arrays, fcn )

Applies a quaternary callback to elements in four input ndarrays and assigns results to elements in an output ndarray.

<!-- eslint-disable max-len -->

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
var getData = require( '@stdlib/ndarray/data-buffer' );
var getShape = require( '@stdlib/ndarray/shape' );
var getStrides = require( '@stdlib/ndarray/strides' );
var getOffset = require( '@stdlib/ndarray/offset' );
var getOrder = require( '@stdlib/ndarray/order' );
var add4 = require( '@stdlib/number/float64/base/add4' );

// Create data buffers:
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var ybuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var zbuf = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var wbuf = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var ubuf = new Float64Array( 6 );

// Define the shape of the input and output arrays:
var shape = [ 3, 1, 2 ];

// Define the array strides:
var sx = [ 2, 2, 1 ];
var sy = [ 2, 2, 1 ];
var sz = [ 2, 2, 1 ];
var sw = [ 2, 2, 1 ];
var su = [ 2, 2, 1 ];

// Create the input and output ndarrays:
var x = ndarray( 'float64', xbuf, shape, sx, 0, 'row-major' );
var y = ndarray( 'float64', ybuf, shape, sy, 0, 'row-major' );
var z = ndarray( 'float64', zbuf, shape, sz, 0, 'row-major' );
var w = ndarray( 'float64', wbuf, shape, sw, 0, 'row-major' );
var u = ndarray( 'float64', ubuf, shape, su, 0, 'row-major' );

// Apply the quaternary callback:
dispatch( [ x, y, z, w, u ], add4 );

var arr = ndarray2array( getData( u ), getShape( u ), getStrides( u ), getOffset( u ), getOrder( u ) );
// returns [ [ [ 4.0, 6.0 ] ], [ [ 8.0, 10.0 ] ], [ [ 12.0, 14.0 ] ] ]
```

The function accepts the following arguments:

- **arrays**: array-like object containing four input ndarrays and one output ndarray.
- **fcn**: quaternary callback accepting four scalar values and returning one scalar value.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Each provided ndarray should be an object with the following properties:

- **dtype**: data type.
- **data**: data buffer.
- **shape**: dimensions.
- **strides**: stride lengths.
- **offset**: index offset.
- **order**: specifies whether an ndarray is row-major (C-style) or column-major (Fortran-style).

- The function dispatches to the most efficient kernel based on array contiguity, memory layout, and dimensionality.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

<!-- eslint-disable max-len -->

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
var getData = require( '@stdlib/ndarray/data-buffer' );
var getShape = require( '@stdlib/ndarray/shape' );
var getStrides = require( '@stdlib/ndarray/strides' );
var getOffset = require( '@stdlib/ndarray/offset' );
var getOrder = require( '@stdlib/ndarray/order' );
var add4 = require( '@stdlib/number/float64/base/add4' );
var dispatch = require( '@stdlib/ndarray/base/kernels/generic/quaternary/dispatch' );

// Create data buffers:
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var ybuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var zbuf = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var wbuf = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var ubuf = new Float64Array( 6 );

// Define the shape of the input and output arrays:
var shape = [ 3, 1, 2 ];

// Define the array strides:
var sx = [ 2, 2, 1 ];
var sy = [ 2, 2, 1 ];
var sz = [ 2, 2, 1 ];
var sw = [ 2, 2, 1 ];
var su = [ 2, 2, 1 ];

// Create the input and output ndarrays:
var x = ndarray( 'float64', xbuf, shape, sx, 0, 'row-major' );
var y = ndarray( 'float64', ybuf, shape, sy, 0, 'row-major' );
var z = ndarray( 'float64', zbuf, shape, sz, 0, 'row-major' );
var w = ndarray( 'float64', wbuf, shape, sw, 0, 'row-major' );
var u = ndarray( 'float64', ubuf, shape, su, 0, 'row-major' );

// Apply the quaternary callback:
dispatch( [ x, y, z, w, u ], add4 );

console.log( ndarray2array( getData( u ), getShape( u ), getStrides( u ), getOffset( u ), getOrder( u ) ) );
// => [ [ [ 4.0, 6.0 ] ], [ [ 8.0, 10.0 ] ], [ [ 12.0, 14.0 ] ] ]
```

</section>

<!-- /.examples -->

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

<section class="related">

</section>

<!-- /.related -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

{{alias}}( arrays, fcn )
Applies a quaternary callback to elements in four input ndarrays and assigns
results to elements in an output ndarray.

Each provided ndarray should be an object with the following properties:

- dtype: data type.
- data: data buffer.
- shape: dimensions.
- strides: stride lengths.
- offset: index offset.
- order: specifies whether an ndarray is row-major (C-style) or column-major
(Fortran-style).

Parameters
----------
arrays: ArrayLikeObject<Object>
Array-like object containing four input ndarray-like objects and one
output ndarray-like object.

fcn: Function
Quaternary callback accepting four scalar values and returning one
scalar value.

Examples
--------
// Define ndarray data and meta data...
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 5.0, 6.0, 7.0, 8.0 ] );
> var zbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 1.0, 1.0, 1.0 ] );
> var wbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 1.0, 1.0, 1.0 ] );
> var ubuf = new {{alias:@stdlib/array/float64}}( 4 );
> var dtype = 'float64';
> var shape = [ 2, 2 ];
> var sx = [ 2, 1 ];
> var sy = [ 2, 1 ];
> var sz = [ 2, 1 ];
> var sw = [ 2, 1 ];
> var su = [ 2, 1 ];
> var ox = 0;
> var oy = 0;
> var oz = 0;
> var ow = 0;
> var ou = 0;
> var order = 'row-major';

// Using ndarrays...
> var x = {{alias:@stdlib/ndarray/ctor}}( dtype, xbuf, shape, sx, ox, order );
> var y = {{alias:@stdlib/ndarray/ctor}}( dtype, ybuf, shape, sy, oy, order );
> var z = {{alias:@stdlib/ndarray/ctor}}( dtype, zbuf, shape, sz, oz, order );
> var w = {{alias:@stdlib/ndarray/ctor}}( dtype, wbuf, shape, sw, ow, order );
> var u = {{alias:@stdlib/ndarray/ctor}}( dtype, ubuf, shape, su, ou, order );
> {{alias}}( [ x, y, z, w, u ], {{alias:@stdlib/number/float64/base/add4}} );
> {{alias:@stdlib/ndarray/data-buffer}}( u )
<Float64Array>[ 8.0, 10.0, 12.0, 14.0 ]

// Using minimal ndarray-like objects...
> x = {
... 'dtype': dtype,
... 'data': xbuf,
... 'shape': shape,
... 'strides': sx,
... 'offset': ox,
... 'order': order
... };
> y = {
... 'dtype': dtype,
... 'data': ybuf,
... 'shape': shape,
... 'strides': sy,
... 'offset': oy,
... 'order': order
... };
> z = {
... 'dtype': dtype,
... 'data': zbuf,
... 'shape': shape,
... 'strides': sz,
... 'offset': oz,
... 'order': order
... };
> w = {
... 'dtype': dtype,
... 'data': wbuf,
... 'shape': shape,
... 'strides': sw,
... 'offset': ow,
... 'order': order
... };
> u = {
... 'dtype': dtype,
... 'data': ubuf,
... 'shape': shape,
... 'strides': su,
... 'offset': ou,
... 'order': order
... };
> {{alias}}( [ x, y, z, w, u ], {{alias:@stdlib/number/float64/base/add4}} );
> {{alias:@stdlib/ndarray/data-buffer}}( u )
<Float64Array>[ 8.0, 10.0, 12.0, 14.0 ]

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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.
*/

'use strict';

var Float64Array = require( '@stdlib/array/float64' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
var getData = require( '@stdlib/ndarray/data-buffer' );
var getShape = require( '@stdlib/ndarray/shape' );
var getStrides = require( '@stdlib/ndarray/strides' );
var getOffset = require( '@stdlib/ndarray/offset' );
var getOrder = require( '@stdlib/ndarray/order' );
var add4 = require( '@stdlib/number/float64/base/add4' );
var dispatch = require( './../lib' );

// Create data buffers:
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var ybuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var zbuf = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var wbuf = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var ubuf = new Float64Array( 6 );

// Define the shape of the input and output arrays:
var shape = [ 3, 1, 2 ];

// Define the array strides:
var sx = [ 2, 2, 1 ];
var sy = [ 2, 2, 1 ];
var sz = [ 2, 2, 1 ];
var sw = [ 2, 2, 1 ];
var su = [ 2, 2, 1 ];

// Define the index offsets:
var ox = 0;
var oy = 0;
var oz = 0;
var ow = 0;
var ou = 0;

// Create the input and output ndarrays:
var x = ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' );
var y = ndarray( 'float64', ybuf, shape, sy, oy, 'row-major' );
var z = ndarray( 'float64', zbuf, shape, sz, oz, 'row-major' );
var w = ndarray( 'float64', wbuf, shape, sw, ow, 'row-major' );
var u = ndarray( 'float64', ubuf, shape, su, ou, 'row-major' );

// Apply the quaternary callback:
dispatch( [ x, y, z, w, u ], add4 );

console.log( ndarray2array( getData( u ), getShape( u ), getStrides( u ), getOffset( u ), getOrder( u ) ) );
// => [ [ [ 4.0, 6.0 ] ], [ [ 8.0, 10.0 ] ], [ [ 12.0, 14.0 ] ] ]
Loading
Loading