From a34dd864c3a7009b0f6624c1c07ac2689df4d7c4 Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Mon, 15 Jan 2024 15:33:52 +0530 Subject: [PATCH 01/32] feat: add implementation and ts declaration to reduce method array/comlpex64 --- .../array/complex64/docs/types/index.d.ts | 77 +++++++++++++++++++ .../@stdlib/array/complex64/lib/main.js | 67 ++++++++++++++++ 2 files changed, 144 insertions(+) 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 257ddf98afdc..923d0a939c61 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 @@ -195,6 +195,50 @@ type TernaryMapFcn = ( this: U, value: Complex64, index: number, arr: Complex */ type MapFcn = NullaryMapFcn | UnaryMapFcn | BinaryMapFcn | TernaryMapFcn; +/** +* Reducer Function Invoked for each element in an array. +* +* @returns accumulated result +*/ +type NullaryReducer = ( this: U ) => ComplexLike; + +/** +* Reducer Function Invoked for each element in an array. +* +* @param acc - accumulated result +* @returns accumulated result +*/ +type UnaryReducer = ( this: U, acc: ComplexLike ) => ComplexLike; + +/** +* Reducer Function Invoked for each element in an array. +* +* @param acc - accumulated result +* @param value - current array element +* @returns accumulated result +*/ +type BinaryReducer = ( this: U, acc: ComplexLike, value: Complex64 ) => ComplexLike; + +/** +* Reducer Function Invoked for each element in an array. +* +* @param acc - accumulated result +* @param value - current array element +* @param index - current array element index +* @returns accumulated result +*/ +type TernaryReducer = ( this: U, acc: ComplexLike, value: Complex64, index: number ) => ComplexLike; + +/** +* Reducer Function Invoked for each element in an array. +* +* @param acc - accumulated result +* @param value - current array element +* @param index - current array element index +* @returns accumulated result +*/ +type ReducerFn = NullaryReducer | UnaryReducer | BinaryReducer | TernaryReducer; + /** * Class for creating a 64-bit complex number array. */ @@ -813,6 +857,39 @@ declare class Complex64Array implements Complex64ArrayInterface { */ map( fcn: MapFcn, thisArg?: ThisParameterType> ): Complex64Array; + /** + * Returns value that results from running the reducer callback function to completion over the entire typed array. + * + * @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 Complex64 = require( '@stdlib/complex/float32' ); + * + * function sum( acc, v ) { + * return new Complex64( realf( acc ) + realf( v ), imagf( acc ) + imagf( v ) ); + * } + * + * 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.reduce( sum ); + * // returns + * + * var re = realf( z ); + * // returns 6.0 + * + * var im = imagf( z ); + * // returns 6.0 + */ + reduce( reducer: ReducerFn, initialValue?: ComplexLike ): Complex64 | void; + /** * 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 48fbfb10a403..eb842e1b9561 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -1699,6 +1699,73 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) { return out; }); +/** +* Returns value that results from running the reducer callback function to completion over the entire typed array. +* +* @name reduce +* @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 +* @returns {*} accumulated result +* +* @example +* var realf = require( '@stdlib/complex/realf' ); +* var imagf = require( '@stdlib/complex/imagf' ); +* var Complex64 = require( '@stdlib/complex/float32' ); +* +* function sum( acc, v ) { +* return new Complex64( realf( acc ) + realf( v ), imagf( acc ) + imagf( v ) ); +* } +* +* 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.reduce( sum ); +* // returns +* +* var re = realf( z ); +* // returns 6.0 +* +* var im = imagf( z ); +* // returns 6.0 +*/ +setReadOnly( Complex64Array.prototype, 'reduce', 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; + } else { + if ( len === 0 ) { + throw new TypeError( 'invalid argument. If not provided an initial value, input array must contain at least one element.' ); + } + acc = getComplex64( buf, 0 ); + i = 1; + } + for ( ; i < len; i++ ) { + v = getComplex64( buf, i ); + acc = reducer( acc, v, i, this ); + } + return acc; +}); + /** * Reverses an array in-place. * From 5c887774a024ad7002f813db0d04ec7da4488d19 Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Mon, 15 Jan 2024 15:38:03 +0530 Subject: [PATCH 02/32] feat: updated typescript declaration --- lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 923d0a939c61..c055e802e75d 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 @@ -888,7 +888,7 @@ declare class Complex64Array implements Complex64ArrayInterface { * var im = imagf( z ); * // returns 6.0 */ - reduce( reducer: ReducerFn, initialValue?: ComplexLike ): Complex64 | void; + reduce( reducer: ReducerFn, initialValue?: ComplexLike ): Complex64 | void; /** * Reverses an array in-place. From 85c749c0b5da8e204bc3751e01203e1c972abd21 Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Sat, 27 Jan 2024 16:04:23 +0530 Subject: [PATCH 03/32] feat: apply suggestion in implementation --- .../array/complex64/docs/types/index.d.ts | 39 +++++++++++-------- .../@stdlib/array/complex64/lib/main.js | 8 ++-- 2 files changed, 26 insertions(+), 21 deletions(-) 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 c055e802e75d..48bef6f21938 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 @@ -196,48 +196,58 @@ type TernaryMapFcn = ( this: U, value: Complex64, index: number, arr: Complex type MapFcn = NullaryMapFcn | UnaryMapFcn | BinaryMapFcn | TernaryMapFcn; /** -* Reducer Function Invoked for each element in an array. +* Reducer function invoked for each element in an array. * * @returns accumulated result */ -type NullaryReducer = ( this: U ) => ComplexLike; +type NullaryReducer = ( this: U ) => V; /** -* Reducer Function Invoked for each element in an array. +* Reducer function invoked for each element in an array. * * @param acc - accumulated result * @returns accumulated result */ -type UnaryReducer = ( this: U, acc: ComplexLike ) => ComplexLike; +type UnaryReducer = ( this: U, acc: ComplexLike ) => V; /** -* Reducer Function Invoked for each element in an array. +* Reducer function invoked for each element in an array. * * @param acc - accumulated result * @param value - current array element * @returns accumulated result */ -type BinaryReducer = ( this: U, acc: ComplexLike, value: Complex64 ) => ComplexLike; +type BinaryReducer = ( this: U, acc: ComplexLike, value: Complex64 ) => V; /** -* Reducer Function Invoked for each element in an array. +* Reducer function invoked for each element in an array. * * @param acc - accumulated result * @param value - current array element * @param index - current array element index * @returns accumulated result */ -type TernaryReducer = ( this: U, acc: ComplexLike, value: Complex64, index: number ) => ComplexLike; +type TernaryReducer = ( this: U, acc: ComplexLike, value: Complex64, index: number ) => V; /** -* Reducer Function Invoked for each element in an array. +* @param acc - accumulated result +* @param value - current array element +* @param index - current array element index +* @param arr - array on which the method was called +* @returns accumulated result +*/ +type QuaternaryReducer = ( this: U, acc: ComplexLike, value: Complex64, index: number, arr: Complex64Array ) => V; + +/** +* Reducer function invoked for each element in an array. * * @param acc - accumulated result * @param value - current array element * @param index - current array element index +* @param arr - array on which the method was called * @returns accumulated result */ -type ReducerFn = NullaryReducer | UnaryReducer | BinaryReducer | TernaryReducer; +type ReducerFn = NullaryReducer | UnaryReducer | BinaryReducer | TernaryReducer | QuaternaryReducer; /** * Class for creating a 64-bit complex number array. @@ -867,19 +877,16 @@ declare class Complex64Array implements Complex64ArrayInterface { * @example * var realf = require( '@stdlib/complex/realf' ); * var imagf = require( '@stdlib/complex/imagf' ); + * var caddf = require( '@stdlib/math/base/ops/caddf' ); * var Complex64 = require( '@stdlib/complex/float32' ); * - * function sum( acc, v ) { - * return new Complex64( realf( acc ) + realf( v ), imagf( acc ) + imagf( v ) ); - * } - * * 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.reduce( sum ); + * var z = arr.reduce( caddf ); * // returns * * var re = realf( z ); @@ -888,7 +895,7 @@ declare class Complex64Array implements Complex64ArrayInterface { * var im = imagf( z ); * // returns 6.0 */ - reduce( reducer: ReducerFn, initialValue?: ComplexLike ): Complex64 | void; + reduce( reducer: ReducerFn, initialValue?: ComplexLike ): V; /** * 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 eb842e1b9561..46c23eb5c295 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -1714,19 +1714,16 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) { * @example * var realf = require( '@stdlib/complex/realf' ); * var imagf = require( '@stdlib/complex/imagf' ); +* var caddf = require( '@stdlib/math/base/ops/caddf' ); * var Complex64 = require( '@stdlib/complex/float32' ); * -* function sum( acc, v ) { -* return new Complex64( realf( acc ) + realf( v ), imagf( acc ) + imagf( v ) ); -* } -* * 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.reduce( sum ); +* var z = arr.reduce( caddf ); * // returns * * var re = realf( z ); @@ -1751,6 +1748,7 @@ setReadOnly( Complex64Array.prototype, 'reduce', function reduce( reducer, initi buf = this._buffer; len = this._length; if ( arguments.length > 1 ) { + i = 0; acc = initialValue; } else { if ( len === 0 ) { From f9c9c5c34320687b4d114c6642b63c93338b9205 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 27 Jan 2024 23:40:31 -0800 Subject: [PATCH 04/32] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex64/docs/types/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 48bef6f21938..7794ee63689e 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 @@ -208,7 +208,7 @@ type NullaryReducer = ( this: U ) => V; * @param acc - accumulated result * @returns accumulated result */ -type UnaryReducer = ( this: U, acc: ComplexLike ) => V; +type UnaryReducer = ( this: U, acc: V ) => V; /** * Reducer function invoked for each element in an array. @@ -217,7 +217,7 @@ type UnaryReducer = ( this: U, acc: ComplexLike ) => V; * @param value - current array element * @returns accumulated result */ -type BinaryReducer = ( this: U, acc: ComplexLike, value: Complex64 ) => V; +type BinaryReducer = ( this: U, acc: V, value: Complex64 ) => V; /** * Reducer function invoked for each element in an array. @@ -227,7 +227,7 @@ type BinaryReducer = ( this: U, acc: ComplexLike, value: Complex64 ) => V; * @param index - current array element index * @returns accumulated result */ -type TernaryReducer = ( this: U, acc: ComplexLike, value: Complex64, index: number ) => V; +type TernaryReducer = ( this: U, acc: V, value: Complex64, index: number ) => V; /** * @param acc - accumulated result @@ -236,7 +236,7 @@ type TernaryReducer = ( this: U, acc: ComplexLike, value: Complex64, index * @param arr - array on which the method was called * @returns accumulated result */ -type QuaternaryReducer = ( this: U, acc: ComplexLike, value: Complex64, index: number, arr: Complex64Array ) => V; +type QuaternaryReducer = ( this: U, acc: V, value: Complex64, index: number, arr: Complex64Array ) => V; /** * Reducer function invoked for each element in an array. From 6d2ee68778a85d4c62ad473c7a921a91007f8ec4 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 27 Jan 2024 23:43:17 -0800 Subject: [PATCH 05/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7794ee63689e..722a7b891f94 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 @@ -895,7 +895,7 @@ declare class Complex64Array implements Complex64ArrayInterface { * var im = imagf( z ); * // returns 6.0 */ - reduce( reducer: ReducerFn, initialValue?: ComplexLike ): V; + reduce( reducer: ReducerFn, initialValue?: V ): V; /** * Reverses an array in-place. From 58fa1c4ff35dc49add74cb4c3a14539be723077c Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 27 Jan 2024 23:44:11 -0800 Subject: [PATCH 06/32] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex64/docs/types/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 722a7b891f94..5f7d1210a0a2 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 @@ -247,7 +247,7 @@ type QuaternaryReducer = ( this: U, acc: V, value: Complex64, index: numbe * @param arr - array on which the method was called * @returns accumulated result */ -type ReducerFn = NullaryReducer | UnaryReducer | BinaryReducer | TernaryReducer | QuaternaryReducer; +type Reducer = NullaryReducer | UnaryReducer | BinaryReducer | TernaryReducer | QuaternaryReducer; /** * Class for creating a 64-bit complex number array. @@ -895,7 +895,7 @@ declare class Complex64Array implements Complex64ArrayInterface { * var im = imagf( z ); * // returns 6.0 */ - reduce( reducer: ReducerFn, initialValue?: V ): V; + reduce( reducer: Reducer, initialValue?: V ): V; /** * Reverses an array in-place. From 53b299a2eb0eb903ef3e59a13636a7819d5aaf1b Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 27 Jan 2024 23:46:36 -0800 Subject: [PATCH 07/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5f7d1210a0a2..7df7f06aac8b 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 @@ -868,7 +868,7 @@ declare class Complex64Array implements Complex64ArrayInterface { map( fcn: MapFcn, thisArg?: ThisParameterType> ): Complex64Array; /** - * Returns value that results from running the reducer callback function to completion over the entire typed array. + * Applies a provided callback function on 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. * * @param reducer - callback function * @param initialValue - initial value From 199ab19cd04feb371b3cb4689bcdf4a9d6d3f0dc Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 27 Jan 2024 23:46:55 -0800 Subject: [PATCH 08/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7df7f06aac8b..1f0e12be149c 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 @@ -868,7 +868,7 @@ declare class Complex64Array implements Complex64ArrayInterface { map( fcn: MapFcn, thisArg?: ThisParameterType> ): Complex64Array; /** - * Applies a provided callback function on 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 preceding element and returning the accumulated result upon completion. * * @param reducer - callback function * @param initialValue - initial value From f83df655b2a1efbe88764eed0f575febfe0c68b1 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 27 Jan 2024 23:51:12 -0800 Subject: [PATCH 09/32] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex64/docs/types/index.d.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 1f0e12be149c..50cb37ad7609 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 @@ -200,7 +200,7 @@ type MapFcn = NullaryMapFcn | UnaryMapFcn | BinaryMapFcn | TernaryMa * * @returns accumulated result */ -type NullaryReducer = ( this: U ) => V; +type NullaryReducer = () => U; /** * Reducer function invoked for each element in an array. @@ -208,7 +208,7 @@ type NullaryReducer = ( this: U ) => V; * @param acc - accumulated result * @returns accumulated result */ -type UnaryReducer = ( this: U, acc: V ) => V; +type UnaryReducer = ( acc: U ) => U; /** * Reducer function invoked for each element in an array. @@ -217,7 +217,7 @@ type UnaryReducer = ( this: U, acc: V ) => V; * @param value - current array element * @returns accumulated result */ -type BinaryReducer = ( this: U, acc: V, value: Complex64 ) => V; +type BinaryReducer = ( acc: U, value: Complex64 ) => U; /** * Reducer function invoked for each element in an array. @@ -227,7 +227,7 @@ type BinaryReducer = ( this: U, acc: V, value: Complex64 ) => V; * @param index - current array element index * @returns accumulated result */ -type TernaryReducer = ( this: U, acc: V, value: Complex64, index: number ) => V; +type TernaryReducer = ( acc: U, value: Complex64, index: number ) => U; /** * @param acc - accumulated result @@ -236,7 +236,7 @@ type TernaryReducer = ( this: U, acc: V, value: Complex64, index: number ) * @param arr - array on which the method was called * @returns accumulated result */ -type QuaternaryReducer = ( this: U, acc: V, value: Complex64, index: number, arr: Complex64Array ) => V; +type QuaternaryReducer = ( acc: U, value: Complex64, index: number, arr: Complex64Array ) => U; /** * Reducer function invoked for each element in an array. @@ -247,7 +247,7 @@ type QuaternaryReducer = ( this: U, acc: V, value: Complex64, index: numbe * @param arr - array on which the method was called * @returns accumulated result */ -type Reducer = NullaryReducer | UnaryReducer | BinaryReducer | TernaryReducer | QuaternaryReducer; +type Reducer = NullaryReducer | UnaryReducer | BinaryReducer | TernaryReducer | QuaternaryReducer; /** * Class for creating a 64-bit complex number array. @@ -895,7 +895,7 @@ declare class Complex64Array implements Complex64ArrayInterface { * var im = imagf( z ); * // returns 6.0 */ - reduce( reducer: Reducer, initialValue?: V ): V; + reduce( reducer: Reducer, initialValue?: U ): U; /** * Reverses an array in-place. From e99521d2cc589a2fd08b2dd5681d7227dcbed68b Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 27 Jan 2024 23:53:42 -0800 Subject: [PATCH 10/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/lib/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index 46c23eb5c295..2a4b4a25b083 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 ) { }); /** -* Returns value that results from running the reducer callback function to completion over the entire typed array. +* 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. * * @name reduce * @memberof Complex64Array.prototype @@ -1748,8 +1748,8 @@ setReadOnly( Complex64Array.prototype, 'reduce', function reduce( reducer, initi buf = this._buffer; len = this._length; if ( arguments.length > 1 ) { - i = 0; acc = initialValue; + i = 0; } else { if ( len === 0 ) { throw new TypeError( 'invalid argument. If not provided an initial value, input array must contain at least one element.' ); From 8a0ae80c085dab033ef373a8ecba513c5f558100 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 27 Jan 2024 23:55:06 -0800 Subject: [PATCH 11/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index 2a4b4a25b083..2e74a5e7a668 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -1752,7 +1752,7 @@ setReadOnly( Complex64Array.prototype, 'reduce', function reduce( reducer, initi i = 0; } else { if ( len === 0 ) { - throw new TypeError( 'invalid argument. If not provided an initial value, input array must contain at least one element.' ); + throw new TypeError( 'invalid argument. If not provided an initial value, an array must contain at least one element.' ); } acc = getComplex64( buf, 0 ); i = 1; From d2982f1686886e0cb3934f9c733a7de4e9db2a0e Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Tue, 20 Feb 2024 17:35:40 +0530 Subject: [PATCH 12/32] feat: added benchmark and update README --- .../@stdlib/array/complex64/README.md | 57 ++++++++++ .../complex64/benchmark/benchmark.reduce.js | 58 ++++++++++ .../benchmark/benchmark.reduce.length.js | 104 ++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js create mode 100644 lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.length.js diff --git a/lib/node_modules/@stdlib/array/complex64/README.md b/lib/node_modules/@stdlib/array/complex64/README.md index 9cde5661a6a4..de5ca3a8e9b8 100644 --- a/lib/node_modules/@stdlib/array/complex64/README.md +++ b/lib/node_modules/@stdlib/array/complex64/README.md @@ -1622,6 +1622,63 @@ var count = context.count; // returns 3 ``` + + +#### Complex64Array.prototype.reduce( reducerFn\[, initialValue] ) + +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. + +```javascript +var realf = require( '@stdlib/complex/realf' ); +var imagf = require( '@stdlib/complex/imagf' ); +var caddf = require( '@stdlib/math/base/ops/caddf' ); +var Complex64 = require( '@stdlib/complex/float32' ); + +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.reduce( 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. + +```javascript +var realf = require( '@stdlib/complex/realf' ); +var imagf = require( '@stdlib/complex/imagf' ); +var caddf = require( '@stdlib/math/base/ops/caddf' ); +var Complex64 = require( '@stdlib/complex/float32' ); + +function reducer( acc, v ) { + acc += realf( v ); + acc += imagf( 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.reduce( reducer, 0 ); +// returns 12.0 +``` + #### Complex64Array.prototype.reverse() diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js new file mode 100644 index 000000000000..7e6958fc7ef5 --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 realf = require( '@stdlib/complex/realf' ); +var imagf = require( '@stdlib/complex/imagf' ); +var isComplexLike = require('@stdlib/assert/is-complex-like'); +var Complex64 = require('@stdlib/complex/float32'); +var pkg = require( './../package.json' ).name; +var Complex64Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':reduce', 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.reduce( reducer ); + 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(); + + function reducer( acc, v ) { + return new Complex64( realf(v) + realf(acc), imagf(v) + imagf(acc) ); + } +}); diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.length.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.length.js new file mode 100644 index 000000000000..1015ae333d7b --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.length.js @@ -0,0 +1,104 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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.reduce( 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+':reduce:len='+len, f ); + } +} + +main(); From bf1a24b23046a8818f9871595e7d4b61c45770a4 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 01:14:29 -0800 Subject: [PATCH 13/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/README.md b/lib/node_modules/@stdlib/array/complex64/README.md index de5ca3a8e9b8..0ac7f1b977a9 100644 --- a/lib/node_modules/@stdlib/array/complex64/README.md +++ b/lib/node_modules/@stdlib/array/complex64/README.md @@ -1657,15 +1657,14 @@ The reducer function is provided four arguments: - **index**: current array element index. - **arr**: the array on which this method was called. +By default, the function initializes the accumulated result to the first element in the array and passes the second array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument. + ```javascript var realf = require( '@stdlib/complex/realf' ); -var imagf = require( '@stdlib/complex/imagf' ); -var caddf = require( '@stdlib/math/base/ops/caddf' ); var Complex64 = require( '@stdlib/complex/float32' ); function reducer( acc, v ) { acc += realf( v ); - acc += imagf( v ); return acc; } @@ -1676,7 +1675,7 @@ arr.set( [ 2.0, 2.0 ], 1 ); arr.set( [ 3.0, 3.0 ], 2 ); var z = arr.reduce( reducer, 0 ); -// returns 12.0 +// returns 6.0 ``` From db3bd3b1866c025231c95d4d2593876228025cb2 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 01:15:30 -0800 Subject: [PATCH 14/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/README.md b/lib/node_modules/@stdlib/array/complex64/README.md index 0ac7f1b977a9..c0893ba22a12 100644 --- a/lib/node_modules/@stdlib/array/complex64/README.md +++ b/lib/node_modules/@stdlib/array/complex64/README.md @@ -1632,7 +1632,6 @@ Applies a provided callback function to each element of the array, in order, pas var realf = require( '@stdlib/complex/realf' ); var imagf = require( '@stdlib/complex/imagf' ); var caddf = require( '@stdlib/math/base/ops/caddf' ); -var Complex64 = require( '@stdlib/complex/float32' ); var arr = new Complex64Array( 3 ); @@ -1661,7 +1660,6 @@ By default, the function initializes the accumulated result to the first element ```javascript var realf = require( '@stdlib/complex/realf' ); -var Complex64 = require( '@stdlib/complex/float32' ); function reducer( acc, v ) { acc += realf( v ); From 5b08331e55b69f13434274c93544e60b6b43f873 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 01:19:31 -0800 Subject: [PATCH 15/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/README.md b/lib/node_modules/@stdlib/array/complex64/README.md index c0893ba22a12..a127736f7e6f 100644 --- a/lib/node_modules/@stdlib/array/complex64/README.md +++ b/lib/node_modules/@stdlib/array/complex64/README.md @@ -1672,7 +1672,7 @@ arr.set( [ 1.0, 1.0 ], 0 ); arr.set( [ 2.0, 2.0 ], 1 ); arr.set( [ 3.0, 3.0 ], 2 ); -var z = arr.reduce( reducer, 0 ); +var z = arr.reduce( reducer, 0.0 ); // returns 6.0 ``` From 39cabdb9f4eaba4118a3495e032a4710430a1e16 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 01:21:18 -0800 Subject: [PATCH 16/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts | 1 - 1 file changed, 1 deletion(-) 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 50cb37ad7609..b36afdf33aa6 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 @@ -878,7 +878,6 @@ declare class Complex64Array implements Complex64ArrayInterface { * var realf = require( '@stdlib/complex/realf' ); * var imagf = require( '@stdlib/complex/imagf' ); * var caddf = require( '@stdlib/math/base/ops/caddf' ); - * var Complex64 = require( '@stdlib/complex/float32' ); * * var arr = new Complex64Array( 3 ); * From c82d5f86599e648692b2758e1f906ae5b67ab84c Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 01:21:50 -0800 Subject: [PATCH 17/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/lib/main.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index 2e74a5e7a668..40a514f0654b 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -1715,7 +1715,6 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) { * var realf = require( '@stdlib/complex/realf' ); * var imagf = require( '@stdlib/complex/imagf' ); * var caddf = require( '@stdlib/math/base/ops/caddf' ); -* var Complex64 = require( '@stdlib/complex/float32' ); * * var arr = new Complex64Array( 3 ); * From 67f4dae1c0b8e60cc3a18329864139511f75623e Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 01:23:16 -0800 Subject: [PATCH 18/32] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex64/benchmark/benchmark.reduce.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js index 7e6958fc7ef5..fdeac889d14d 100644 --- a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js @@ -51,8 +51,4 @@ bench( pkg+':reduce', function benchmark( b ) { } b.pass( 'benchmark finished' ); b.end(); - - function reducer( acc, v ) { - return new Complex64( realf(v) + realf(acc), imagf(v) + imagf(acc) ); - } }); From 26abe08f18ca82443c9ea69f9699cffbde02578e Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 01:23:41 -0800 Subject: [PATCH 19/32] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex64/benchmark/benchmark.reduce.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js index fdeac889d14d..0f23209ab06a 100644 --- a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js @@ -40,7 +40,7 @@ bench( pkg+':reduce', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = arr.reduce( reducer ); + out = arr.reduce( caddf ); if ( typeof out !== 'object' ) { b.fail( 'should return an object' ); } From ad19a46bb1644eaf0eab97c65b27ded1ae0b8cdd Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 01:23:49 -0800 Subject: [PATCH 20/32] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex64/benchmark/benchmark.reduce.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js index 0f23209ab06a..2364d654198d 100644 --- a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js @@ -23,6 +23,7 @@ var bench = require( '@stdlib/bench' ); var realf = require( '@stdlib/complex/realf' ); var imagf = require( '@stdlib/complex/imagf' ); +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; From c6c12b65e41cdda8e999f68b6c6a7cb414623974 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 01:24:04 -0800 Subject: [PATCH 21/32] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex64/benchmark/benchmark.reduce.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js index 2364d654198d..e43212413fee 100644 --- a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js @@ -21,8 +21,6 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var realf = require( '@stdlib/complex/realf' ); -var imagf = require( '@stdlib/complex/imagf' ); var caddf = require( '@stdlib/math/base/ops/caddf' ); var isComplexLike = require('@stdlib/assert/is-complex-like'); var Complex64 = require('@stdlib/complex/float32'); From fdf608cebf992e72ac4fefd07f5f925b820f1077 Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Fri, 23 Feb 2024 15:27:46 +0530 Subject: [PATCH 22/32] feat: added tests --- .../array/complex64/test/test.reduce.js | 181 ++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/complex64/test/test.reduce.js diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js new file mode 100644 index 000000000000..108e756859f3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js @@ -0,0 +1,181 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 `reduce` method', function test( t ) { + t.strictEqual( hasOwnProp( Complex64Array.prototype, 'reduce' ), true, 'has property' ); + t.strictEqual( isFunction( Complex64Array.prototype.reduce ), 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.reduce( 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.reduce( value ); + }; + } +}); + +tape( 'the method throws an TypeError if provided an empty complex number array and not provided initial value', function test( t ) { + var arr; + + arr = new Complex64Array( 0 ); + t.throws( foo, TypeError, 'throws an error' ); + t.end(); + + function foo() { + arr.reduce( caddf ); + } +}); + +tape( 'the method takes initial value as the first element of the array if not provided', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex64Array( [ 1.0, -1.0, 0.0, 0.0, 0.0, 0.0 ] ); + expected = new Complex64( 1.0, -1.0 ); + actual = arr.reduce( 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 takes initial value as the second argument if provided', 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( 8.0, -8.0 ); + actual = arr.reduce( caddf, 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(); +}); + +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.reduce( 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 uses provided reducer function', 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.reduce( reducer, 0.0 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value ) { + return acc + realf( value ); + } +}); From 1934ab4897d51c1e129351e9adf72b90a7b0f33d Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Fri, 23 Feb 2024 15:36:41 +0530 Subject: [PATCH 23/32] fix: lint error in benchmark --- .../@stdlib/array/complex64/benchmark/benchmark.reduce.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js index e43212413fee..f266762c6468 100644 --- a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.reduce.js @@ -23,7 +23,6 @@ var bench = require( '@stdlib/bench' ); 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' ); From 856fa9212887a494d877ea2fdc25614d4758da65 Mon Sep 17 00:00:00 2001 From: Athan Date: Fri, 23 Feb 2024 04:20:48 -0800 Subject: [PATCH 24/32] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex64/test/test.reduce.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js index 108e756859f3..d985184324d5 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js @@ -118,7 +118,7 @@ tape( 'the method throws an TypeError if provided an empty complex number array } }); -tape( 'the method takes initial value as the first element of the array if not provided', function test( t ) { +tape( 'the method uses the first element of the array as the initial value when an initial value is not provided', function test( t ) { var expected; var actual; var arr; @@ -133,7 +133,7 @@ tape( 'the method takes initial value as the first element of the array if not p t.end(); }); -tape( 'the method takes initial value as the second argument if provided', function test( t ) { +tape( 'the method supports providing an initial value as the second argument', function test( t ) { var expected; var actual; var arr; @@ -163,7 +163,7 @@ tape( 'the method returns the accumulated result', function test( t ) { t.end(); }); -tape( 'the method uses provided reducer function', function test( t ) { +tape( 'the method supports returning real-valued results', function test( t ) { var expected; var actual; var arr; From 8f01c36a34776c98e0a2e3c211c5184e4f7d0bfb Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Sat, 24 Feb 2024 15:40:16 +0530 Subject: [PATCH 25/32] feat: updated tests --- .../array/complex64/test/test.reduce.js | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js index d985184324d5..aaaee8004846 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js @@ -119,32 +119,64 @@ tape( 'the method throws an TypeError if provided an empty complex number array }); tape( 'the method uses the first 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 ind; - arr = new Complex64Array( [ 1.0, -1.0, 0.0, 0.0, 0.0, 0.0 ] ); - expected = new Complex64( 1.0, -1.0 ); - actual = arr.reduce( caddf ); + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + accArray = [ 1.0, -1.0, 3.0, -3.0 ]; + valueArray = [ 2.0, -2.0, 3.0, -3.0 ]; + expected = new Complex64( 6.0, -6.0 ); + actual = arr.reduce( 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' ); + + function reducer( acc, value, index ) { + ind = 2*(index-1); + t.strictEqual( instanceOf( acc, Complex64 ), true, 'provides expected value' ); + t.strictEqual( realf( acc ), accArray[ ind ], 'provides expected value' ); + t.strictEqual( imagf( acc ), accArray[ ind+1 ], 'provides expected value' ); + t.strictEqual( instanceOf( value, Complex64 ), true, 'provides expected value' ); + t.strictEqual( realf( value ), valueArray[ ind ], 'provides expected value' ); + t.strictEqual( imagf( value ), valueArray[ ind+1 ], 'provides expected value' ); + return caddf( acc, value ); + } + t.end(); }); 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; arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + accArray = [ 2.0, -2.0, 3.0, -3.0, 5.0, -5.0 ]; + valueArray = [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ]; expected = new Complex64( 8.0, -8.0 ); - actual = arr.reduce( caddf, new Complex64( 2.0, -2.0 ) ); + actual = arr.reduce( 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' ); + + function reducer( acc, value, index ) { + t.strictEqual( instanceOf( acc, Complex64 ), true, 'provides expected value' ); + t.strictEqual( realf( acc ), accArray[ 2*index ], 'provides expected value' ); + t.strictEqual( imagf( acc ), accArray[ (2*index)+1 ], 'provides expected value' ); + t.strictEqual( instanceOf( value, Complex64 ), true, 'provides expected value' ); + t.strictEqual( realf( value ), valueArray[ 2*index ], 'provides expected value' ); + t.strictEqual( imagf( value ), valueArray[ (2*index)+1 ], 'provides expected value' ); + return caddf( acc, value ); + } + t.end(); }); From f35837f83c8343d19ead6fe06f374d627ec3db85 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 24 Feb 2024 02:18:39 -0800 Subject: [PATCH 26/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/lib/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index 40a514f0654b..ab9a0fcb74c2 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -1709,6 +1709,7 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) { * @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 @@ -1751,7 +1752,7 @@ setReadOnly( Complex64Array.prototype, 'reduce', function reduce( reducer, initi i = 0; } else { if ( len === 0 ) { - throw new TypeError( 'invalid argument. If not provided an initial value, an array must contain at least one element.' ); + throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' ); } acc = getComplex64( buf, 0 ); i = 1; From 2a48fe98af488e6647c1fd7375434a4a363ff0dc Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 24 Feb 2024 02:19:03 -0800 Subject: [PATCH 27/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/test/test.reduce.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js index aaaee8004846..d4021c832f2f 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js @@ -106,7 +106,7 @@ tape( 'the method throws an error if provided a first argument which is not a fu } }); -tape( 'the method throws an TypeError if provided an empty complex number array and not provided initial value', function test( t ) { +tape( 'the method throws an error if provided an empty complex number array and not provided initial value', function test( t ) { var arr; arr = new Complex64Array( 0 ); From d087f6b2df9b361f026af1391b2633ddbb43ec2f Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 24 Feb 2024 02:20:37 -0800 Subject: [PATCH 28/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/test/test.reduce.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js index d4021c832f2f..fa40f508cc92 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js @@ -106,7 +106,7 @@ tape( 'the method throws an error if provided a first argument which is not a fu } }); -tape( 'the method throws an error if provided an empty complex number array and not provided initial value', function test( t ) { +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 ); From f5ff8a771b40cd61293c2b91b493b75b4f5b6f43 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 24 Feb 2024 02:22:13 -0800 Subject: [PATCH 29/32] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex64/test/test.reduce.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js index fa40f508cc92..aee6649a5029 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js @@ -138,12 +138,12 @@ tape( 'the method uses the first element of the array as the initial value when function reducer( acc, value, index ) { ind = 2*(index-1); - t.strictEqual( instanceOf( acc, Complex64 ), true, 'provides expected value' ); - t.strictEqual( realf( acc ), accArray[ ind ], 'provides expected value' ); - t.strictEqual( imagf( acc ), accArray[ ind+1 ], 'provides expected value' ); - t.strictEqual( instanceOf( value, Complex64 ), true, 'provides expected value' ); - t.strictEqual( realf( value ), valueArray[ ind ], 'provides expected value' ); - t.strictEqual( imagf( value ), valueArray[ ind+1 ], 'provides expected value' ); + 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 ); } From be872b38029309e04a29f44dddd2e8d36db24066 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 24 Feb 2024 02:23:58 -0800 Subject: [PATCH 30/32] Apply suggestions from code review Signed-off-by: Athan --- .../array/complex64/test/test.reduce.js | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js index aee6649a5029..0bb38c658c7f 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js @@ -136,6 +136,8 @@ tape( 'the method uses the first element of the array as the initial value when 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 ) { ind = 2*(index-1); t.strictEqual( instanceOf( acc, Complex64 ), true, 'returns expected value' ); @@ -146,8 +148,6 @@ tape( 'the method uses the first element of the array as the initial value when t.strictEqual( imagf( value ), valueArray[ ind+1 ], 'returns expected value' ); return caddf( acc, value ); } - - t.end(); }); tape( 'the method supports providing an initial value as the second argument', function test( t ) { @@ -167,17 +167,17 @@ tape( 'the method supports providing an initial value as the second argument', f 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 ) { - t.strictEqual( instanceOf( acc, Complex64 ), true, 'provides expected value' ); - t.strictEqual( realf( acc ), accArray[ 2*index ], 'provides expected value' ); - t.strictEqual( imagf( acc ), accArray[ (2*index)+1 ], 'provides expected value' ); - t.strictEqual( instanceOf( value, Complex64 ), true, 'provides expected value' ); - t.strictEqual( realf( value ), valueArray[ 2*index ], 'provides expected value' ); - t.strictEqual( imagf( value ), valueArray[ (2*index)+1 ], 'provides expected value' ); + t.strictEqual( instanceOf( acc, Complex64 ), true, 'returns expected value' ); + t.strictEqual( realf( acc ), accArray[ 2*index ], 'returns expected value' ); + t.strictEqual( imagf( acc ), accArray[ (2*index)+1 ], 'returns expected value' ); + t.strictEqual( instanceOf( value, Complex64 ), true, 'returns expected value' ); + t.strictEqual( realf( value ), valueArray[ 2*index ], 'returns expected value' ); + t.strictEqual( imagf( value ), valueArray[ (2*index)+1 ], 'returns expected value' ); return caddf( acc, value ); } - - t.end(); }); tape( 'the method returns the accumulated result', function test( t ) { From 9529b8e15a7b846327d0e706c14e0b039dcbe003 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 24 Feb 2024 02:25:48 -0800 Subject: [PATCH 31/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/test/test.reduce.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js index 0bb38c658c7f..10fa59a785d6 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js @@ -124,7 +124,6 @@ tape( 'the method uses the first element of the array as the initial value when var expected; var actual; var arr; - var ind; arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); accArray = [ 1.0, -1.0, 3.0, -3.0 ]; @@ -139,7 +138,7 @@ tape( 'the method uses the first element of the array as the initial value when t.end(); function reducer( acc, value, index ) { - ind = 2*(index-1); + var ind = 2*(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' ); From d94a0527176f46acc617f68dd1a68b094b93ce56 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 24 Feb 2024 02:27:42 -0800 Subject: [PATCH 32/32] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex64/test/test.reduce.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js index 10fa59a785d6..30b1ed095a1a 100644 --- a/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js +++ b/lib/node_modules/@stdlib/array/complex64/test/test.reduce.js @@ -110,7 +110,7 @@ tape( 'the method throws an error if not provided an initial value when operatin var arr; arr = new Complex64Array( 0 ); - t.throws( foo, TypeError, 'throws an error' ); + t.throws( foo, Error, 'throws an error' ); t.end(); function foo() {