Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions lib/node_modules/@stdlib/array/complex128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,72 @@ var count = context.count;
// returns 3
```

<a name="method-for-each"></a>

#### Complex128Array.prototype.forEach( callbackFn\[, thisArg] )

Invokes a function once for each array element.

```javascript
var Complex128 = require( '@stdlib/complex/float64' );

function log( v, i ) {
console.log( '%s: %s', i, v.toString() );
}

var arr = new Complex128Array( 3 );

// Set the first three elements:
arr.set( [ 1.0, 1.0 ], 0 );
arr.set( [ 2.0, 2.0 ], 1 );
arr.set( [ 3.0, 3.0 ], 2 );

arr.forEach( log );
/* =>
0: 1 + 1i
1: 2 + 2i
2: 3 + 3i
*/
```

The invoked function is provided three arguments:

- **value**: current array element.
- **index**: current array element index.
- **arr**: the array on which this method was called.

To set the function execution context, provide a `thisArg`.

```javascript
var Complex128 = require( '@stdlib/complex/float64' );

function fcn( v, i ) {
this.count += 1;
console.log( '%s: %s', i, v.toString() );
}

var arr = new Complex128Array( 3 );

var context = {
'count': 0
};

// Set the first three elements:
arr.set( [ 1.0, -1.0 ], 0 );
arr.set( [ 2.0, -2.0 ], 1 );
arr.set( [ 3.0, -3.0 ], 2 );

arr.forEach( fcn, context );
/* =>
0: 1 + 1i
1: 2 + 2i
2: 3 + 3i
*/

var count = context.count;
// returns 3
```

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

#### Complex128Array.prototype.get( i )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @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 isComplex128 = require( '@stdlib/assert/is-complex128' );
var pkg = require( './../package.json' ).name;
var Complex128Array = require( './../lib' );


// MAIN //

bench( pkg+':forEach', function benchmark( b ) {
var arr;
var i;

arr = new Complex128Array( [ 1, 2, 3, 4 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr.forEach( check );
if ( arr.length !== 2 ) {
b.fail( 'should not change an array length' );
}
}
b.toc();
if ( arr.length !== 2 ) {
b.fail( 'should not change an array length' );
}
b.pass( 'benchmark finished' );
b.end();

function check( v ) {
if ( !isComplex128( v ) ) {
b.fail( 'should be a complex number' );
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @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 real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );
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 i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr.forEach( callback );
if ( arr.length !== len ) {
b.fail( 'should not change an array length' );
}
}
b.toc();
if ( arr.length !== len ) {
b.fail( 'should not change an array length' );
}
b.pass( 'benchmark finished' );
b.end();

function callback( value ) {
if ( real( value ) !== imag( value ) ) {
throw new Error( 'something went wrong' );
}
}
}
}


// 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+':forEach:len='+len, f );
}
}

main();
71 changes: 71 additions & 0 deletions lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,50 @@ type TernaryPredicate<U> = ( this: U, value: Complex128, index: number, arr: Com
*/
type Predicate<U> = NullaryPredicate<U> | UnaryPredicate<U> | BinaryPredicate<U> | TernaryPredicate<U>;

/**
* Callback invoked for each element in an array.
*
* @returns undefined
*/
type NullaryCallback<U> = ( this: U ) => void;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @returns undefined
*/
type UnaryCallback<U> = ( this: U, value: Complex128 ) => void;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @param index - current array element index
* @returns undefined
*/
type BinaryCallback<U> = ( this: U, value: Complex128, index: number ) => void;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @param index - current array element index
* @param arr - array on which the method was called
* @returns undefined
*/
type TernaryCallback<U> = ( this: U, value: Complex128, index: number, arr: Complex128Array ) => void;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @param index - current array element index
* @param arr - array on which the method was called
* @returns undefined
*/
type Callback<U> = NullaryCallback<U> | UnaryCallback<U> | BinaryCallback<U> | TernaryCallback<U>;

/**
* Callback invoked for each element in an array.
*
Expand Down Expand Up @@ -543,6 +587,33 @@ declare class Complex128Array implements Complex128ArrayInterface {
*/
findLastIndex<U = unknown>( predicate: Predicate<U>, thisArg?: ThisParameterType<Predicate<U>> ): number;

/**
* Invokes a function once for each array element.
*
* @param fcn - function to invoke
* @param thisArg - execution context
* @returns undefined
*
* @example
* var Complex128 = require( '@stdlib/complex/float64' );
*
* function log( v, i ) {
* console.log( '%s: %s', i, v.toString() );
* }
*
* 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 );
*
* arr.forEach( log );
* // => 0: 1 + 1i
* // => 1: 2 + 2i
* // => 2: 3 + 3i
*/
forEach<U = unknown>( fcn: Callback<U>, thisArg?: ThisParameterType<Callback<U>> ): void;

/**
* Returns an array element.
*
Expand Down
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/array/complex128/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,49 @@ setReadOnly( Complex128Array.prototype, 'findLastIndex', function findLastIndex(
return -1;
});

/**
* Invokes a function once for each array element.
*
* @name forEach
* @memberof Complex128Array.prototype
* @type {Function}
* @param {Function} fcn - function to invoke
* @param {*} [thisArg] - function invocation context
* @throws {TypeError} `this` must be a complex number array
* @throws {TypeError} first argument must be a function
*
* @example
* var Complex128 = require( '@stdlib/complex/float64' );
*
* function log( v, i ) {
* console.log( '%s: %s', i, v.toString() );
* }
*
* 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 );
*
* arr.forEach( log );
*/
setReadOnly( Complex128Array.prototype, 'forEach', function forEach( fcn, thisArg ) {
var buf;
var i;
var z;
if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
if ( !isFunction( fcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
}
buf = this._buffer;
for ( i = 0; i < this._length; i++ ) {
z = getComplex128( buf, i );
fcn.call( thisArg, z, i, this );
}
});

/**
* Returns an array element.
*
Expand Down
Loading