Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Jan 2, 2024
1 parent f615c8a commit 8b4c8dc
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 4 deletions.
30 changes: 30 additions & 0 deletions complex128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,36 @@ idx = arr.indexOf( new Complex128( 1.0, -1.0 ), 1 );
// returns -1
```

<a name="method-join"></a>

#### Complex128Array.prototype.join( \[separator] )

Returns a new string by concatenating all array elements.

```javascript
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 str = arr.join();
// returns '1 + 1i,2 - 2i,3 + 3i'
```

By default, the method separates serialized array elements with a comma. To use an alternative separator, provide a `separator` string.

```javascript
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 str = arr.join( '/' );
// returns '1 + 1i/2 - 2i/3 + 3i'
```

<a name="method-last-index-of"></a>

#### Complex128Array.prototype.lastIndexOf( searchElement\[, fromIndex] )
Expand Down
50 changes: 50 additions & 0 deletions complex128/benchmark/benchmark.join.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @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 pkg = require( './../package.json' ).name;
var Complex128Array = require( './../lib' );


// MAIN //

bench( pkg+':join', 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.join();
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();
});
102 changes: 102 additions & 0 deletions complex128/benchmark/benchmark.join.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @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 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.join( '/' );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
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+':join:len='+len, f );
}
}

main();
20 changes: 20 additions & 0 deletions complex128/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,26 @@ declare class Complex128Array implements Complex128ArrayInterface {
*/
indexOf( searchElement: ComplexLike, fromIndex?: number ): number;

/**
* Returns a new string by concatenating all array elements.
*
* @param separator - value separator (default: ',')
* @returns string
*
* @example
* var arr = new Complex128Array( 2 );
*
* arr.set( [ 1.0, 1.0 ], 0 );
* arr.set( [ 2.0, 2.0 ], 1 );
*
* var str = arr.join();
* // returns '1 + 1i,2 + 2i'
*
* str = arr.join( '/' );
* // returns '1 + 1i/2 + 2i'
*/
join( separator?: string ): string;

/**
* Returns the last index at which a given element can be found.
*
Expand Down
47 changes: 47 additions & 0 deletions complex128/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var isCollection = require( '@stdlib/assert/is-collection' );
var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
var isObject = require( '@stdlib/assert/is-object' );
var isArray = require( '@stdlib/assert/is-array' );
var isString = require( '@stdlib/assert/is-string' );
var isFunction = require( '@stdlib/assert/is-function' );
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
var isEven = require( '@stdlib/math/base/assert/is-even' );
Expand Down Expand Up @@ -1537,6 +1538,52 @@ setReadOnly( Complex128Array.prototype, 'indexOf', function indexOf( searchEleme
return -1;
});

/**
* Returns a new string by concatenating all array elements.
*
* @name join
* @memberof Complex128Array.prototype
* @type {Function}
* @param {string} [separator=','] - element separator
* @throws {TypeError} `this` must be a complex number array
* @throws {TypeError} first argument must be a string
* @returns {string} string representation
*
* @example
* var arr = new Complex128Array( 2 );
*
* arr.set( [ 1.0, 1.0 ], 0 );
* arr.set( [ 2.0, 2.0 ], 1 );
*
* var str = arr.join();
* // returns '1 + 1i,2 + 2i'
*
* str = arr.join( '/' );
* // returns '1 + 1i/2 + 2i'
*/
setReadOnly( Complex128Array.prototype, 'join', function join( separator ) {
var out;
var buf;
var sep;
var i;
if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
if ( arguments.length === 0 ) {
sep = ',';
} else if ( isString( separator ) ) {
sep = separator;
} else {
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', separator ) );
}
out = [];
buf = this._buffer;
for ( i = 0; i < this._length; i++ ) {
out.push( getComplex128( buf, i ).toString() );
}
return out.join( sep );
});

/**
* Returns the last index at which a given element can be found.
*
Expand Down
Loading

0 comments on commit 8b4c8dc

Please sign in to comment.