From 24f6941bc625d051be0e648a95f2fdea11ad9f3b Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Tue, 27 Feb 2024 02:10:56 +0530 Subject: [PATCH 1/2] feat: add reduceRight method to array/complex64 --- .../@stdlib/array/complex64/README.md | 54 +++++ .../benchmark/benchmark.reduce_right.js | 52 +++++ .../benchmark.reduce_right.length.js | 104 +++++++++ .../array/complex64/docs/types/index.d.ts | 29 +++ .../@stdlib/array/complex64/lib/main.js | 67 +++++- .../array/complex64/test/test.reduce_right.js | 217 ++++++++++++++++++ 6 files changed, 522 insertions(+), 1 deletion(-) create mode 100644 lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.js create mode 100644 lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.length.js create mode 100644 lib/node_modules/@stdlib/array/complex64/test/test.reduce_right.js diff --git a/lib/node_modules/@stdlib/array/complex64/README.md b/lib/node_modules/@stdlib/array/complex64/README.md index a127736f7e6f..338d8b40f132 100644 --- a/lib/node_modules/@stdlib/array/complex64/README.md +++ b/lib/node_modules/@stdlib/array/complex64/README.md @@ -1676,6 +1676,60 @@ var z = arr.reduce( reducer, 0.0 ); // returns 6.0 ``` + + +#### Complex64Array.prototype.reduceRight( reducerFn\[, initialValue] ) + +Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion. + +```javascript +var realf = require( '@stdlib/complex/realf' ); +var imagf = require( '@stdlib/complex/imagf' ); +var caddf = require( '@stdlib/math/base/ops/caddf' ); + +var arr = new Complex64Array( 3 ); + +arr.set( [ 1.0, 1.0 ], 0 ); +arr.set( [ 2.0, 2.0 ], 1 ); +arr.set( [ 3.0, 3.0 ], 2 ); + +var z = arr.reduceRight( caddf ); +// returns + +var re = realf( z ); +// returns 6.0 + +var im = imagf( z ); +// returns 6.0 +``` + +The reducer function is provided four arguments: + +- **acc**: accumulated result. +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +By default, the function initializes the accumulated result to the last element in the array and passes the second-last array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the last array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument. + +```javascript +var realf = require( '@stdlib/complex/realf' ); + +function reducer( acc, v ) { + acc += realf( v ); + return acc; +} + +var arr = new Complex64Array( 3 ); + +arr.set( [ 1.0, 1.0 ], 0 ); +arr.set( [ 2.0, 2.0 ], 1 ); +arr.set( [ 3.0, 3.0 ], 2 ); + +var z = arr.reduceRight( reducer, 0.0 ); +// returns 6.0 +``` + #### Complex64Array.prototype.reverse() diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.js new file mode 100644 index 000000000000..c726ffcda39c --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.js @@ -0,0 +1,52 @@ +/** +* @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 caddf = require( '@stdlib/math/base/ops/caddf' ); +var isComplexLike = require('@stdlib/assert/is-complex-like'); +var pkg = require( './../package.json' ).name; +var Complex64Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':reduceRight', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.reduceRight( caddf ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isComplexLike( out ) ) { + b.fail( 'should return a complex number' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.length.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.length.js new file mode 100644 index 000000000000..64a81f65053f --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.length.js @@ -0,0 +1,104 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var caddf = require( '@stdlib/math/base/ops/caddf' ); +var isComplexLike = require('@stdlib/assert/is-complex-like'); +var Complex64 = require( '@stdlib/complex/float32' ); +var pkg = require( './../package.json' ).name; +var Complex64Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr; + var i; + + arr = []; + for ( i = 0; i < len; i++ ) { + arr.push( new Complex64( i, i ) ); + } + arr = new Complex64Array( arr ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.reduceRight( caddf ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isComplexLike( out ) ) { + b.fail( 'should return a complex number' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':reduceRight:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts index b36afdf33aa6..9cd6b7d9eab3 100644 --- a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts @@ -896,6 +896,35 @@ declare class Complex64Array implements Complex64ArrayInterface { */ reduce( reducer: Reducer, initialValue?: U ): U; + /** + * Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion. + * + * @param reducer - callback function + * @param initialValue - initial value + * @returns accumulated result + * + * @example + * var realf = require( '@stdlib/complex/realf' ); + * var imagf = require( '@stdlib/complex/imagf' ); + * var caddf = require( '@stdlib/math/base/ops/caddf' ); + * + * var arr = new Complex64Array( 3 ); + * + * arr.set( [ 1.0, 1.0 ], 0 ); + * arr.set( [ 2.0, 2.0 ], 1 ); + * arr.set( [ 3.0, 3.0 ], 2 ); + * + * var z = arr.reduceRight( caddf ); + * // returns + * + * var re = realf( z ); + * // returns 6.0 + * + * var im = imagf( z ); + * // returns 6.0 + */ + reduceRight( reducer: Reducer, initialValue?: U ): U; + /** * Reverses an array in-place. * diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index ab9a0fcb74c2..7e1043a14007 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -1700,7 +1700,7 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) { }); /** -* Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion. +* Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion. * * @name reduce * @memberof Complex64Array.prototype @@ -1764,6 +1764,71 @@ setReadOnly( Complex64Array.prototype, 'reduce', function reduce( reducer, initi return acc; }); +/** +* Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the succeding element and returning the accumulated result upon completion. +* +* @name reduceRight +* @memberof Complex64Array.prototype +* @type {Function} +* @param {Function} reducer - callback function +* @param {*} [initialValue] - initial value +* @throws {TypeError} `this` must be a complex number array +* @throws {TypeError} first argument must be a function +* @throws {Error} if not provided an initial value, the array must have at least one element +* @returns {*} accumulated result +* +* @example +* var realf = require( '@stdlib/complex/realf' ); +* var imagf = require( '@stdlib/complex/imagf' ); +* var caddf = require( '@stdlib/math/base/ops/caddf' ); +* +* var arr = new Complex64Array( 3 ); +* +* arr.set( [ 1.0, 1.0 ], 0 ); +* arr.set( [ 2.0, 2.0 ], 1 ); +* arr.set( [ 3.0, 3.0 ], 2 ); +* +* var z = arr.reduceRight( caddf ); +* // returns +* +* var re = realf( z ); +* // returns 6.0 +* +* var im = imagf( z ); +* // returns 6.0 +*/ +setReadOnly( Complex64Array.prototype, 'reduceRight', function reduce( reducer, initialValue ) { + var buf; + var acc; + var len; + var v; + var i; + + if ( !isComplexArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); + } + if ( !isFunction( reducer ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length > 1 ) { + acc = initialValue; + i = len-1; + } else { + if ( len === 0 ) { + throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' ); + } + acc = getComplex64( buf, len-1 ); + i = len-2; + } + for ( ; i >= 0; i-- ) { + v = getComplex64( buf, i ); + acc = reducer( acc, v, i, this ); + } + return acc; +}); + /** * Reverses an array in-place. * diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce_right.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce_right.js new file mode 100644 index 000000000000..7884c5932d00 --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce_right.js @@ -0,0 +1,217 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var caddf = require( '@stdlib/math/base/ops/caddf' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var realf = require( '@stdlib/complex/realf' ); +var imagf = require( '@stdlib/complex/imagf' ); +var Complex64 = require('@stdlib/complex/float32'); +var Complex64Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Complex64Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `reduceRight` method', function test( t ) { + t.strictEqual( hasOwnProp( Complex64Array.prototype, 'reduceRight' ), true, 'has property' ); + t.strictEqual( isFunction( Complex64Array.prototype.reduceRight ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a complex number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Complex64Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.map.reduceRight( value, caddf ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Complex64Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.reduceRight( value ); + }; + } +}); + +tape( 'the method throws an error if not provided an initial value when operating on an empty complex number array', function test( t ) { + var arr; + + arr = new Complex64Array( 0 ); + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + arr.reduceRight( caddf ); + } +}); + +tape( 'the method uses the last element of the array as the initial value when an initial value is not provided', function test( t ) { + var valueArray; + var accArray; + var expected; + var actual; + var arr; + var len; + + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + len = arr.length; + accArray = [ 3.0, -3.0, 5.0, -5.0 ]; + valueArray = [ 2.0, -2.0, 1.0, -1.0 ]; + expected = new Complex64( 6.0, -6.0 ); + actual = arr.reduceRight( reducer ); + + t.strictEqual( instanceOf( actual, Complex64 ), true, 'returns expected value' ); + t.strictEqual( realf( actual ), realf( expected ), 'returns expected value' ); + t.strictEqual( imagf( actual ), imagf( expected ), 'returns expected value' ); + + t.end(); + + function reducer( acc, value, index ) { + var ind = 2*(len-index-2); + t.strictEqual( instanceOf( acc, Complex64 ), true, 'returns expected value' ); + t.strictEqual( realf( acc ), accArray[ ind ], 'returns expected value' ); + t.strictEqual( imagf( acc ), accArray[ ind+1 ], 'returns expected value' ); + t.strictEqual( instanceOf( value, Complex64 ), true, 'returns expected value' ); + t.strictEqual( realf( value ), valueArray[ ind ], 'returns expected value' ); + t.strictEqual( imagf( value ), valueArray[ ind+1 ], 'returns expected value' ); + return caddf( acc, value ); + } +}); + +tape( 'the method supports providing an initial value as the second argument', function test( t ) { + var valueArray; + var accArray; + var expected; + var actual; + var arr; + var len; + + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + len = arr.length; + accArray = [ 2.0, -2.0, 5.0, -5.0, 7.0, -7.0 ]; + valueArray = [ 3.0, -3.0, 2.0, -2.0, 1.0, -1.0 ]; + expected = new Complex64( 8.0, -8.0 ); + actual = arr.reduceRight( reducer, new Complex64( 2.0, -2.0 ) ); + + t.strictEqual( instanceOf( actual, Complex64 ), true, 'returns expected value' ); + t.strictEqual( realf( actual ), realf( expected ), 'returns expected value' ); + t.strictEqual( imagf( actual ), imagf( expected ), 'returns expected value' ); + + t.end(); + + function reducer( acc, value, index ) { + var ind = 2*(len-index-1); + t.strictEqual( instanceOf( acc, Complex64 ), true, 'returns expected value' ); + t.strictEqual( realf( acc ), accArray[ ind ], 'returns expected value' ); + t.strictEqual( imagf( acc ), accArray[ ind+1 ], 'returns expected value' ); + t.strictEqual( instanceOf( value, Complex64 ), true, 'returns expected value' ); + t.strictEqual( realf( value ), valueArray[ ind ], 'returns expected value' ); + t.strictEqual( imagf( value ), valueArray[ ind+1 ], 'returns expected value' ); + return caddf( acc, value ); + } +}); + +tape( 'the method returns the accumulated result', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + expected = new Complex64( 6.0, -6.0 ); + actual = arr.reduceRight( caddf ); + + t.strictEqual( instanceOf( actual, Complex64 ), true, 'returns expected value' ); + t.strictEqual( realf( actual ), realf( expected ), 'returns expected value' ); + t.strictEqual( imagf( actual ), imagf( expected ), 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports returning real-valued results', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + expected = 6.0; + actual = arr.reduceRight( reducer, 0.0 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value ) { + return acc + realf( value ); + } +}); From a2a3a92e23b405dcb45bb6869ad6227387bb94e2 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Mon, 26 Feb 2024 20:16:33 -0500 Subject: [PATCH 2/2] Apply suggestions from code review Signed-off-by: Philipp Burckhardt --- .../@stdlib/array/complex64/benchmark/benchmark.reduce_right.js | 2 +- .../array/complex64/benchmark/benchmark.reduce_right.length.js | 2 +- .../@stdlib/array/complex64/test/test.reduce_right.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.js index c726ffcda39c..028ca36b136e 100644 --- a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.js +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.js @@ -22,7 +22,7 @@ var bench = require( '@stdlib/bench' ); var caddf = require( '@stdlib/math/base/ops/caddf' ); -var isComplexLike = require('@stdlib/assert/is-complex-like'); +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); var pkg = require( './../package.json' ).name; var Complex64Array = require( './../lib' ); diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.length.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.length.js index 64a81f65053f..3b6ba9d03668 100644 --- a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.length.js +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce_right.length.js @@ -23,7 +23,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var caddf = require( '@stdlib/math/base/ops/caddf' ); -var isComplexLike = require('@stdlib/assert/is-complex-like'); +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); var Complex64 = require( '@stdlib/complex/float32' ); var pkg = require( './../package.json' ).name; var Complex64Array = require( './../lib' ); diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce_right.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce_right.js index 7884c5932d00..d08d03e97ea6 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.reduce_right.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce_right.js @@ -27,7 +27,7 @@ var caddf = require( '@stdlib/math/base/ops/caddf' ); var instanceOf = require( '@stdlib/assert/instance-of' ); var realf = require( '@stdlib/complex/realf' ); var imagf = require( '@stdlib/complex/imagf' ); -var Complex64 = require('@stdlib/complex/float32'); +var Complex64 = require( '@stdlib/complex/float32' ); var Complex64Array = require( './../lib' );