From bc207b9b095c6659ec04cf18ed8c95a519ba05cf Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Thu, 11 Jan 2024 17:36:04 +0530 Subject: [PATCH 1/4] feat: add reverse method to array/complex128 --- .../@stdlib/array/complex128/README.md | 47 ++++++++ .../complex128/benchmark/benchmark.reverse.js | 51 ++++++++ .../benchmark/benchmark.reverse.length.js | 103 ++++++++++++++++ .../array/complex128/docs/types/index.d.ts | 47 ++++++++ .../@stdlib/array/complex128/lib/main.js | 75 ++++++++++++ .../array/complex128/test/test.reverse.js | 114 ++++++++++++++++++ 6 files changed, 437 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.js create mode 100644 lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.length.js create mode 100644 lib/node_modules/@stdlib/array/complex128/test/test.reverse.js diff --git a/lib/node_modules/@stdlib/array/complex128/README.md b/lib/node_modules/@stdlib/array/complex128/README.md index b1588258c0ac..ad144a24b676 100644 --- a/lib/node_modules/@stdlib/array/complex128/README.md +++ b/lib/node_modules/@stdlib/array/complex128/README.md @@ -1620,6 +1620,53 @@ var count = context.count; // returns 3 ``` + + +#### Complex128Array.prototype.reverse() + +Reverses an array in-place. + +```javascript +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); + +var arr = new Complex128Array( 3 ); + +arr.set( [ 1.0, 1.0 ], 0 ); +arr.set( [ 2.0, 2.0 ], 1 ); +arr.set( [ 3.0, 3.0 ], 2 ); + +var out = arr.reverse(); +// returns + +var z = out.get( 0 ); +// returns + +var re = real( z ); +// returns 3.0 + +var im = imag( z ); +// returns 3.0 + +z = out.get( 1 ); +// returns + +re = real( z ); +// returns 2.0 + +im = imag( z ); +// returns 2.0 + +z = out.get( 2 ); +// returns + +re = real( z ); +// returns 1.0 + +im = imag( z ); +// returns 1.0 +``` + #### Complex128Array.prototype.set( z\[, i] ) diff --git a/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.js b/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.js new file mode 100644 index 000000000000..0ab070bb97b2 --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.js @@ -0,0 +1,51 @@ +/** +* @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 isComplex128Array = require( '@stdlib/assert/is-complex128array' ); +var pkg = require( './../package.json' ).name; +var Complex128Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':reverse', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.reverse(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isComplex128Array( out ) ) { + b.fail( 'should return a Complex64Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.length.js b/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.length.js new file mode 100644 index 000000000000..14bee87b9074 --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.length.js @@ -0,0 +1,103 @@ +/** +* @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 isComplex128Array = require( '@stdlib/assert/is-complex128array' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var Complex128 = require( '@stdlib/complex/float32' ); +var pkg = require( './../package.json' ).name; +var Complex128Array = 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 Complex128( i, i ) ); + } + arr = new Complex128Array( 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.reverse(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isComplex128Array( out ) ) { + b.fail( 'should return a Complex64Array' ); + } + 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+':reverse:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts index 97fd9917f5ad..b2322dfc4072 100644 --- a/lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts @@ -811,6 +811,53 @@ declare class Complex128Array implements Complex128ArrayInterface { */ map( fcn: MapFcn, thisArg?: ThisParameterType> ): Complex128Array; + /** + * Reverses an array in-place. + * + * @returns reversed array + * + * @example + * var real = require( '@stdlib/complex/real' ); + * var imag = require( '@stdlib/complex/imag' ); + * + * var arr = new Complex128Array( 3 ); + * + * arr.set( [ 1.0, 1.0 ], 0 ); + * arr.set( [ 2.0, 2.0 ], 1 ); + * arr.set( [ 3.0, 3.0 ], 2 ); + * + * var out = arr.reverse(); + * // returns + * + * var z = out.get( 0 ); + * // returns + * + * var re = real( z ); + * // returns 3.0 + * + * var im = imag( z ); + * // returns 3.0 + * + * z = out.get( 1 ); + * // returns + * + * re = real( z ); + * // returns 2.0 + * + * im = imag( z ); + * // returns 2.0 + * + * z = out.get( 2 ); + * // returns + * + * re = real( z ); + * // returns 1.0 + * + * im = imag( z ); + * // returns 1.0 + */ + reverse(): Complex128Array; + /** * Sets an array element. * diff --git a/lib/node_modules/@stdlib/array/complex128/lib/main.js b/lib/node_modules/@stdlib/array/complex128/lib/main.js index fa4de92e3686..799521424699 100644 --- a/lib/node_modules/@stdlib/array/complex128/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex128/lib/main.js @@ -41,6 +41,7 @@ var Float64Array = require( '@stdlib/array/float64' ); var Complex128 = require( '@stdlib/complex/float64' ); var real = require( '@stdlib/complex/real' ); var imag = require( '@stdlib/complex/imag' ); +var floor = require( '@stdlib/math/base/special/floor' ); var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' ); var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); var getter = require( '@stdlib/array/base/getter' ); @@ -1725,6 +1726,80 @@ setReadOnly( Complex128Array.prototype, 'map', function map( fcn, thisArg ) { return out; }); +/** +* Reverses an array in-place. +* +* @name reverse +* @memberof Complex128Array.prototype +* @type {Function} +* @throws {TypeError} `this` must be a complex number array +* @returns {Complex64Array} reversed array +* +* @example +* var real = require( '@stdlib/complex/real' ); +* var imag = require( '@stdlib/complex/imag' ); +* +* var arr = new Complex128Array( 3 ); +* +* arr.set( [ 1.0, 1.0 ], 0 ); +* arr.set( [ 2.0, 2.0 ], 1 ); +* arr.set( [ 3.0, 3.0 ], 2 ); +* +* var out = arr.reverse(); +* // returns +* +* var z = out.get( 0 ); +* // returns +* +* var re = real( z ); +* // returns 3.0 +* +* var im = imag( z ); +* // returns 3.0 +* +* z = out.get( 1 ); +* // returns +* +* re = real( z ); +* // returns 2.0 +* +* im = imag( z ); +* // returns 2.0 +* +* z = out.get( 2 ); +* // returns +* +* re = real( z ); +* // returns 1.0 +* +* im = imag( z ); +* // returns 1.0 +*/ +setReadOnly( Complex128Array.prototype, 'reverse', function reverse() { + var buf; + var tmp; + var len; + var N; + var i; + var j; + if ( !isComplexArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); + } + len = this._length; + buf = this._buffer; + N = floor( len / 2 ); + for ( i = 0; i < N; i++ ) { + j = len - i - 1; + tmp = buf[ (2*i) ]; + buf[ (2*i) ] = buf[ (2*j) ]; + buf[ (2*j) ] = tmp; + tmp = buf[ (2*i)+1 ]; + buf[ (2*i)+1 ] = buf[ (2*j)+1 ]; + buf[ (2*j)+1 ] = tmp; + } + return this; +}); + /** * Sets an array element. * diff --git a/lib/node_modules/@stdlib/array/complex128/test/test.reverse.js b/lib/node_modules/@stdlib/array/complex128/test/test.reverse.js new file mode 100644 index 000000000000..de6465150728 --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex128/test/test.reverse.js @@ -0,0 +1,114 @@ +/** +* @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 reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex128Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Complex128Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `reverse` method', function test( t ) { + t.strictEqual( hasOwnProp( Complex128Array.prototype, 'reverse' ), true, 'has property' ); + t.strictEqual( isFunction( Complex128Array.prototype.reverse ), 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 Complex128Array( 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.reverse.call( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty complex number array', function test( t ) { + var arr; + var out; + + arr = new Complex128Array(); + out = arr.reverse(); + + t.strictEqual( out.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method reverses elements of a complex number array in-place', function test( t ) { + var expected; + var arr; + var out; + + arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + expected = new Float64Array( [ 3.0, -3.0, 2.0, -2.0, 1.0, -1.0 ] ); + out = arr.reverse(); + + t.strictEqual( instanceOf( arr, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( out, arr, 'returns expected value' ); + t.strictEqual( arr.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( arr, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method does not change the array length', function test( t ) { + var arr; + var out; + + arr = new Complex128Array( 10 ); + out = arr.reverse(); + + t.strictEqual( out.length, 10, 'returns expected value' ); + t.end(); +}); From 31df7b7abc6bfd07e50d84a53607de0dbf21acfc Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 11 Jan 2024 23:51:45 -0800 Subject: [PATCH 2/4] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/complex128/benchmark/benchmark.reverse.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.js b/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.js index 0ab070bb97b2..fa2731237189 100644 --- a/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.js +++ b/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.js @@ -44,7 +44,7 @@ bench( pkg+':reverse', function benchmark( b ) { } b.toc(); if ( !isComplex128Array( out ) ) { - b.fail( 'should return a Complex64Array' ); + b.fail( 'should return a Complex128Array' ); } b.pass( 'benchmark finished' ); b.end(); From 3c3768dd959c977065b924d1d0a329e1e4306ef4 Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 11 Jan 2024 23:52:40 -0800 Subject: [PATCH 3/4] Apply suggestions from code review Signed-off-by: Athan --- .../array/complex128/benchmark/benchmark.reverse.length.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.length.js b/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.length.js index 14bee87b9074..ad21fae79bad 100644 --- a/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.length.js +++ b/lib/node_modules/@stdlib/array/complex128/benchmark/benchmark.reverse.length.js @@ -23,7 +23,7 @@ var bench = require( '@stdlib/bench' ); var isComplex128Array = require( '@stdlib/assert/is-complex128array' ); var pow = require( '@stdlib/math/base/special/pow' ); -var Complex128 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); var pkg = require( './../package.json' ).name; var Complex128Array = require( './../lib' ); @@ -68,7 +68,7 @@ function createBenchmark( len ) { } b.toc(); if ( !isComplex128Array( out ) ) { - b.fail( 'should return a Complex64Array' ); + b.fail( 'should return a Complex128Array' ); } b.pass( 'benchmark finished' ); b.end(); From 86bb6b4b37f621d97f9440b3de29d8bec937fa8b Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 11 Jan 2024 23:53:54 -0800 Subject: [PATCH 4/4] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/complex128/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/complex128/lib/main.js b/lib/node_modules/@stdlib/array/complex128/lib/main.js index 799521424699..b998e5b6db64 100644 --- a/lib/node_modules/@stdlib/array/complex128/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex128/lib/main.js @@ -1733,7 +1733,7 @@ setReadOnly( Complex128Array.prototype, 'map', function map( fcn, thisArg ) { * @memberof Complex128Array.prototype * @type {Function} * @throws {TypeError} `this` must be a complex number array -* @returns {Complex64Array} reversed array +* @returns {Complex128Array} reversed array * * @example * var real = require( '@stdlib/complex/real' );