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,344 @@
<!--

@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.

-->

# kernel

> Return a kernel for applying a quaternary callback to elements in four input ndarrays and assigning results to elements in an output ndarray using linear view iteration.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

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

#### kernel( ndims )

Returns a kernel for applying a quaternary callback to elements in four input ndarrays and assigning results to elements in an output ndarray using linear view iteration.

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
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, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var zbuf = new Float64Array( [ 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 ] );
var wbuf = new Float64Array( [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0 ] );
var ubuf = new Float64Array( 6 );

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

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

// Create the input and output ndarray-like objects:
var x = {
'dtype': 'float64',
'data': xbuf,
'shape': shape,
'strides': sx,
'offset': 0,
'order': 'row-major'
};
var y = {
'dtype': 'float64',
'data': ybuf,
'shape': shape,
'strides': sy,
'offset': 0,
'order': 'row-major'
};
var z = {
'dtype': 'float64',
'data': zbuf,
'shape': shape,
'strides': sz,
'offset': 0,
'order': 'row-major'
};
var w = {
'dtype': 'float64',
'data': wbuf,
'shape': shape,
'strides': sw,
'offset': 0,
'order': 'row-major'
};
var u = {
'dtype': 'float64',
'data': ubuf,
'shape': shape,
'strides': su,
'offset': 0,
'order': 'row-major'
};

// Resolve a kernel:
var f = kernel( 13 );

// Apply the quaternary function:
f( x, y, z, w, u, add4 );

var arr = ndarray2array( u.data, u.shape, u.strides, u.offset, u.order );
// returns [ [ [ [ [ [ [ [ [ [ [ [ [ 7.0, 8.0 ], [ 9.0, 10.0 ], [ 11.0, 12.0 ] ] ] ] ] ] ] ] ] ] ] ] ]
```

The function accepts the following arguments:

- **ndims**: number of dimensions.

If the function is provided an `ndims` value less than `0`, the function returns `null`.

```javascript
var f = kernel( -1 );
// returns null
```

The returned function accepts the following arguments:

- **x**: first input ndarray descriptor (object with `dtype`, `data`, `shape`, `strides`, `offset`, `order`).
- **y**: second input ndarray descriptor.
- **z**: third input ndarray descriptor.
- **w**: fourth input ndarray descriptor.
- **u**: output ndarray descriptor.
- **fcn**: quaternary callback accepting four scalar values and returning one scalar value.

The returned function iterates over ndarray elements according to the linear **view** index, regardless as to how the data is stored in memory.

#### kernel.nd( x, y, z, w, u, fcn )

Applies a quaternary callback to elements in n-dimensional input ndarrays and assigns results to elements in an equivalently shaped output ndarray using linear view iteration.

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
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, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var zbuf = new Float64Array( [ 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 ] );
var wbuf = new Float64Array( [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0 ] );
var ubuf = new Float64Array( 6 );

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

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

// Create the input and output ndarray-like objects:
var x = {
'dtype': 'float64',
'data': xbuf,
'shape': shape,
'strides': sx,
'offset': 0,
'order': 'row-major'
};
var y = {
'dtype': 'float64',
'data': ybuf,
'shape': shape,
'strides': sy,
'offset': 0,
'order': 'row-major'
};
var z = {
'dtype': 'float64',
'data': zbuf,
'shape': shape,
'strides': sz,
'offset': 0,
'order': 'row-major'
};
var w = {
'dtype': 'float64',
'data': wbuf,
'shape': shape,
'strides': sw,
'offset': 0,
'order': 'row-major'
};
var u = {
'dtype': 'float64',
'data': ubuf,
'shape': shape,
'strides': su,
'offset': 0,
'order': 'row-major'
};

// Apply the quaternary function:
kernel.nd( x, y, z, w, u, add4 );

var arr = ndarray2array( u.data, u.shape, u.strides, u.offset, u.order );
// returns [ [ [ [ [ [ [ [ [ [ [ [ [ 7.0, 8.0 ], [ 9.0, 10.0 ], [ 11.0, 12.0 ] ] ] ] ] ] ] ] ] ] ] ] ]
```

The function has the following parameters:

- **x**: first input ndarray descriptor (object with `dtype`, `data`, `shape`, `strides`, `offset`, `order`).
- **y**: second input ndarray descriptor.
- **z**: third input ndarray descriptor.
- **w**: fourth input ndarray descriptor.
- **u**: output ndarray descriptor.
- **fcn**: quaternary callback accepting four scalar values and returning one scalar value.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The quaternary callback is expected to have the following signature:

```text
fcn( v1, v2, v3, v4 )
```

where

- **v1**: element from the first input ndarray.
- **v2**: element from the second input ndarray.
- **v3**: element from the third input ndarray.
- **v4**: element from the fourth input ndarray.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
var add4 = require( '@stdlib/number/float64/base/add4' );
var kernel = require( '@stdlib/ndarray/base/kernels/generic/quaternary/linear' );

// 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, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var zbuf = new Float64Array( [ 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 ] );
var wbuf = new Float64Array( [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0 ] );
var ubuf = new Float64Array( 6 );

// Define the array shapes:
var shape = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2 ];

// Create the input and output ndarray-like objects:
var x = {
'dtype': 'float64',
'data': xbuf,
'shape': shape,
'strides': [ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 1 ],
'offset': 0,
'order': 'row-major'
};
var y = {
'dtype': 'float64',
'data': ybuf,
'shape': shape,
'strides': [ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 1 ],
'offset': 0,
'order': 'row-major'
};
var z = {
'dtype': 'float64',
'data': zbuf,
'shape': shape,
'strides': [ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 1 ],
'offset': 0,
'order': 'row-major'
};
var w = {
'dtype': 'float64',
'data': wbuf,
'shape': shape,
'strides': [ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 1 ],
'offset': 0,
'order': 'row-major'
};
var u = {
'dtype': 'float64',
'data': ubuf,
'shape': shape,
'strides': [ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 1 ],
'offset': 0,
'order': 'row-major'
};

// Resolve a kernel:
var f = kernel( 13 );

// Apply the quaternary function:
f( x, y, z, w, u, add4 );

console.log( ndarray2array( u.data, u.shape, u.strides, u.offset, u.order ) );
// => [ [ [ [ [ [ [ [ [ [ [ [ [ 7.0, 8.0 ], [ 9.0, 10.0 ], [ 11.0, 12.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 -->
Loading
Loading