From a4e08d4fd1d0c0ad7900bbca6c741d3848c0d2df Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 26 Nov 2024 03:42:48 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 41 ++- base/broadcasted-ternary4d/README.md | 141 +++++++++ .../benchmark/benchmark.js | 133 ++++++++ base/broadcasted-ternary4d/docs/repl.txt | 32 ++ .../docs/types/index.d.ts | 114 +++++++ base/broadcasted-ternary4d/docs/types/test.ts | 110 +++++++ base/broadcasted-ternary4d/examples/index.js | 47 +++ base/broadcasted-ternary4d/lib/index.js | 57 ++++ base/broadcasted-ternary4d/lib/main.js | 195 ++++++++++++ base/broadcasted-ternary4d/package.json | 67 ++++ base/broadcasted-ternary4d/test/test.js | 298 ++++++++++++++++++ 11 files changed, 1232 insertions(+), 3 deletions(-) create mode 100644 base/broadcasted-ternary4d/README.md create mode 100644 base/broadcasted-ternary4d/benchmark/benchmark.js create mode 100644 base/broadcasted-ternary4d/docs/repl.txt create mode 100644 base/broadcasted-ternary4d/docs/types/index.d.ts create mode 100644 base/broadcasted-ternary4d/docs/types/test.ts create mode 100644 base/broadcasted-ternary4d/examples/index.js create mode 100644 base/broadcasted-ternary4d/lib/index.js create mode 100644 base/broadcasted-ternary4d/lib/main.js create mode 100644 base/broadcasted-ternary4d/package.json create mode 100644 base/broadcasted-ternary4d/test/test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 296772ab..a566ba89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-11-25) +## Unreleased (2024-11-26)
@@ -156,6 +156,40 @@ This release closes the following issue: +
+ +#### [@stdlib/array/base/broadcasted-ternary4d](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/broadcasted-ternary4d) + +
+ +
+ +##### Features + +- [`c712a8e`](https://github.com/stdlib-js/stdlib/commit/c712a8ec07db672a01355ddfca2192be25fe85ca) - add `array/base/broadcasted-ternary4d` [(#3232)](https://github.com/stdlib-js/stdlib/pull/3232) + +
+ + + +
+ +##### Closed Issues + +This release closes the following issue: + +[#3165](https://github.com/stdlib-js/stdlib/issues/3165) + +
+ + + +
+ +
+ + +
#### [@stdlib/array/base/group-entries](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/group-entries) @@ -514,9 +548,9 @@ A total of 2 issues were closed in this release: ### Closed Issues -A total of 8 issues were closed in this release: +A total of 9 issues were closed in this release: -[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3164](https://github.com/stdlib-js/stdlib/issues/3164), [#3176](https://github.com/stdlib-js/stdlib/issues/3176), [#3177](https://github.com/stdlib-js/stdlib/issues/3177), [#3178](https://github.com/stdlib-js/stdlib/issues/3178), [#3179](https://github.com/stdlib-js/stdlib/issues/3179), [#3180](https://github.com/stdlib-js/stdlib/issues/3180) +[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3164](https://github.com/stdlib-js/stdlib/issues/3164), [#3165](https://github.com/stdlib-js/stdlib/issues/3165), [#3176](https://github.com/stdlib-js/stdlib/issues/3176), [#3177](https://github.com/stdlib-js/stdlib/issues/3177), [#3178](https://github.com/stdlib-js/stdlib/issues/3178), [#3179](https://github.com/stdlib-js/stdlib/issues/3179), [#3180](https://github.com/stdlib-js/stdlib/issues/3180)
@@ -545,6 +579,7 @@ A total of 6 people contributed to this release. Thank you to the following cont
+- [`c712a8e`](https://github.com/stdlib-js/stdlib/commit/c712a8ec07db672a01355ddfca2192be25fe85ca) - **feat:** add `array/base/broadcasted-ternary4d` [(#3232)](https://github.com/stdlib-js/stdlib/pull/3232) _(by Vinit Pandit, Athan Reines)_ - [`28db4f2`](https://github.com/stdlib-js/stdlib/commit/28db4f2869749f6a6d6f7ae6476bd27f65052e96) - **feat:** add `array/base/mskunary4d` [(#3208)](https://github.com/stdlib-js/stdlib/pull/3208) _(by Abhijit, Athan Reines)_ - [`fb3c880`](https://github.com/stdlib-js/stdlib/commit/fb3c880b79a6861fe3da4b14eefadafc90d63eda) - **feat:** add `array/base/braodcasted-ternary3d` [(#3228)](https://github.com/stdlib-js/stdlib/pull/3228) _(by Vinit Pandit, Athan Reines)_ - [`2af4b75`](https://github.com/stdlib-js/stdlib/commit/2af4b757cab1052ee7a788f2154f2c7b8c9aa9bb) - **docs:** add note _(by Athan Reines)_ diff --git a/base/broadcasted-ternary4d/README.md b/base/broadcasted-ternary4d/README.md new file mode 100644 index 00000000..5785a297 --- /dev/null +++ b/base/broadcasted-ternary4d/README.md @@ -0,0 +1,141 @@ + + +# bternary4d + +> Apply a ternary callback to elements in three [broadcasted][@stdlib/array/base/broadcast-array] nested input arrays and assign results to elements in a four-dimensional nested output array. + +
+ +
+ + + +
+ +## Usage + +```javascript +var bternary4d = require( '@stdlib/array/base/broadcasted-ternary4d' ); +``` + +#### bternary4d( arrays, shapes, fcn ) + +Applies a ternary callback to elements in three [broadcasted][@stdlib/array/base/broadcast-array] nested input arrays and assigns results to elements in a four-dimensional nested output array. + +```javascript +var add = require( '@stdlib/math/base/ops/add3' ); +var zeros4d = require( '@stdlib/array/base/zeros4d' ); + +var x = [ [ [ 1.0 ] ] ]; +var y = [ [ [ 2.0, 3.0 ] ] ]; +var z = [ [ [ [ 4.0, 5.0 ] ] ] ]; +var out = zeros4d( [ 1, 1, 2, 2 ] ); + +var shapes = [ + [ 1, 1, 1 ], + [ 1, 1, 2 ], + [ 1, 1, 1, 2 ], + [ 1, 1, 2, 2 ] +]; + +bternary4d( [ x, y, z, out ], shapes, add ); +// out => [ [ [ [ 7.0, 9.0 ], [ 7.0, 9.0 ] ] ] ] +``` + +The function accepts the following arguments: + +- **arrays**: array-like object containing three input nested arrays and one output nested array. +- **shapes**: array shapes. +- **fcn**: ternary function to apply. + +
+ + + +
+ +## Notes + +- The input and output array shapes must be broadcast [compatible][@stdlib/ndarray/base/broadcast-shapes]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var filled4dBy = require( '@stdlib/array/base/filled4d-by' ); +var zeros4d = require( '@stdlib/array/base/zeros4d' ); +var add = require( '@stdlib/math/base/ops/add3' ); +var bternary4d = require( '@stdlib/array/base/broadcasted-ternary4d' ); + +var shapes = [ + [ 1, 1, 1, 3 ], + [ 1, 1, 3, 1 ], + [ 1, 1, 3 ], + [ 1, 1, 3, 3 ] +]; + +var x = filled4dBy( shapes[ 0 ], discreteUniform( -100, 100 ) ); +console.log( x ); + +var y = filled4dBy( shapes[ 1 ], discreteUniform( -100, 100 ) ); +console.log( y ); + +var z = filled4dBy( shapes[ 2 ], discreteUniform( -100, 100 ) ); +console.log( z ); + +var out = zeros4d( shapes[ 3 ] ); +console.log( out ); + +bternary4d( [ x, y, z, out ], shapes, add ); +console.log( out ); +``` + +
+ + + + + + + + + + + + + + diff --git a/base/broadcasted-ternary4d/benchmark/benchmark.js b/base/broadcasted-ternary4d/benchmark/benchmark.js new file mode 100644 index 00000000..e3168f65 --- /dev/null +++ b/base/broadcasted-ternary4d/benchmark/benchmark.js @@ -0,0 +1,133 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/base/uniform' ).factory; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var add = require( '@stdlib/math/base/ops/add3' ); +var filled4dBy = require( './../../../base/filled4d-by' ); +var zeros4d = require( './../../../base/zeros4d' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var pkg = require( './../package.json' ).name; +var bternary4d = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveIntegerArray} shape - output array shape +* @returns {Function} benchmark function +*/ +function createBenchmark( shape ) { + var arrays; + var shapes; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 1, shape[ 2 ], 1 ], + [ shape[ 0 ], 1, 1, 1 ], + [ 1, shape[ 1 ], 1, 1 ], + shape + ]; + x = filled4dBy( shapes[ 0 ], uniform( -100.0, 100.0 ) ); + y = filled4dBy( shapes[ 1 ], uniform( -100.0, 100.0 ) ); + z = filled4dBy( shapes[ 2 ], uniform(-100.0, 100.0 ) ); + w = zeros4d( shapes[ 3 ] ); + + arrays = [ x, y, z, w ]; + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i0; + var i1; + var i2; + var i3; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bternary4d( arrays, shapes, add ); + i3 = i % shapes[ 1 ][ 0 ]; + i2 = i % shapes[ 1 ][ 1 ]; + i1 = i % shapes[ 1 ][ 2 ]; + i0 = i % shapes[ 1 ][ 3 ]; + if ( isnan( arrays[ 3 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + + i3 = i % shapes[ 1 ][ 0 ]; + i2 = i % shapes[ 1 ][ 1 ]; + i1 = i % shapes[ 1 ][ 2 ]; + i0 = i % shapes[ 1 ][ 3 ]; + if ( isnan( arrays[ 3 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var sh; + var N; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + N = floor( pow( pow( 10, i ), 1.0 / 4.0 ) ); + sh = [ N, N, N, N ]; + f = createBenchmark( sh ); + bench( pkg + '::equidimensional:size=' + numel( sh ), f ); + } +} + +main(); diff --git a/base/broadcasted-ternary4d/docs/repl.txt b/base/broadcasted-ternary4d/docs/repl.txt new file mode 100644 index 00000000..5b4fdd3f --- /dev/null +++ b/base/broadcasted-ternary4d/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( arrays, shapes, fcn ) + Applies a ternary callback to elements in three broadcasted input arrays and + assigns results to elements in a four-dimensional nested output array. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing three input nested arrays and one output + nested array. + + shapes: Array> + Array shapes. + + fcn: Function + Ternary callback. + + Examples + -------- + > function fcn( x, y, z ) { return x + y + z }; + > var x = [ [ [ 1.0 ] ] ]; + > var y = [ [ [ 2.0, 3.0 ] ] ]; + > var z = [ [ [ [ 4.0, 5.0 ] ] ] ]; + > var out = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ]; + > var shapes =[ [ 1, 1, 1 ], [ 1, 1, 2 ], [ 1, 1, 1, 2 ], [ 1, 1, 2, 2 ] ]; + > {{alias}}( [ x, y, z, out ], shapes, fcn ); + > out + [ [ [ [ 7.0, 9.0 ], [ 7.0, 9.0 ] ] ] ] + + See Also + -------- + diff --git a/base/broadcasted-ternary4d/docs/types/index.d.ts b/base/broadcasted-ternary4d/docs/types/index.d.ts new file mode 100644 index 00000000..e42ecb5f --- /dev/null +++ b/base/broadcasted-ternary4d/docs/types/index.d.ts @@ -0,0 +1,114 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Array1D, Array2D, Array3D, Array4D } from '@stdlib/types/array'; +import { Shape1D, Shape2D, Shape3D, Shape4D } from '@stdlib/types/ndarray'; + +/** +* Ternary callback. +* +* @param v1 - element from first input array +* @param v2 - element from second input array +* @param v3 - element from third input array +* @returns result +*/ +type Ternary = ( v1: T, v2: U, v3: V ) => W; + +/** +* Input array. +*/ +type InputArray = Array1D | Array2D | Array3D | Array4D; + +/** +* Input array shape. +*/ +type InputArrayShape = Shape1D | Shape2D | Shape3D | Shape4D; + +/** +* Output array. +*/ +type OutputArray = Array4D; + +/** +* Output array shape. +*/ +type OutputArrayShape = Shape4D; + +/** +* Input and output arrays. +*/ +type InOutArrays = [ + InputArray, + InputArray, + InputArray, + OutputArray +]; + +/** +* Input and output array shapes. +*/ +type InOutShapes = [ + InputArrayShape, + InputArrayShape, + InputArrayShape, + OutputArrayShape +]; + +/** +* Applies a ternary callback to elements in three broadcasted input arrays and assigns results to elements in a four-dimensional nested output array. +* +* ## Notes +* +* - The input array shapes must be broadcast compatible with the output array shape. +* +* @param arrays - array containing three input nested arrays and one output nested array +* @param shapes - array shapes +* @param fcn - ternary callback +* +* @example +* var ones4d = require( '@stdlib/array/base/ones4d' ); +* var zeros4d = require( '@stdlib/array/base/zeros4d' ); +* var add = require( '@stdlib/math/base/ops/add3' ); +* +* var shapes = [ +* [ 1, 2, 1, 1 ], +* [ 2, 1, 1, 1 ], +* [ 1, 1, 2, 1 ], +* [ 2, 2, 2, 2 ] +* ]; +* +* var x = ones4d( shapes[ 0 ] ); +* var y = ones4d( shapes[ 1 ] ); +* var z = ones4d( shapes[ 2 ] ); +* var out = zeros4d( shapes[ 3 ] ); +* +* bternary4d( [ x, y, z, out ], shapes, add ); +* +* console.log( out ); +* // => [ [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ], [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ], [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ], [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ] ] +*/ +declare function bternary4d( arrays: InOutArrays, shapes: InOutShapes, fcn: Ternary ): void; + + +// EXPORTS // + +export = bternary4d; diff --git a/base/broadcasted-ternary4d/docs/types/test.ts b/base/broadcasted-ternary4d/docs/types/test.ts new file mode 100644 index 00000000..8300caef --- /dev/null +++ b/base/broadcasted-ternary4d/docs/types/test.ts @@ -0,0 +1,110 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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. +*/ + +import bternary4d = require( './index' ); + +/** +* Ternary function. +* +* @param x - input value +* @param y - input value +* @param z - input value +* @returns result +*/ +function fcn( x: number, y: number, z: number ): number { + return x + y + z; +} + + +// TESTS // + +// The function returns undefined... +{ + const x = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const y = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const z = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const out = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ]; + + const shapes: [ Array, Array, Array, Array ] = [ [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ] ]; + + bternary4d( [ x, y, z, out ], shapes, fcn ); // $ExpectType void + bternary4d( [ x[ 0 ][ 0 ][ 0 ], y, z, out ], [ [ shapes[ 0 ][ 2 ] ], shapes[ 1 ], shapes[ 2 ], shapes[ 3 ] ], fcn ); // $ExpectType void +} + +// The compiler throws an error if the function is provided a first argument which is not an array of nested arrays... +{ + const shapes: [ Array, Array, Array, Array ] = [ [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ] ]; + + bternary4d( 'abc', shapes, fcn ); // $ExpectError + bternary4d( 3.14, shapes, fcn ); // $ExpectError + bternary4d( true, shapes, fcn ); // $ExpectError + bternary4d( false, shapes, fcn ); // $ExpectError + bternary4d( null, shapes, fcn ); // $ExpectError + bternary4d( [ '1' ], shapes, fcn ); // $ExpectError + bternary4d( {}, shapes, fcn ); // $ExpectError + bternary4d( ( x: number ): number => x, shapes, fcn ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array of arrays... +{ + const x = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const y = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const z = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const out = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ]; + + bternary4d( [ x, y, z, out ], 'abc', fcn ); // $ExpectError + bternary4d( [ x, y, z, out ], 3.14, fcn ); // $ExpectError + bternary4d( [ x, y, z, out ], true, fcn ); // $ExpectError + bternary4d( [ x, y, z, out ], false, fcn ); // $ExpectError + bternary4d( [ x, y, z, out ], null, fcn ); // $ExpectError + bternary4d( [ x, y, z, out ], [ '1' ], fcn ); // $ExpectError + bternary4d( [ x, y, z, out ], {}, fcn ); // $ExpectError + bternary4d( [ x, y, z, out ], ( x: number ): number => x, fcn ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a valid callback... +{ + const x = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const y = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const z = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const out = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ]; + + const shapes: [ Array, Array, Array, Array ] = [ [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ] ]; + + bternary4d( [ x, y, z, out ], shapes, 'abc' ); // $ExpectError + bternary4d( [ x, y, z, out ], shapes, 3.14 ); // $ExpectError + bternary4d( [ x, y, z, out ], shapes, true ); // $ExpectError + bternary4d( [ x, y, z, out ], shapes, false ); // $ExpectError + bternary4d( [ x, y, z, out ], shapes, null ); // $ExpectError + bternary4d( [ x, y, z, out ], shapes, [ '1' ] ); // $ExpectError + bternary4d( [ x, y, z, out ], shapes, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const y = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const z = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]; + const out = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ]; + + const shapes: [ Array, Array, Array, Array ] = [ [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ], [ 2, 2, 2, 2 ] ]; + + bternary4d(); // $ExpectError + bternary4d( [ x, y, z, out ] ); // $ExpectError + bternary4d( [ x, y, z, out ], shapes, fcn, {} ); // $ExpectError +} diff --git a/base/broadcasted-ternary4d/examples/index.js b/base/broadcasted-ternary4d/examples/index.js new file mode 100644 index 00000000..b554301b --- /dev/null +++ b/base/broadcasted-ternary4d/examples/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var filled4dBy = require( './../../../base/filled4d-by' ); +var zeros4d = require( './../../../base/zeros4d' ); +var add = require( '@stdlib/math/base/ops/add3' ); +var bternary4d = require( './../lib' ); + +var shapes = [ + [ 1, 3, 1, 1 ], + [ 3, 1, 1, 3 ], + [ 1, 1, 3, 1 ], + [ 3, 3, 3, 3 ] +]; + +var x = filled4dBy( shapes[ 0 ], discreteUniform( -100, 100 ) ); +console.log( x ); + +var y = filled4dBy( shapes[ 1 ], discreteUniform( -100, 100 ) ); +console.log( y ); + +var z = filled4dBy( shapes[ 2 ], discreteUniform( -100, 100 ) ); +console.log( z ); + +var out = zeros4d( shapes[ 3 ] ); +console.log( out ); + +bternary4d( [ x, y, z, out ], shapes, add ); +console.log( out ); diff --git a/base/broadcasted-ternary4d/lib/index.js b/base/broadcasted-ternary4d/lib/index.js new file mode 100644 index 00000000..1da50d3f --- /dev/null +++ b/base/broadcasted-ternary4d/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Apply a ternary callback to elements in three broadcasted input arrays and assign results to elements in a four-dimensional nested output array. +* +* @module @stdlib/array/base/broadcasted-ternary4d +* +* @example +* var ones4d = require( '@stdlib/array/base/ones4d' ); +* var zeros4d = require( '@stdlib/array/base/zeros4d' ); +* var add = require( '@stdlib/math/base/ops/add3' ); +* var bternary4d = require( '@stdlib/array/base/broadcasted-ternary4d' ); +* +* var shapes = [ +* [ 1, 2, 1, 1 ], +* [ 2, 1, 1, 1 ], +* [ 1, 1, 2, 1 ], +* [ 2, 2, 2, 2 ] +* ]; +* +* var x = ones4d( shapes[ 0 ] ); +* var y = ones4d( shapes[ 1 ] ); +* var z = ones4d( shapes[ 2 ] ); +* var out = zeros4d( shapes[ 3 ] ); +* +* bternary3d( [ x, y, z, out ], shapes, add ); +* +* console.log( out ); +* // => [ [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ], [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ], [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ], [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/broadcasted-ternary4d/lib/main.js b/base/broadcasted-ternary4d/lib/main.js new file mode 100644 index 00000000..93b3dc8d --- /dev/null +++ b/base/broadcasted-ternary4d/lib/main.js @@ -0,0 +1,195 @@ +/* eslint-disable max-statements */ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +// MODULES // + +var broadcastArray = require( './../../../base/broadcast-array' ); + + +// MAIN // + +/** +* Applies a ternary callback to elements in three broadcasted input arrays and assigns results to elements in a four-dimensional nested output array. +* +* @param {ArrayLikeObject>} arrays - array-like object containing three input nested arrays and one output nested array +* @param {ArrayLikeObject} shapes - array shapes +* @param {Callback} fcn - ternary callback +* @returns {void} +* +* @example +* var ones4d = require( '@stdlib/array/base/ones4d' ); +* var zeros4d = require( '@stdlib/array/base/zeros4d' ); +* var add = require( '@stdlib/math/base/ops/add3' ); +* +* var shapes = [ +* [ 1, 2, 1, 1 ], +* [ 2, 1, 1, 1 ], +* [ 1, 1, 2, 1 ], +* [ 2, 2, 2, 2 ] +* ]; +* +* var x = ones4d( shapes[ 0 ] ); +* var y = ones4d( shapes[ 1 ] ); +* var z = ones4d( shapes[ 2 ] ); +* var out = zeros4d( shapes[ 3 ] ); +* +* bternary4d( [ x, y, z, out ], shapes, add ); +* +* console.log( out ); +* // => [ [ [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ], [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ] ], [ [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ], [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ] ] ] +*/ +function bternary4d( arrays, shapes, fcn ) { + var dx0; + var dx1; + var dx2; + var dx3; + var dy0; + var dy1; + var dy2; + var dy3; + var dz0; + var dz1; + var dz2; + var dz3; + var S0; + var S1; + var S2; + var S3; + var i0; + var i1; + var i2; + var i3; + var j0; + var j1; + var j2; + var j3; + var k0; + var k1; + var k2; + var k3; + var m0; + var m1; + var m2; + var m3; + var x0; + var x1; + var x2; + var y0; + var y1; + var y2; + var z0; + var z1; + var z2; + var sh; + var st; + var w0; + var w1; + var w2; + var o; + var x; + var y; + var z; + var w; + + sh = shapes[ 3 ]; + S0 = sh[ 3 ]; + S1 = sh[ 2 ]; + S2 = sh[ 1 ]; + S3 = sh[ 0 ]; + if ( S0 <= 0 || S1 <= 0 || S2 <= 0 || S3 <= 0 ) { + return; + } + o = broadcastArray( arrays[ 0 ], shapes[ 0 ], sh ); + x = o.data; + st = o.strides; + dx0 = st[ 3 ]; + dx1 = st[ 2 ]; + dx2 = st[ 1 ]; + dx3 = st[ 0 ]; + + o = broadcastArray( arrays[ 1 ], shapes[ 1 ], sh ); + y = o.data; + st = o.strides; + dy0 = st[ 3 ]; + dy1 = st[ 2 ]; + dy2 = st[ 1 ]; + dy3 = st[ 0 ]; + + o = broadcastArray( arrays[ 2 ], shapes[ 2 ], sh ); + z = o.data; + st = o.strides; + dz0 = st[ 3 ]; + dz1 = st[ 2 ]; + dz2 = st[ 1 ]; + dz3 = st[ 0 ]; + + w = arrays[ 3 ]; + j3 = 0; + k3 = 0; + m3 = 0; + for ( i3 = 0; i3 < S3; i3++ ) { + j2 = 0; + k2 = 0; + m2 = 0; + x2 = x[ j3 ]; + y2 = y[ k3 ]; + z2 = z[ m3 ]; + w2 = w[ i3 ]; + for ( i2 = 0; i2 < S2; i2++ ) { + j1 = 0; + k1 = 0; + m1 = 0; + x1 = x2[ j2 ]; + y1 = y2[ k2 ]; + z1 = z2[ m2 ]; + w1 = w2[ i2 ]; + for ( i1 = 0; i1 < S1; i1++ ) { + j0 = 0; + k0 = 0; + m0 = 0; + x0 = x1[ j1 ]; + y0 = y1[ k1 ]; + z0 = z1[ m1 ]; + w0 = w1[ i1 ]; + for ( i0 = 0; i0 < S0; i0++ ) { + w0[ i0 ] = fcn( x0[ j0 ], y0[ k0 ], z0[ m0 ] ); + j0 += dx0; + k0 += dy0; + m0 += dz0; + } + j1 += dx1; + k1 += dy1; + m1 += dz1; + } + j2 += dx2; + k2 += dy2; + m2 += dz2; + } + j3 += dx3; + k3 += dy3; + m3 += dz3; + } +} + + +// EXPORTS // + +module.exports = bternary4d; diff --git a/base/broadcasted-ternary4d/package.json b/base/broadcasted-ternary4d/package.json new file mode 100644 index 00000000..fd100ff9 --- /dev/null +++ b/base/broadcasted-ternary4d/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/array/base/broadcasted-ternary4d", + "version": "0.0.0", + "description": "Apply a ternary callback to elements in three broadcasted input arrays and assign results to elements in a four-dimensional nested output array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "base", + "array", + "multidimensional", + "ndarray", + "matrix", + "4d", + "binary", + "apply", + "foreach", + "map", + "transform", + "broadcast" + ], + "__stdlib__": {} +} diff --git a/base/broadcasted-ternary4d/test/test.js b/base/broadcasted-ternary4d/test/test.js new file mode 100644 index 00000000..18278204 --- /dev/null +++ b/base/broadcasted-ternary4d/test/test.js @@ -0,0 +1,298 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +// MODULES // + +var tape = require( 'tape' ); +var add = require( '@stdlib/math/base/ops/add3' ); +var zeros4d = require( './../../../base/zeros4d' ); +var bternary4d = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof bternary4d, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function applies a provided callback to broadcasted input arrays and assigns results to a nested output array', function test( t ) { + var expected; + var shapes; + var x; + var y; + var z; + var w; + + shapes = [ + [ 2 ], + [ 2, 1, 1, 1 ], + [ 2, 1, 2 ], + [ 2, 2, 2, 2 ] + ]; + x = [ 1.0, 2.0 ]; + y = [ + [ [ [ 3.0 ] ] ], + [ [ [ 4.0 ] ] ] + ]; + z = [ [ [ 5.0, 6.0 ] ], [ [ 7.0, 8.0 ] ] ]; + w = zeros4d( shapes[ 3 ] ); + + expected = [ + [ + [ [ 9.0, 11.0 ], [ 9.0, 11.0 ] ], + [ [ 11.0, 13.0 ], [ 11.0, 13.0 ] ] + ], + [ + [ [ 10.0, 12.0 ], [ 10.0, 12.0 ] ], + [ [ 12.0, 14.0 ], [ 12.0, 14.0 ] ] + ] + ]; + bternary4d( [ x, y, z, w ], shapes, add ); + t.deepEqual( w, expected, 'returns expected value' ); + + shapes = [ + [ 2 ], + [ 1, 2 ], + [ 2, 1, 1, 2 ], + [ 2, 2, 2, 2 ] + ]; + x = [ 1.0, 2.0 ]; + y = [ [ 3.0, 4.0 ] ]; + z = [ [ [ [ 5.0, 6.0 ] ] ], [ [ [ 7.0, 8.0 ] ] ] ]; + w = zeros4d( shapes[ 3 ] ); + + expected = [ + [ + [ [ 9.0, 12.0 ], [ 9.0, 12.0 ] ], + [ [ 9.0, 12.0 ], [ 9.0, 12.0 ] ] + + ], + [ + [ [ 11.0, 14.0 ], [ 11.0, 14.0 ] ], + [ [ 11.0, 14.0 ], [ 11.0, 14.0 ] ] + ] + ]; + bternary4d( [ x, y, z, w ], shapes, add ); + t.deepEqual( w, expected, 'returns expected value' ); + + // Same shapes: + shapes = [ + [ 2, 2, 2, 2 ], + [ 2, 2, 2, 2 ], + [ 2, 2, 2, 2 ], + [ 2, 2, 2, 2 ] + ]; + x = [ + [ + [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], + [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + ], + [ + [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], + [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + ] + ]; + + y = [ + [ + [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ], + [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] + ], + [ + [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ], + [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] + ] + ]; + z = [ + [ + [ [ 9.0, 10.0 ], [ 10.0, 12.0 ] ], + [ [ 9.0, 10.0 ], [ 10.0, 12.0 ] ] + ], + [ + [ [ 9.0, 10.0 ], [ 10.0, 12.0 ] ], + [ [ 9.0, 10.0 ], [ 10.0, 12.0 ] ] + ] + ]; + w = zeros4d( shapes[ 3 ] ); + + expected = [ + [ + [ [ 15.0, 18.0 ], [ 20.0, 24.0 ] ], + [ [ 15.0, 18.0 ], [ 20.0, 24.0 ] ] + ], + [ + [ [ 15.0, 18.0 ], [ 20.0, 24.0 ] ], + [ [ 15.0, 18.0 ], [ 20.0, 24.0 ] ] + ] + ]; + bternary4d( [ x, y, z, w ], shapes, add ); + t.deepEqual( w, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function does not invoke a provided callback if provided an output shape having a first element equal to zero', function test( t ) { + var expected; + var shapes; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 1, 2, 2 ], + [ 1, 1, 2, 2 ], + [ 1, 1, 2, 2 ], + [ 0, 2, 2, 2 ] + ]; + x = [ + [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ] + ]; + y = x; + z = x; + w = zeros4d( [ 2, 2, 2, 2 ] ); + + expected = zeros4d( [ 2, 2, 2, 2 ] ); + bternary4d( [ x, y, z, w ], shapes, clbk ); + t.deepEqual( w, expected, 'returns expected value' ); + + t.end(); + + function clbk() { + t.ok( false, 'should not invoke callback' ); + } +}); + +tape( 'the function does not invoke a provided callback if provided an output shape having a second element equal to zero', function test( t ) { + var expected; + var shapes; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 1, 2, 2 ], + [ 1, 1, 2, 2 ], + [ 1, 1, 2, 2 ], + [ 2, 0, 2, 2 ] + ]; + x = [ + [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ] + ]; + y = x; + z = x; + w = zeros4d( [ 2, 2, 2, 2 ] ); + + expected = zeros4d( [ 2, 2, 2, 2 ] ); + bternary4d( [ x, y, z, w ], shapes, clbk ); + t.deepEqual( w, expected, 'returns expected value' ); + + t.end(); + + function clbk() { + t.ok( false, 'should not invoke callback' ); + } +}); + +tape( 'the function does not invoke a provided callback if provided an output shape having a third element equal to zero', function test( t ) { + var expected; + var shapes; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 1, 2, 2 ], + [ 1, 1, 2, 2 ], + [ 1, 1, 2, 2 ], + [ 2, 2, 0, 2 ] + ]; + x = [ + [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ] + ]; + y = x; + z = x; + w = zeros4d( [ 2, 2, 2, 2 ] ); + + expected = zeros4d( [ 2, 2, 2, 2 ] ); + bternary4d( [ x, y, z, w ], shapes, clbk ); + t.deepEqual( w, expected, 'returns expected value' ); + + t.end(); + + function clbk() { + t.ok( false, 'should not invoke callback' ); + } +}); + +tape( 'the function does not invoke a provided callback if provided an output shape having a forth element equal to zero', function test( t ) { + var expected; + var shapes; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 1, 2, 2 ], + [ 1, 1, 2, 2 ], + [ 1, 1, 2, 2 ], + [ 2, 2, 2, 0 ] + ]; + x = [ + [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ] + ]; + y = x; + z = x; + w = zeros4d( [ 2, 2, 2, 2 ] ); + + expected = zeros4d( [ 2, 2, 2, 2 ] ); + bternary4d( [ x, y, z, w ], shapes, clbk ); + t.deepEqual( w, expected, 'returns expected value' ); + + t.end(); + + function clbk() { + t.ok( false, 'should not invoke callback' ); + } +});