From 4dc48b96af246a0f1bcea75791a4bcf2c8fca566 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 23 Feb 2024 12:31:06 +0000 Subject: [PATCH] Auto-generated commit --- CONTRIBUTORS | 1 + complex128/README.md | 94 ++++++ complex128/benchmark/benchmark.slice.js | 51 ++++ .../benchmark/benchmark.slice.length.js | 103 +++++++ complex128/docs/types/index.d.ts | 70 +++++ complex128/lib/main.js | 130 +++++++++ complex128/test/test.slice.js | 271 ++++++++++++++++++ 7 files changed, 720 insertions(+) create mode 100644 complex128/benchmark/benchmark.slice.js create mode 100644 complex128/benchmark/benchmark.slice.length.js create mode 100644 complex128/test/test.slice.js diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 726c1ec4..4d10e1be 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -21,6 +21,7 @@ Joey Reed Jordan Gallivan <115050475+Jordan-Gallivan@users.noreply.github.com> Joris Labie Justin Dennison +Karthik Prakash <116057817+skoriop@users.noreply.github.com> Marcus Fantham Matt Cochrane Milan Raj diff --git a/complex128/README.md b/complex128/README.md index 878c6053..ec5a5f51 100644 --- a/complex128/README.md +++ b/complex128/README.md @@ -1799,6 +1799,100 @@ A few notes: - If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error. - If provided a [typed array][@stdlib/array/typed] which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range. + + +#### Complex128Array.prototype.slice( \[start\[, end]] ) + +Copies a portion of a typed array to a new typed array. + +```javascript +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); + +var arr = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + +var out = arr.slice(); +// returns + +var len = out.length; +// returns 4 + +var z = out.get( 0 ); +// returns + +var re = real( z ); +// returns 1.0 + +var im = imag( z ); +// returns 2.0 + +z = out.get( len-1 ); +// returns + +re = real( z ); +// returns 7.0 + +im = imag( z ); +// returns 8.0 +``` + +By default, the method returns a typed array beginning with the first array element. To specify an alternative array index at which to begin, provide a `start` index (inclusive). + +```javascript +var imag = require( '@stdlib/complex/imag' ); +var real = require( '@stdlib/complex/real' ); + +var arr = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + +var out = arr.slice( 1 ); +// returns + +var len = out.length; +// returns 3 + +var z = out.get( 0 ); +// returns + +var re = real( z ); +// returns 3.0 + +var im = imag( z ); +// returns 4.0 +``` + +By default, the method returns a typed array which includes all array elements after `start`. To limit the number of array elements after `start`, provide an `end` index (exclusive). + +```javascript +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); + +var arr = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + +var out = arr.slice( 1, -1 ); +// returns + +var len = out.length; +// returns 2 + +var z = out.get( 0 ); +// returns + +var re = real( z ); +// returns 3.0 + +var im = imag( z ); +// returns 4.0 + +z = out.get( len-1 ); +// returns + +re = real( z ); +// returns 5.0 + +im = imag( z ); +// returns 6.0 +``` + #### Complex128Array.prototype.some( predicate\[, thisArg] ) diff --git a/complex128/benchmark/benchmark.slice.js b/complex128/benchmark/benchmark.slice.js new file mode 100644 index 00000000..5f8a43bc --- /dev/null +++ b/complex128/benchmark/benchmark.slice.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+':slice', 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.slice(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isComplex128Array( out ) ) { + b.fail( 'should return a Complex128Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/complex128/benchmark/benchmark.slice.length.js b/complex128/benchmark/benchmark.slice.length.js new file mode 100644 index 00000000..83b073f2 --- /dev/null +++ b/complex128/benchmark/benchmark.slice.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/float64' ); +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.slice(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isComplex128Array( out ) ) { + b.fail( 'should return a Complex128Array' ); + } + 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+':slice:len='+len, f ); + } +} + +main(); diff --git a/complex128/docs/types/index.d.ts b/complex128/docs/types/index.d.ts index 5a757305..55491c41 100644 --- a/complex128/docs/types/index.d.ts +++ b/complex128/docs/types/index.d.ts @@ -917,6 +917,76 @@ declare class Complex128Array implements Complex128ArrayInterface { */ set( value: ArrayLike | RealOrComplexTypedArray | ComplexLike, i?: number ): void; + /** + * Copies a portion of a typed array to a new typed array. + * + * @param start - starting index (inclusive) + * @param end - ending index (exclusive) + * @throws indices must be integers + * @returns output array + * + * @example + * var real = require( '@stdlib/complex/real' ); + * var imag = require( '@stdlib/complex/imag' ); + * + * var arr = new Complex128Array( 5 ); + * + * arr.set( [ 1.0, -1.0 ], 0 ); + * arr.set( [ 2.0, -2.0 ], 1 ); + * arr.set( [ 3.0, -3.0 ], 2 ); + * arr.set( [ 4.0, -4.0 ], 3 ); + * arr.set( [ 5.0, -5.0 ], 4 ); + * + * var out = arr.slice(); + * // returns + * + * var len = out.length; + * // returns 5 + * + * var z = out.get( 0 ); + * // returns + * + * var re = real( z ); + * // returns 1.0 + * + * var im = imag( z ); + * // returns -1.0 + * + * z = out.get( len-1 ); + * // returns + * + * re = real( z ); + * // returns 5.0 + * + * im = imag( z ); + * // returns -5.0 + * + * out = arr.slice( 1, -2 ); + * // returns + * + * len = out.length; + * // returns 2 + * + * z = out.get( 0 ); + * // returns + * + * re = real( z ); + * // returns 2.0 + * + * im = imag( z ); + * // returns -2.0 + * + * z = out.get( len-1 ); + * // returns + * + * re = real( z ); + * // returns 3.0 + * + * im = imag( z ); + * // returns -3.0 + */ + slice( start?: number, end?: number ): Complex128Array; + /** * Tests whether at least one element in an array passes a test implemented by a predicate function. * diff --git a/complex128/lib/main.js b/complex128/lib/main.js index eff3bed5..f3468c1d 100644 --- a/complex128/lib/main.js +++ b/complex128/lib/main.js @@ -1961,6 +1961,136 @@ setReadOnly( Complex128Array.prototype, 'set', function set( value ) { /* eslint-enable no-underscore-dangle */ }); +/** +* Copies a portion of a typed array to a new typed array. +* +* @name slice +* @memberof Complex128Array.prototype +* @type {Function} +* @param {integer} [start=0] - starting index (inclusive) +* @param {integer} [end] - ending index (exclusive) +* @throws {TypeError} `this` must be a complex number array +* @throws {TypeError} first argument must be an integer +* @throws {TypeError} second argument must be an integer +* @returns {Complex128Array} complex number array +* +* @example +* var real = require( '@stdlib/complex/real' ); +* var imag = require( '@stdlib/complex/imag' ); +* +* var arr = new Complex128Array( 5 ); +* +* arr.set( [ 1.0, -1.0 ], 0 ); +* arr.set( [ 2.0, -2.0 ], 1 ); +* arr.set( [ 3.0, -3.0 ], 2 ); +* arr.set( [ 4.0, -4.0 ], 3 ); +* arr.set( [ 5.0, -5.0 ], 4 ); +* +* var out = arr.slice(); +* // returns +* +* var len = out.length; +* // returns 5 +* +* var z = out.get( 0 ); +* // returns +* +* var re = real( z ); +* // returns 1.0 +* +* var im = imag( z ); +* // returns -1.0 +* +* z = out.get( len-1 ); +* // returns +* +* re = real( z ); +* // returns 5.0 +* +* im = imag( z ); +* // returns -5.0 +* +* out = arr.slice( 1, -2 ); +* // returns +* +* len = out.length; +* // returns 2 +* +* z = out.get( 0 ); +* // returns +* +* re = real( z ); +* // returns 2.0 +* +* im = imag( z ); +* // returns -2.0 +* +* z = out.get( len-1 ); +* // returns +* +* re = real( z ); +* // returns 3.0 +* +* im = imag( z ); +* // returns -3.0 +*/ +setReadOnly( Complex128Array.prototype, 'slice', function slice( start, end ) { + var outlen; + var outbuf; + var out; + var idx; + var buf; + var len; + var i; + if ( !isComplexArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length === 0 ) { + start = 0; + end = len; + } else { + if ( !isInteger( start ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', start ) ); + } + if ( start < 0 ) { + start += len; + if ( start < 0 ) { + start = 0; + } + } + if ( arguments.length === 1 ) { + end = len; + } else { + if ( !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) ); + } + if ( end < 0 ) { + end += len; + if ( end < 0 ) { + end = 0; + } + } else if ( end > len ) { + end = len; + } + } + } + if ( start < end ) { + outlen = end - start; + } else { + outlen = 0; + } + out = new this.constructor( outlen ); + outbuf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < outlen; i++ ) { + idx = 2*(i+start); + outbuf[ 2*i ] = buf[ idx ]; + outbuf[ (2*i)+1 ] = buf[ idx+1 ]; + } + return out; +}); + /** * Tests whether at least one element in an array passes a test implemented by a predicate function. * diff --git a/complex128/test/test.slice.js b/complex128/test/test.slice.js new file mode 100644 index 00000000..c8d3dcbc --- /dev/null +++ b/complex128/test/test.slice.js @@ -0,0 +1,271 @@ +/** +* @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( './../../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 `slice` method', function test( t ) { + t.strictEqual( hasOwnProp( Complex128Array.prototype, 'slice' ), true, 'has property' ); + t.strictEqual( isFunction( Complex128Array.prototype.slice ), 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.slice.call( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Complex128Array( 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.slice( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Complex128Array( 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.slice( 0, value ); + }; + } +}); + +tape( 'the method returns an empty typed array if operating on an empty complex number array', function test( t ) { + var arr; + var out; + + arr = new Complex128Array(); + out = arr.slice(); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'if called without arguments, the method returns a typed array containing the same elements as the original array', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + expected = new Float64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + actual = arr.slice(); + + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.notEqual( actual, arr, 'returns a new instance' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if called with one argument, the method returns a typed array containing elements starting from a specified beginning index (inclusive)', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] ); + expected = new Float64Array( [ 2.0, -2.0, 3.0, -3.0 ] ); + actual = arr.slice( 1 ); + + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided two arguments, the method returns a typed array containing elements starting from a specified beginning index (inclusive) and ending at a specified stop index (exclusive)', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ] ); + expected = new Float64Array( [ 2.0, -2.0, 3.0, -3.0 ] ); + actual = arr.slice( 1, 3 ); + + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + + arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ] ); + expected = new Float64Array( [ 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ] ); + actual = arr.slice( 1, 30 ); + + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method resolves negative indices relative to the last element', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ] ); + + expected = new Float64Array( [ 2.0, -2.0, 3.0, -3.0 ] ); + actual = arr.slice( -3, -1 ); + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + + expected = new Float64Array( [ 1.0, -1.0, 2.0, -2.0 ] ); + actual = arr.slice( -30, -2 ); + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty typed array if a resolved beginning index exceeds a resolved ending index', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ] ); + expected = new Float64Array( [] ); + actual = arr.slice( 2, 0 ); + + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty typed array if a resolved beginning index exceeds the maximum array index', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ] ); + expected = new Float64Array( [] ); + actual = arr.slice( 5 ); + + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty typed array if a resolved ending index is less than or equal to zero', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ] ); + expected = new Float64Array( [] ); + + actual = arr.slice( 2, -8 ); + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + + actual = arr.slice( 1, 0 ); + t.strictEqual( instanceOf( actual, Complex128Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' ); + t.end(); +});