Skip to content

Commit 401a602

Browse files
committed
Auto-generated commit
1 parent 70cf7e2 commit 401a602

File tree

9 files changed

+619
-5
lines changed

9 files changed

+619
-5
lines changed

README.md

+72-1
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ var count = context.count;
12301230

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

1233-
#### Complex64Array.prototype.forEach( predicate\[, thisArg] )
1233+
#### Complex64Array.prototype.forEach( callbackFn\[, thisArg] )
12341234

12351235
Invokes a function once for each array element.
12361236

@@ -1445,6 +1445,77 @@ idx = arr.lastIndexOf( new Complex64( 2.0, -2.0 ), 0 );
14451445
// returns -1
14461446
```
14471447

1448+
<a name="method-map"></a>
1449+
1450+
#### Complex64Array.prototype.map( callbackFn\[, thisArg] )
1451+
1452+
Returns a new array with each element being the result of the callback function.
1453+
1454+
```javascript
1455+
var Complex64 = require( '@stdlib/complex-float32' );
1456+
var realf = require( '@stdlib/complex-realf' );
1457+
var imagf = require( '@stdlib/complex-imagf' );
1458+
1459+
function scale( v ) {
1460+
return new Complex64( 2.0*realf( v ), 2.0*imagf( v ) );
1461+
}
1462+
1463+
var arr = new Complex64Array( 3 );
1464+
1465+
// Set the first three elements:
1466+
arr.set( [ 1.0, -1.0 ], 0 );
1467+
arr.set( [ 2.0, -2.0 ], 1 );
1468+
arr.set( [ 3.0, -3.0 ], 2 );
1469+
1470+
var out = arr.map( scale );
1471+
// returns <Complex64Array>
1472+
1473+
var z = out.get( 0 );
1474+
// returns <complex64>
1475+
1476+
var re = realf( z );
1477+
// returns 2.0
1478+
1479+
var im = imagf( z );
1480+
// returns -2.0
1481+
```
1482+
1483+
The callback function is provided three arguments:
1484+
1485+
- **value**: current array element.
1486+
- **index**: current array element index.
1487+
- **arr**: the array on which this method was called.
1488+
1489+
To set the function execution context, provide a `thisArg`.
1490+
1491+
```javascript
1492+
var Complex64 = require( '@stdlib/complex-float32' );
1493+
var realf = require( '@stdlib/complex-realf' );
1494+
var imagf = require( '@stdlib/complex-imagf' );
1495+
1496+
function scale( v ) {
1497+
this.count += 1;
1498+
return new Complex64( 2.0*realf( v ), 2.0*imagf( v ) );
1499+
}
1500+
1501+
var arr = new Complex64Array( 3 );
1502+
1503+
var context = {
1504+
'count': 0
1505+
};
1506+
1507+
// Set the first three elements:
1508+
arr.set( [ 1.0, 1.0 ], 0 );
1509+
arr.set( [ 2.0, 2.0 ], 1 );
1510+
arr.set( [ 3.0, 3.0 ], 2 );
1511+
1512+
var out = arr.map( scale, context );
1513+
// returns <Complex64Array>
1514+
1515+
var count = context.count;
1516+
// returns 3
1517+
```
1518+
14481519
<a name="method-set"></a>
14491520

14501521
#### Complex64Array.prototype.set( z\[, i] )

benchmark/benchmark.map.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench-harness' );
24+
var isComplex64Array = require( '@stdlib/assert-is-complex64array' );
25+
var realf = require( '@stdlib/complex-realf' );
26+
var imagf = require( '@stdlib/complex-imagf' );
27+
var Complex64 = require('@stdlib/complex-float32');
28+
var pkg = require( './../package.json' ).name;
29+
var Complex64Array = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg+':map', function benchmark( b ) {
35+
var out;
36+
var arr;
37+
var i;
38+
39+
arr = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
out = arr.map( scale );
44+
if ( typeof out !== 'object' ) {
45+
b.fail( 'should return an object' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isComplex64Array( out ) ) {
50+
b.fail( 'should return a Complex64Array' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
55+
function scale( v ) {
56+
return new Complex64( realf(v)*2.0, imagf(v)*2.0 );
57+
}
58+
});

benchmark/benchmark.map.length.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench-harness' );
24+
var isComplex64Array = require( '@stdlib/assert-is-complex64array' );
25+
var pow = require( '@stdlib/math-base-special-pow' );
26+
var identity = require( '@stdlib/utils-identity-function' );
27+
var Complex64 = require( '@stdlib/complex-float32' );
28+
var pkg = require( './../package.json' ).name;
29+
var Complex64Array = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var arr;
43+
var i;
44+
45+
arr = [];
46+
for ( i = 0; i < len; i++ ) {
47+
arr.push( new Complex64( i, i ) );
48+
}
49+
arr = new Complex64Array( arr );
50+
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var out;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
out = arr.map( identity );
66+
if ( typeof out !== 'object' ) {
67+
b.fail( 'should return an object' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isComplex64Array( out ) ) {
72+
b.fail( 'should return a Complex64Array' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':map:len='+len, f );
101+
}
102+
}
103+
104+
main();

dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/types/index.d.ts

+80
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,50 @@ type TernaryCallback<U> = ( this: U, value: Complex64, index: number, arr: Compl
151151
*/
152152
type Callback<U> = NullaryCallback<U> | UnaryCallback<U> | BinaryCallback<U> | TernaryCallback<U>;
153153

154+
/**
155+
* Callback invoked for each element in an array.
156+
*
157+
* @returns transformed value
158+
*/
159+
type NullaryMapFcn<U> = ( this: U ) => ComplexLike | ArrayLike<number>;
160+
161+
/**
162+
* Callback invoked for each element in an array.
163+
*
164+
* @param value - current array element
165+
* @returns transformed value
166+
*/
167+
type UnaryMapFcn<U> = ( this: U, value: Complex64 ) => ComplexLike | ArrayLike<number>;
168+
169+
/**
170+
* Callback invoked for each element in an array.
171+
*
172+
* @param value - current array element
173+
* @param index - current array element index
174+
* @returns transformed
175+
*/
176+
type BinaryMapFcn<U> = ( this: U, value: Complex64, index: number ) => ComplexLike | ArrayLike<number>;
177+
178+
/**
179+
* Callback invoked for each element in an array.
180+
*
181+
* @param value - current array element
182+
* @param index - current array element index
183+
* @param arr - array on which the method was called
184+
* @returns transformed value
185+
*/
186+
type TernaryMapFcn<U> = ( this: U, value: Complex64, index: number, arr: Complex64Array ) => ComplexLike | ArrayLike<number>;
187+
188+
/**
189+
* Callback invoked for each element in an array.
190+
*
191+
* @param value - current array element
192+
* @param index - current array element index
193+
* @param arr - array on which the method was called
194+
* @returns transformed value
195+
*/
196+
type MapFcn<U> = NullaryMapFcn<U> | UnaryMapFcn<U> | BinaryMapFcn<U> | TernaryMapFcn<U>;
197+
154198
/**
155199
* Class for creating a 64-bit complex number array.
156200
*/
@@ -677,6 +721,42 @@ declare class Complex64Array implements Complex64ArrayInterface {
677721
*/
678722
lastIndexOf( searchElement: ComplexLike, fromIndex?: number ): number;
679723

724+
/**
725+
* Returns a new array with each element being the result of the callback function.
726+
*
727+
* @param fcn - callback function
728+
* @param thisArg - execution context
729+
* @returns new array containing elements which are the result of the callback function
730+
*
731+
* @example
732+
* var Complex64 = require( '@stdlib/complex-float32' );
733+
* var realf = require( '@stdlib/complex-realf' );
734+
* var imagf = require( '@stdlib/complex-imagf' );
735+
*
736+
* function scale( v, i ) {
737+
* return new Complex64( 2.0*realf( v ), 2.0*imagf( v ) );
738+
* }
739+
*
740+
* var arr = new Complex64Array( 3 );
741+
*
742+
* arr.set( [ 1.0, -1.0 ], 0 );
743+
* arr.set( [ 2.0, -2.0 ], 1 );
744+
* arr.set( [ 3.0, -3.0 ], 2 );
745+
*
746+
* var out = arr.map( scale );
747+
* // returns <Complex64Array>
748+
*
749+
* var z = out.get( 0 );
750+
* // returns <Complex64>
751+
*
752+
* var re = realf( z );
753+
* // returns 2.0
754+
*
755+
* var im = imagf( z );
756+
* // returns -2.0
757+
*/
758+
map<U = unknown>( fcn: MapFcn<U>, thisArg?: ThisParameterType<MapFcn<U>> ): Complex64Array;
759+
680760
/**
681761
* Sets an array element.
682762
*

0 commit comments

Comments
 (0)