Skip to content

Commit 70901b9

Browse files
committed
Update to support returning complex number arrays
1 parent 53cb31f commit 70901b9

File tree

12 files changed

+723
-25
lines changed

12 files changed

+723
-25
lines changed

lib/node_modules/@stdlib/array/typed/README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ The function recognizes the following data types:
5353

5454
- `float64`: double-precision floating-point numbers (IEEE 754)
5555
- `float32`: single-precision floating-point numbers (IEEE 754)
56+
- `complex128`: double-precision complex floating-point numbers
57+
- `complex64`: single-precision complex floating-point numbers
5658
- `int32`: 32-bit two's complement signed integers
5759
- `uint32`: 32-bit unsigned integers
5860
- `int16`: 16-bit two's complement signed integers
@@ -97,7 +99,7 @@ var arr3 = typedarray( arr1, 'int32' );
9799

98100
#### typedarray( obj\[, dtype] )
99101

100-
Creates a [typed array][mdn-typed-array] from an array-like `object` or iterable.
102+
Creates a [typed array][mdn-typed-array] from an array-like object or iterable.
101103

102104
```javascript
103105
var arr1 = typedarray( [ 0.5, 0.5, 0.5 ] );
@@ -107,6 +109,8 @@ var arr2 = typedarray( [ 0.5, 0.5, 0.5 ], 'float32' );
107109
// returns <Float32Array>[ 0.5, 0.5, 0.5 ]
108110
```
109111

112+
If `dtype` is complex number data type and an array-like object contains interleaved real and imaginary components, the array-like object must have a length which is a multiple of two.
113+
110114
#### typedarray( buffer\[, byteOffset\[, length]]\[, dtype] )
111115

112116
Returns a [typed array][mdn-typed-array] view of an [`ArrayBuffer`][mdn-arraybuffer].
@@ -142,6 +146,10 @@ var arr6 = typedarray( buf, 10, 4, 'int16' );
142146

143147
<section class="notes">
144148

149+
## Notes
150+
151+
- When providing a complex number array, if `dtype` is unspecified or the specified data type is not a complex number data type, the returned array contains interleaved real and imaginary components.
152+
145153
</section>
146154

147155
<!-- /.notes -->
@@ -158,10 +166,9 @@ var arr6 = typedarray( buf, 10, 4, 'int16' );
158166
var randu = require( '@stdlib/random/base/randu' );
159167
var typedarray = require( '@stdlib/array/typed' );
160168

161-
var arr;
162-
var i;
169+
var arr = typedarray( 100, 'float64' );
163170

164-
arr = typedarray( 100, 'float64' );
171+
var i;
165172
for ( i = 0; i < arr.length; i++ ) {
166173
arr[ i ] = randu() * 100.0;
167174
}

lib/node_modules/@stdlib/array/typed/benchmark/benchmark.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
25+
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
2526
var pkg = require( './../package.json' ).name;
2627
var typedarray = require( './../lib' );
2728

@@ -82,6 +83,42 @@ bench( pkg+':dtype=float32', function benchmark( b ) {
8283
b.end();
8384
});
8485

86+
bench( pkg+':dtype=complex128', function benchmark( b ) {
87+
var arr;
88+
var i;
89+
b.tic();
90+
for ( i = 0; i < b.iterations; i++ ) {
91+
arr = typedarray( 0, 'complex128' );
92+
if ( arr.length !== 0 ) {
93+
b.fail( 'should have length 0' );
94+
}
95+
}
96+
b.toc();
97+
if ( !isComplexTypedArray( arr ) ) {
98+
b.fail( 'should return a complex typed array' );
99+
}
100+
b.pass( 'benchmark finished' );
101+
b.end();
102+
});
103+
104+
bench( pkg+':dtype=complex64', function benchmark( b ) {
105+
var arr;
106+
var i;
107+
b.tic();
108+
for ( i = 0; i < b.iterations; i++ ) {
109+
arr = typedarray( 0, 'complex64' );
110+
if ( arr.length !== 0 ) {
111+
b.fail( 'should have length 0' );
112+
}
113+
}
114+
b.toc();
115+
if ( !isComplexTypedArray( arr ) ) {
116+
b.fail( 'should return a complex typed array' );
117+
}
118+
b.pass( 'benchmark finished' );
119+
b.end();
120+
});
121+
85122
bench( pkg+':dtype=int32', function benchmark( b ) {
86123
var arr;
87124
var i;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var typedarray = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
return benchmark;
41+
42+
/**
43+
* Benchmark function.
44+
*
45+
* @private
46+
* @param {Benchmark} b - benchmark instance
47+
*/
48+
function benchmark( b ) {
49+
var arr;
50+
var i;
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
arr = typedarray( len, 'complex128' );
55+
if ( arr.length !== len ) {
56+
b.fail( 'unexpected length' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isComplexTypedArray( arr ) ) {
61+
b.fail( 'should return a complex typed array' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
}
66+
}
67+
68+
69+
// MAIN //
70+
71+
/**
72+
* Main execution sequence.
73+
*
74+
* @private
75+
*/
76+
function main() {
77+
var len;
78+
var min;
79+
var max;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
len = pow( 10, i );
88+
f = createBenchmark( len );
89+
bench( pkg+':dtype=complex128,len='+len, f );
90+
}
91+
}
92+
93+
main();
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var typedarray = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
return benchmark;
41+
42+
/**
43+
* Benchmark function.
44+
*
45+
* @private
46+
* @param {Benchmark} b - benchmark instance
47+
*/
48+
function benchmark( b ) {
49+
var arr;
50+
var i;
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
arr = typedarray( len, 'complex64' );
55+
if ( arr.length !== len ) {
56+
b.fail( 'unexpected length' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isComplexTypedArray( arr ) ) {
61+
b.fail( 'should return a complex typed array' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
}
66+
}
67+
68+
69+
// MAIN //
70+
71+
/**
72+
* Main execution sequence.
73+
*
74+
* @private
75+
*/
76+
function main() {
77+
var len;
78+
var min;
79+
var max;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
len = pow( 10, i );
88+
f = createBenchmark( len );
89+
bench( pkg+':dtype=complex64,len='+len, f );
90+
}
91+
}
92+
93+
main();

lib/node_modules/@stdlib/array/typed/docs/repl.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- float64: double-precision floating-point numbers (IEEE 754)
88
- float32: single-precision floating-point numbers (IEEE 754)
9+
- complex128: double-precision complex floating-point numbers
10+
- complex64: single-precision complex floating-point numbers
911
- int32: 32-bit two's complement signed integers
1012
- uint32: 32-bit unsigned integers
1113
- int16: 16-bit two's complement signed integers

lib/node_modules/@stdlib/array/typed/docs/types/index.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/// <reference types="@stdlib/types"/>
2424

25-
import { ArrayLike, TypedArray, RealDataType as DataType } from '@stdlib/types/array';
25+
import { ArrayLike, RealOrComplexTypedArray, RealOrComplexDataType } from '@stdlib/types/array';
2626
import ArrayBuffer = require( '@stdlib/array/buffer' );
2727

2828
/**
@@ -39,7 +39,7 @@ import ArrayBuffer = require( '@stdlib/array/buffer' );
3939
* var arr = typedarray( 'float32');
4040
* // returns <Float32Array>
4141
*/
42-
declare function typedarray( dtype?: DataType ): TypedArray;
42+
declare function typedarray( dtype?: RealOrComplexDataType ): RealOrComplexTypedArray; // tslint:disable-line:max-line-length
4343

4444
/**
4545
* Creates a typed array.
@@ -56,7 +56,7 @@ declare function typedarray( dtype?: DataType ): TypedArray;
5656
* var arr = typedarray( 2, 'float32' );
5757
* // returns <Float32Array>[ 0.0, 0.0 ]
5858
*/
59-
declare function typedarray( length: number, dtype?: DataType ): TypedArray;
59+
declare function typedarray( length: number, dtype?: RealOrComplexDataType ): RealOrComplexTypedArray; // tslint:disable-line:max-line-length
6060

6161
/**
6262
* Creates a typed array.
@@ -83,7 +83,7 @@ declare function typedarray( length: number, dtype?: DataType ): TypedArray;
8383
* var arr2 = typedarray( arr1, 'uint32' );
8484
* // returns <Uint32Array>[ 5, 3 ]
8585
*/
86-
declare function typedarray( typedarray: TypedArray, dtype?: DataType ): TypedArray; // tslint:disable-line:max-line-length
86+
declare function typedarray( typedarray: RealOrComplexTypedArray, dtype?: RealOrComplexDataType ): RealOrComplexTypedArray; // tslint:disable-line:max-line-length
8787

8888
/**
8989
* Creates a typed array.
@@ -100,7 +100,7 @@ declare function typedarray( typedarray: TypedArray, dtype?: DataType ): TypedAr
100100
* var arr = typedarray( [ 5, -3 ], 'int32' );
101101
* // returns <Int32Array>[ 5, -3 ]
102102
*/
103-
declare function typedarray( obj: ArrayLike<number> | Iterable<any>, dtype?: DataType ): TypedArray; // tslint:disable-line:max-line-length
103+
declare function typedarray( obj: ArrayLike<number> | Iterable<any>, dtype?: RealOrComplexDataType ): RealOrComplexTypedArray; // tslint:disable-line:max-line-length
104104

105105
/**
106106
* Creates a typed array.
@@ -123,7 +123,7 @@ declare function typedarray( obj: ArrayLike<number> | Iterable<any>, dtype?: Dat
123123
* var arr = typedarray( buf, 'float32' );
124124
* // returns <Float32Array>[ 0.0, 0.0, 0.0, 0.0 ]
125125
*/
126-
declare function typedarray( buffer: ArrayBuffer, dtype?: DataType ): TypedArray; // tslint:disable-line:max-line-length
126+
declare function typedarray( buffer: ArrayBuffer, dtype?: RealOrComplexDataType ): RealOrComplexTypedArray; // tslint:disable-line:max-line-length
127127

128128
/**
129129
* Creates a typed array.
@@ -147,7 +147,7 @@ declare function typedarray( buffer: ArrayBuffer, dtype?: DataType ): TypedArray
147147
* var arr = typedarray( buf, 8, 'float32' );
148148
* // returns <Float32Array>[ 0.0, 0.0 ]
149149
*/
150-
declare function typedarray( buffer: ArrayBuffer, byteOffset?: number, dtype?: DataType ): TypedArray; // tslint:disable-line:max-line-length
150+
declare function typedarray( buffer: ArrayBuffer, byteOffset?: number, dtype?: RealOrComplexDataType ): RealOrComplexTypedArray; // tslint:disable-line:max-line-length
151151

152152
/**
153153
* Creates a typed array.
@@ -172,7 +172,7 @@ declare function typedarray( buffer: ArrayBuffer, byteOffset?: number, dtype?: D
172172
* var arr = typedarray( buf, 8, 2, 'int32' );
173173
* // returns <Int32Array>[ 0, 0 ]
174174
*/
175-
declare function typedarray( buffer: ArrayBuffer, byteOffset?: number, length?: number, dtype?: DataType ): TypedArray; // tslint:disable-line:max-line-length
175+
declare function typedarray( buffer: ArrayBuffer, byteOffset?: number, length?: number, dtype?: RealOrComplexDataType ): RealOrComplexTypedArray; // tslint:disable-line:max-line-length
176176

177177

178178
// EXPORTS //

lib/node_modules/@stdlib/array/typed/docs/types/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import typedarray = require( './index' );
2323

2424
// The function returns a typed array..
2525
{
26-
typedarray(); // $ExpectType TypedArray
27-
typedarray( 'float32' ); // $ExpectType TypedArray
28-
typedarray( 10, 'float32' ); // $ExpectType TypedArray
29-
typedarray( [ 1, 2, 3 ], 'int32' ); // $ExpectType TypedArray
26+
typedarray(); // $ExpectType RealOrComplexTypedArray
27+
typedarray( 'float32' ); // $ExpectType RealOrComplexTypedArray
28+
typedarray( 10, 'float32' ); // $ExpectType RealOrComplexTypedArray
29+
typedarray( [ 1, 2, 3 ], 'int32' ); // $ExpectType RealOrComplexTypedArray
3030
}
3131

3232
// The compiler throws an error if the function is provided a first argument which is not a data type, number, array-like object, or typed array...

lib/node_modules/@stdlib/array/typed/examples/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
var randu = require( '@stdlib/random/base/randu' );
2222
var typedarray = require( './../lib' );
2323

24-
var arr;
25-
var i;
24+
var arr = typedarray( 100, 'float64' );
2625

27-
arr = typedarray( 100, 'float64' );
26+
var i;
2827
for ( i = 0; i < arr.length; i++ ) {
2928
arr[ i ] = randu() * 100.0;
3029
}

0 commit comments

Comments
 (0)