From 5f89de630c3b91368e7569b75765b81a8dd2cd52 Mon Sep 17 00:00:00 2001 From: Gururaj Gurram Date: Mon, 25 Nov 2024 00:17:31 +0530 Subject: [PATCH 1/3] feat: add array/base/broadcasted-quaternary4d --- .../base/broadcasted-quaternary4d/README.md | 147 +++++++ .../benchmark/benchmark.js | 136 +++++++ .../broadcasted-quaternary4d/docs/repl.txt | 33 ++ .../docs/types/index.d.ts | 120 ++++++ .../docs/types/test.ts | 115 ++++++ .../examples/index.js | 51 +++ .../broadcasted-quaternary4d/lib/index.js | 59 +++ .../base/broadcasted-quaternary4d/lib/main.js | 229 +++++++++++ .../broadcasted-quaternary4d/package.json | 67 ++++ .../broadcasted-quaternary4d/test/test.js | 358 ++++++++++++++++++ 10 files changed, 1315 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/README.md create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/package.json create mode 100644 lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/test/test.js diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/README.md b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/README.md new file mode 100644 index 00000000000..193e513c604 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/README.md @@ -0,0 +1,147 @@ + + +# bquaternary4d + +> Apply a quaternary callback to elements in four [broadcasted][@stdlib/array/base/broadcast-array] nested input arrays and assign results to elements in a four-dimensional nested output array. + +
+ +
+ + + +
+ +## Usage + +```javascript +var bquaternary4d = require( '@stdlib/array/base/broadcasted-quaternary4d' ); +``` + +#### bquaternary4d( arrays, shapes, fcn ) + +Applies a quaternary callback to elements in four [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/add4' ); +var zeros4d = require( '@stdlib/array/base/zeros4d' ); + +var x = [ [ 1.0, 2.0 ] ]; +var y = [ [ 3.0 ], [ 4.0 ] ]; +var z = [ [ 5.0 ] ]; +var w = [ [ 2.0 ] ]; +var out = zeros4d( [ 2, 2, 2, 2 ] ); + +var shapes = [ + [ 1, 2 ], + [ 2, 1 ], + [ 1, 1 ], + [ 1, 1 ], + [ 1, 2, 2, 2 ] +]; + +bquaternary4d( [ x, y, z, w, out ], shapes, add ); +// out => [ [ [ [ 11.0, 12.0 ], [ 12.0, 13.0 ] ], [ [ 11.0, 12.0 ], [ 12.0, 13.0 ] ] ] ] +``` + +The function accepts the following arguments: + +- **arrays**: array-like object containing two input nested arrays and one output nested array. +- **shapes**: array shapes. +- **fcn**: quaternary 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/add4' ); +var bquaternary4d = require( '@stdlib/array/base/broadcasted-quaternary4d' ); + +var shapes = [ + [ 1, 1, 3, 1 ], + [ 3, 1, 1, 1 ], + [ 1, 1, 1, 3 ], + [ 1, 3, 1, 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 w = filled4dBy( shapes[ 3 ], discreteUniform( -100, 100 ) ); +console.log( w ); + +var out = zeros4d( shapes[ 4 ] ); +console.log( z ); + +bquaternary4d( [ x, y, z, w, out ], shapes, add ); +console.log( out ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/benchmark/benchmark.js new file mode 100644 index 00000000000..617d41ed337 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/benchmark/benchmark.js @@ -0,0 +1,136 @@ +/** +* @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/add4' ); +var filled4dBy = require( '@stdlib/array/base/filled4d-by' ); +var zeros4d = require( '@stdlib/array/base/zeros4d' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var pkg = require( './../package.json' ).name; +var bquaternary4d = 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 out; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 1, shape[ 2 ] ], + [ 1, shape[ 1 ], 1 ], + [ shape[ 0 ], 1, 1 ], + [ shape[ 0 ], shape[ 1 ], shape[ 2 ] ], + 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 = filled4dBy( shapes[ 3 ], uniform( -100.0, 100.0 ) ); + out = zeros4d( shapes[ 4 ] ); + + arrays = [ x, y, z, w, out ]; + + 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++ ) { + bquaternary4d( 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[ 4 ][ 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[ 4 ][ 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/3.0 ) ); + sh = [ N, N, N, N ]; + f = createBenchmark( sh ); + bench( pkg+'::equidimensional:size='+numel( sh ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/repl.txt b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/repl.txt new file mode 100644 index 00000000000..3d41340e89e --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( arrays, shapes, fcn ) + Applies a quaternary callback to elements in four broadcasted nested + input arrays and assigns results to elements in a four-dimensional + nested output array. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing two input nested arrays and one output + nested array. + + shapes: Array> + Array shapes. + + fcn: Function + Binary callback. + + Examples + -------- + > function fcn( x, y, z, w ) { return x + y + z + w; }; + > var x = [ 1.0, 2.0 ]; + > var y = [ [ 3.0 ], [ 4.0 ] ]; + > var z = [ [ 5.0 ] ]; + > var w = [ 1.0 ]; + > var out = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ]; + > var shapes = [ [ 2 ], [ 2, 1 ], [ 1, 1 ], [ 1 ], [ 1, 1, 2, 2 ] ]; + > {{alias}}( [ x, y, z, w, out ], shapes, fcn ); + > out + [ [ [ [ 10.0, 11.0 ], [ 11.0, 12.0 ] ] ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/types/index.d.ts new file mode 100644 index 00000000000..64ebe50979e --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/types/index.d.ts @@ -0,0 +1,120 @@ +/* +* @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'; + +/** +* Quaternary callback. +* +* @param v1 - element from first input array +* @param v2 - element from second input array +* @param v3 - element from third input array +* @param v4 - element from fourth input array +* @returns result +*/ +type Quaternary = ( v1: T, v2: U, v3: V, v4: W ) => X; + +/** +* 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, + InputArray, + OutputArray +]; + +/** +* Input and output array shapes. +*/ +type InOutShapes = [ + InputArrayShape, + InputArrayShape, + InputArrayShape, + InputArrayShape, + OutputArrayShape +]; + +/** +* Applies a quaternary callback to elements in four broadcasted nested 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 two input nested arrays and one output nested array +* @param shapes - array shapes +* @param fcn - quaternary callback +* +* @example +* var add = require( '@stdlib/math/base/ops/add4' ); +* var ones4d = require( '@stdlib/array/base/ones4d' ); +* var zeros4d = require( '@stdlib/array/base/zeros4d' ); +* var bquaternary4d = require( '@stdlib/array/base/broadcasted-quaternary4d' ); +* +* var shapes = [ +* [ 1, 1, 3, 1 ], +* [ 3, 1, 1, 1 ], +* [ 1, 1, 1, 3 ], +* [ 1, 3, 1, 1 ], +* [ 3, 3, 3, 3 ] +* ]; +* +* var x = ones4d( shapes[ 0 ] ); +* var y = ones4d( shapes[ 1 ] ); +* var z = ones4d( shapes[ 2 ] ); +* var w = ones4d( shapes[ 3 ] ); +* var out = zeros4d( shapes[ 4 ] ); +* +* bquaternary4d( [ x, y, z, w, out ], shapes, add ); +* +* console.log( out ); +* // => [ [ [ [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ] ], [ [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ] ], [ [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ] ] ] ] +*/ +declare function bquaternary4d( arrays: InOutArrays, shapes: InOutShapes, fcn: Quaternary ): void; + + +// EXPORTS // + +export = bquaternary4d; diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/types/test.ts new file mode 100644 index 00000000000..cf462380472 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/docs/types/test.ts @@ -0,0 +1,115 @@ +/* +* @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 bquaternary4d = require( './index' ); + +/** +* Quaternary function. +* +* @param x - input value +* @param y - input value +* @param z - input value +* @param w - input value +* @returns result +*/ +function fcn( x: number, y: number, z: number, w: number ): number { + return x + y + z + w; +} + + +// TESTS // + +// The function returns undefined... +{ + const x = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const y = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const z = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const w = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const out = [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ]; + + const shapes: [ Array, Array, Array, Array, Array ] = [ [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2, 2 ] ]; + + bquaternary4d( [ x, y, z, w, out ], shapes, fcn ); // $ExpectType void + bquaternary4d( [ x[ 0 ], y, z, w, out ], [ [ shapes[ 0 ][ 1 ] ], shapes[ 1 ], shapes[ 2 ], shapes[ 3 ], shapes[ 4 ] ], 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, Array ] = [ [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2, 2 ] ]; + + bquaternary4d( 'abc', shapes, fcn ); // $ExpectError + bquaternary4d( 3.14, shapes, fcn ); // $ExpectError + bquaternary4d( true, shapes, fcn ); // $ExpectError + bquaternary4d( false, shapes, fcn ); // $ExpectError + bquaternary4d( null, shapes, fcn ); // $ExpectError + bquaternary4d( [ '1' ], shapes, fcn ); // $ExpectError + bquaternary4d( {}, shapes, fcn ); // $ExpectError + bquaternary4d( ( 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 ] ] ]; + const y = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const z = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const w = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const out = [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ]; + + bquaternary4d( [ x, y, z, w, out ], 'abc', fcn ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], 3.14, fcn ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], true, fcn ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], false, fcn ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], null, fcn ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], [ '1' ], fcn ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], {}, fcn ); // $ExpectError + bquaternary4d( [ x, y, z, w, 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 ] ] ]; + const y = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const z = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const w = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const out = [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ]; + + const shapes: [ Array, Array, Array, Array, Array ] = [ [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2, 2 ] ]; + + bquaternary4d( [ x, y, z, w, out ], shapes, 'abc' ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], shapes, 3.14 ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], shapes, true ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], shapes, false ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], shapes, null ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], shapes, [ '1' ] ); // $ExpectError + bquaternary4d( [ x, y, z, w, 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 ] ] ]; + const y = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const z = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const w = [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]; + const out = [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ]; + + const shapes: [ Array, Array, Array, Array, Array ] = [ [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2 ], [ 1, 2, 2, 2 ] ]; + + bquaternary4d(); // $ExpectError + bquaternary4d( [ x, y, z, w, out ] ); // $ExpectError + bquaternary4d( [ x, y, z, w, out ], shapes, fcn, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/examples/index.js b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/examples/index.js new file mode 100644 index 00000000000..7d8a033a5d8 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/examples/index.js @@ -0,0 +1,51 @@ +/** +* @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( '@stdlib/array/base/filled4d-by' ); +var zeros4d = require( '@stdlib/array/base/zeros4d' ); +var add = require( '@stdlib/math/base/ops/add4' ); +var bquaternary4d = require( './../lib' ); + +var shapes = [ + [ 1, 1, 3, 1 ], + [ 3, 1, 1, 1 ], + [ 1, 1, 1, 3 ], + [ 1, 3, 1, 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 w = filled4dBy( shapes[ 3 ], discreteUniform( -100, 100 ) ); +console.log( w ); + +var out = zeros4d( shapes[ 4 ] ); +console.log( z ); + +bquaternary4d( [ x, y, z, w, out ], shapes, add ); +console.log( out ); diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/lib/index.js b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/lib/index.js new file mode 100644 index 00000000000..2a39ddf9010 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/lib/index.js @@ -0,0 +1,59 @@ +/** +* @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 quaternary callback to elements in four broadcasted nested input arrays and assign results to elements in a four-dimensional nested output array. +* +* @module @stdlib/array/base/broadcasted-quaternary4d +* +* @example +* var add = require( '@stdlib/math/base/ops/add4' ); +* var ones4d = require( '@stdlib/array/base/ones4d' ); +* var zeros4d = require( '@stdlib/array/base/zeros4d' ); +* var bquaternary4d = require( '@stdlib/array/base/broadcasted-quaternary4d' ); +* +* var shapes = [ +* [ 1, 1, 3, 1 ], +* [ 3, 1, 1, 1 ], +* [ 1, 1, 1, 3 ], +* [ 1, 3, 1, 1 ], +* [ 3, 3, 3, 3 ] +* ]; +* +* var x = ones4d( shapes[ 0 ] ); +* var y = ones4d( shapes[ 1 ] ); +* var z = ones4d( shapes[ 2 ] ); +* var w = ones4d( shapes[ 3 ] ); +* var out = zeros4d( shapes[ 4 ] ); +* +* bquaternary4d( [ x, y, z, w, out ], shapes, add ); +* +* console.log( out ); +* // => [ [ [ [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ] ], [ [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ] ], [ [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ] ] ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/lib/main.js b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/lib/main.js new file mode 100644 index 00000000000..1ae8520ce35 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/lib/main.js @@ -0,0 +1,229 @@ +/** +* @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( '@stdlib/array/base/broadcast-array' ); + + +// MAIN // + +/** +* Applies a quaternary callback to elements in four broadcasted input arrays and assigns results to elements in a four-dimensional nested output array. +* +* @param {ArrayLikeObject} arrays - array-like object containing two input nested arrays and one output nested array +* @param {ArrayLikeObject} shapes - array shapes +* @param {Callback} fcn - quaternary callback +* @returns {void} +* +* @example +* var add = require( '@stdlib/math/base/ops/add4' ); +* var ones4d = require( '@stdlib/array/base/ones4d' ); +* var zeros4d = require( '@stdlib/array/base/zeros4d' ); +* var bquaternary4d = require( '@stdlib/array/base/broadcasted-quaternary4d' ); +* +* var shapes = [ +* [ 1, 1, 3, 1 ], +* [ 3, 1, 1, 1 ], +* [ 1, 1, 1, 3 ], +* [ 1, 3, 1, 1 ], +* [ 3, 3, 3, 3 ] +* ]; +* +* var x = ones4d( shapes[ 0 ] ); +* var y = ones4d( shapes[ 1 ] ); +* var z = ones4d( shapes[ 2 ] ); +* var w = ones4d( shapes[ 3 ] ); +* var out = zeros4d( shapes[ 4 ] ); +* +* bquaternary4d( [ x, y, z, w, out ], shapes, add ); +* +* console.log( out ); +* // => [ [ [ [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ] ], [ [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ] ], [ [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ], [ 4.0, 4.0, 4.0 ] ] ] ] +*/ +function bquaternary4d( 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 dw0; + var dw1; + var dw2; + var dw3; + 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 n0; + var n1; + var n2; + var n3; + var x0; + var x1; + var x2; + var y0; + var y1; + var y2; + var z0; + var z1; + var z2; + var w0; + var w1; + var w2; + var u0; + var u1; + var u2; + var sh; + var st; + var o; + var x; + var y; + var z; + var w; + var u; + + sh = shapes[ 4 ]; + 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 ]; + + o = broadcastArray( arrays[ 3 ], shapes[ 3 ], sh ); + w = o.data; + st = o.strides; + dw0 = st[ 3 ]; + dw1 = st[ 2 ]; + dw2 = st[ 1 ]; + dw3 = st[ 0 ]; + + u = arrays[ 4 ]; + + j3 = 0; + k3 = 0; + m3 = 0; + n3 = 0; + for ( i3 = 0; i3 < S3; i3++ ) { + j2 = 0; + k2 = 0; + m2 = 0; + n2 = 0; + x2 = x[ j3 ]; + y2 = y[ k3 ]; + z2 = z[ m3 ]; + w2 = w[ n3 ]; + u2 = u[ i3 ]; + for ( i2 = 0; i2 < S2; i2++ ) { + j1 = 0; + k1 = 0; + m1 = 0; + n1 = 0; + x1 = x2[ j2 ]; + y1 = y2[ k2 ]; + z1 = z2[ m2 ]; + w1 = w2[ n2 ]; + u1 = u2[ i2 ]; + for ( i1 = 0; i1 < S1; i1++ ) { + j0 = 0; + k0 = 0; + m0 = 0; + n0 = 0; + x0 = x1[ j1 ]; + y0 = y1[ k1 ]; + z0 = z1[ m1 ]; + w0 = w1[ n1 ]; + u0 = u1[ i1 ]; + for ( i0 = 0; i0 < S0; i0++ ) { + u0[ i0 ] = fcn( x0[ j0 ], y0[ k0 ], z0[ m0 ], w0[ n0 ] ); + j0 += dx0; + k0 += dy0; + m0 += dz0; + n0 += dw0; + } + j1 += dx1; + k1 += dy1; + m1 += dz1; + n1 += dw1; + } + j2 += dx2; + k2 += dy2; + m2 += dz2; + n2 += dw2; + } + j3 += dx3; + k3 += dy3; + m3 += dz3; + n3 += dw3; + } +} + + +// EXPORTS // + +module.exports = bquaternary4d; diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/package.json b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/package.json new file mode 100644 index 00000000000..03110d61877 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/array/base/broadcasted-quaternary4d", + "version": "0.0.0", + "description": "Apply a quaternary callback to elements in four broadcasted nested 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", + "quaternary", + "apply", + "foreach", + "map", + "transform", + "broadcast" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/test/test.js b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/test/test.js new file mode 100644 index 00000000000..9b5721b5ab1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/test/test.js @@ -0,0 +1,358 @@ +/** +* @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/add4' ); +var zeros4d = require( '@stdlib/array/base/zeros4d' ); +var bquaternary4d = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof bquaternary4d, '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 out; + var x; + var y; + var z; + var w; + + shapes = [ + [ 2 ], + [ 2, 1 ], + [ 1, 1 ], + [ 1 ], + [ 2, 2, 2, 2 ] + ]; + x = [ 1.0, 2.0 ]; + y = [ + [ 3.0 ], + [ 4.0 ] + ]; + z = [ [ 5.0 ] ]; + w = [ 1.0 ]; + out = zeros4d( shapes[ 4 ] ); + + expected = [ + [ + [ + [ 10.0, 11.0 ], + [ 11.0, 12.0 ] + ], + [ + [ 10.0, 11.0 ], + [ 11.0, 12.0 ] + ] + ], + [ + [ + [ 10.0, 11.0 ], + [ 11.0, 12.0 ] + ], + [ + [ 10.0, 11.0 ], + [ 11.0, 12.0 ] + ] + ] + ]; + bquaternary4d( [ x, y, z, w, out ], shapes, add ); + t.deepEqual( out, expected, 'returns expected value' ); + + shapes = [ + [ 1, 2 ], + [ 2 ], + [ 1 ], + [ 2, 2 ], + [ 2, 2, 2, 2 ] + ]; + x = [ + [ 1.0, 2.0 ] + ]; + y = [ 3.0, 4.0 ]; + z = [ 5.0 ]; + w = [ + [ 2.0, 3.0 ], + [ 4.0, 5.0 ] + ]; + out = zeros4d( shapes[ 4 ] ); + + expected = [ + [ + [ + [ 11.0, 14.0 ], + [ 13.0, 16.0 ] + ], + [ + [ 11.0, 14.0 ], + [ 13.0, 16.0 ] + ] + ], + [ + [ + [ 11.0, 14.0 ], + [ 13.0, 16.0 ] + ], + [ + [ 11.0, 14.0 ], + [ 13.0, 16.0 ] + ] + ] + ]; + bquaternary4d( [ x, y, z, w, out ], shapes, add ); + t.deepEqual( out, 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 ] + ] + ]; + y = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ]; + z = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ]; + w = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ]; + out = zeros4d( shapes[ 4 ] ); + + expected = [ + [ + [ + [ 4.0, 8.0 ], + [ 12.0, 16.0 ] + ], + [ + [ 4.0, 8.0 ], + [ 12.0, 16.0 ] + ] + ], + [ + [ + [ 4.0, 8.0 ], + [ 12.0, 16.0 ] + ], + [ + [ 4.0, 8.0 ], + [ 12.0, 16.0 ] + ] + ] + ]; + bquaternary4d( [ x, y, z, w, out ], shapes, add ); + t.deepEqual( out, 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 out; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 0, 2, 2, 2 ] + ]; + x = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ]; + y = x; + z = x; + w = x; + out = zeros4d( [ 2, 2, 2, 2 ] ); + + expected = zeros4d( [ 2, 2, 2, 2 ] ); + bquaternary4d( [ x, y, z, w, out ], shapes, clbk ); + t.deepEqual( out, 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 out; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 2, 0, 2, 2 ] + ]; + x = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ]; + y = x; + z = x; + w = x; + out = zeros4d( [ 2, 2, 2, 2 ] ); + + expected = zeros4d( [ 2, 2, 2, 2 ] ); + bquaternary4d( [ x, y, z, w, out ], shapes, clbk ); + t.deepEqual( out, 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 out; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 2, 2, 0, 2 ] + ]; + x = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ]; + y = x; + z = x; + w = x; + out = zeros4d( [ 2, 2, 2, 2 ] ); + + expected = zeros4d( [ 2, 2, 2, 2 ] ); + bquaternary4d( [ x, y, z, w, out ], shapes, clbk ); + t.deepEqual( out, 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 fourth element equal to zero', function test( t ) { + var expected; + var shapes; + var out; + var x; + var y; + var z; + var w; + + shapes = [ + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 1, 2, 2 ], + [ 2, 2, 2, 0 ] + ]; + x = [ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ]; + y = x; + z = x; + w = x; + out = zeros4d( [ 2, 2, 2, 2 ] ); + + expected = zeros4d( [ 2, 2, 2, 2 ] ); + bquaternary4d( [ x, y, z, w, out ], shapes, clbk ); + t.deepEqual( out, expected, 'returns expected value' ); + + t.end(); + + function clbk() { + t.ok( false, 'should not invoke callback' ); + } +}); From 96fcb6251ba0548c8ed59a343c5c677b443ecb28 Mon Sep 17 00:00:00 2001 From: Gururaj Gurram Date: Mon, 25 Nov 2024 00:27:55 +0530 Subject: [PATCH 2/3] fix: lint errors --- .../base/broadcasted-quaternary4d/examples/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/examples/index.js b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/examples/index.js index 7d8a033a5d8..9d406d82595 100644 --- a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/examples/index.js +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/examples/index.js @@ -25,11 +25,11 @@ var add = require( '@stdlib/math/base/ops/add4' ); var bquaternary4d = require( './../lib' ); var shapes = [ - [ 1, 1, 3, 1 ], - [ 3, 1, 1, 1 ], - [ 1, 1, 1, 3 ], - [ 1, 3, 1, 1 ], - [ 3, 3, 3, 3 ] + [ 1, 1, 3, 1 ], + [ 3, 1, 1, 1 ], + [ 1, 1, 1, 3 ], + [ 1, 3, 1, 1 ], + [ 3, 3, 3, 3 ] ]; var x = filled4dBy( shapes[ 0 ], discreteUniform( -100, 100 ) ); From 23ba9637c694c206c222d2c87433c49649857875 Mon Sep 17 00:00:00 2001 From: Gururaj Gurram Date: Mon, 25 Nov 2024 16:50:40 +0530 Subject: [PATCH 3/3] fix: benchmark.js --- .../array/base/broadcasted-quaternary4d/benchmark/benchmark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/benchmark/benchmark.js index 617d41ed337..21514146f33 100644 --- a/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/array/base/broadcasted-quaternary4d/benchmark/benchmark.js @@ -126,7 +126,7 @@ function main() { max = 6; // 10^max for ( i = min; i <= max; i++ ) { - N = floor( pow( pow( 10, i ), 1.0/3.0 ) ); + 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 );