Skip to content

Commit 70969b2

Browse files
committed
Add utility to return a complex number constructor
1 parent 204ad90 commit 70969b2

File tree

11 files changed

+640
-0
lines changed

11 files changed

+640
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Constructors
22+
23+
> Complex number constructors.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var ctors = require( '@stdlib/complex/ctors' );
41+
```
42+
43+
#### ctors( dtype )
44+
45+
Returns a complex number constructor for a specified data type.
46+
47+
```javascript
48+
var ctor = ctors( 'complex128' );
49+
// returns <Function>
50+
```
51+
52+
The function returns constructors for the following data types:
53+
54+
- `complex64`: single-precision complex floating-point numbers.
55+
- `complex128`: double-precision complex floating-point numbers.
56+
57+
If provided an unknown or unsupported data type, the function returns `null`.
58+
59+
```javascript
60+
var ctor = ctors( 'complex' );
61+
// returns null
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var dtypes = require( '@stdlib/complex/dtypes' );
86+
var ctors = require( '@stdlib/complex/ctors' );
87+
88+
var DTYPES = dtypes();
89+
var ctor;
90+
var i;
91+
92+
for ( i = 0; i < DTYPES.length; i++ ) {
93+
ctor = ctors( DTYPES[ i ] );
94+
console.log( ctor );
95+
}
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
103+
104+
<section class="references">
105+
106+
</section>
107+
108+
<!-- /.references -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
</section>
115+
116+
<!-- /.related -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
</section>
123+
124+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 dtypes = require( '@stdlib/complex/dtypes' );
25+
var isFunction = require( '@stdlib/assert/is-function' );
26+
var pkg = require( './../package.json' ).name;
27+
var ctors = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var DTYPES = dtypes();
33+
34+
35+
// MAIN //
36+
37+
bench( pkg, function benchmark( b ) {
38+
var ctor;
39+
var i;
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
ctor = ctors( DTYPES[ i%DTYPES.length ] );
44+
if ( typeof ctor !== 'function' ) {
45+
b.fail( 'should return a function' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isFunction( ctor ) ) {
50+
b.fail( 'should return a function' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( dtype )
3+
Returns a complex number constructor.
4+
5+
The function returns constructors for the following data types:
6+
7+
- complex64: single-precision complex floating-point numbers.
8+
- complex128: double-precision complex floating-point numbers.
9+
10+
Parameters
11+
----------
12+
dtype: string
13+
Data type.
14+
15+
Returns
16+
-------
17+
out: Function|null
18+
Constructor.
19+
20+
Examples
21+
--------
22+
> var ctor = {{alias}}( 'complex128' )
23+
<Function>
24+
> ctor = {{alias}}( 'complex' )
25+
null
26+
27+
See Also
28+
--------
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
// TypeScript Version: 2.0
20+
21+
import Complex128 = require( '@stdlib/complex/float64' );
22+
import Complex64 = require( '@stdlib/complex/float32' );
23+
24+
/**
25+
* Returns a `Complex128` constructor.
26+
*
27+
* @param dtype - data type
28+
* @returns constructor
29+
*
30+
* @example
31+
* var ctor = ctors( 'complex128' );
32+
* // returns <Function>
33+
*/
34+
declare function ctors( dtype: 'complex128' ): typeof Complex128;
35+
36+
/**
37+
* Returns a `Complex64` constructor.
38+
*
39+
* @param dtype - data type
40+
* @returns constructor
41+
*
42+
* @example
43+
* var ctor = ctors( 'complex64' );
44+
* // returns <Function>
45+
*/
46+
declare function ctors( dtype: 'complex64' ): typeof Complex64;
47+
48+
/**
49+
* Returns a complex number constructor.
50+
*
51+
* @param dtype - data type
52+
* @returns constructor or null
53+
*
54+
* @example
55+
* var ctor = ctors( 'complex128' );
56+
* // returns <Function>
57+
*
58+
* @example
59+
* var ctor = ctors( 'float' );
60+
* // returns null
61+
*/
62+
declare function ctors( dtype: string ): Function | null;
63+
64+
65+
// EXPORTS //
66+
67+
export = ctors;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
import ctors = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a function or null..
25+
{
26+
ctors( 'complex128' ); // $ExpectType typeof Complex128
27+
ctors( 'complex64' ); // $ExpectType typeof Complex64
28+
ctors( 'float64' ); // $ExpectType Function | null
29+
}
30+
31+
// The compiler throws an error if the function is provided an unsupported number of arguments...
32+
{
33+
ctors(); // $ExpectError
34+
ctors( 'int32', 3 ); // $ExpectError
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
var dtypes = require( '@stdlib/complex/dtypes' );
22+
var ctors = require( './../lib' );
23+
24+
var DTYPES = dtypes();
25+
var ctor;
26+
var i;
27+
28+
for ( i = 0; i < DTYPES.length; i++ ) {
29+
ctor = ctors( DTYPES[ i ] );
30+
console.log( ctor );
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 Complex64 = require( '@stdlib/complex/float32' );
24+
var Complex128 = require( '@stdlib/complex/float64' );
25+
26+
27+
// MAIN //
28+
29+
// Mapping from data types to constructors...
30+
var ctors = {
31+
'complex64': Complex64,
32+
'complex128': Complex128
33+
};
34+
35+
36+
// EXPORTS //
37+
38+
module.exports = ctors;

0 commit comments

Comments
 (0)