Skip to content

Commit a5f357d

Browse files
kawaho2Pranavchiku
authored and
Utkarsh Gupta
committed
feat: add complex/parse-float32
PR-URL: stdlib-js#1430 Closes: stdlib-js#1332 --------- Signed-off-by: Rejoan Sardar <119718513+Rejoan-Sardar@users.noreply.github.com> Signed-off-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Co-authored-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Reviewed-by: Pranav <85227306+Pranavchiku@users.noreply.github.com>
1 parent 83c859c commit a5f357d

File tree

10 files changed

+728
-0
lines changed

10 files changed

+728
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# parseComplex64
22+
23+
> Parse a string representation of a 64-bit [complex number][@stdlib/complex/float32].
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 parseComplex64 = require( '@stdlib/complex/parse-float32' );
41+
```
42+
43+
#### parseComplex64( str )
44+
45+
Parse a string representation of a 64-bit [complex number][@stdlib/complex/float32].
46+
47+
```javascript
48+
var parseComplex64 = require( '@stdlib/complex/parse-float32' );
49+
var real = require( '@stdlib/complex/real' );
50+
var imag = require( '@stdlib/complex/imag' );
51+
52+
var str = '5 + 3i';
53+
54+
var z = parseComplex64( str );
55+
// returns <Complex64>
56+
57+
var re = real( z );
58+
// returns 5.0
59+
60+
var im = imag( z );
61+
// returns 3.0
62+
```
63+
64+
For details on the string format, see [Complex64][@stdlib/complex/float32].
65+
66+
</section>
67+
68+
<!-- /.usage -->
69+
70+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
71+
72+
<section class="notes">
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<!-- Package usage examples. -->
79+
80+
<section class="examples">
81+
82+
## Examples
83+
84+
<!-- eslint no-undef: "error" -->
85+
86+
```javascript
87+
var parseComplex64 = require( '@stdlib/complex/parse-float32' );
88+
var isComplex64 = require( '@stdlib/assert/is-complex64' );
89+
var real = require( '@stdlib/complex/real' );
90+
var imag = require( '@stdlib/complex/imag' );
91+
92+
var str = '1e3 - 2.75i';
93+
94+
var z = parseComplex64( str );
95+
var bool = isComplex64( z );
96+
// returns true
97+
98+
bool = ( real( z ) === 1e3 );
99+
// returns true
100+
101+
bool = ( imag( z ) === -2.75 );
102+
// returns true
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- 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. -->
110+
111+
<section class="references">
112+
113+
</section>
114+
115+
<!-- /.references -->
116+
117+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
118+
119+
<section class="related">
120+
121+
</section>
122+
123+
<!-- /.related -->
124+
125+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="links">
128+
129+
[@stdlib/complex/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float32
130+
131+
</section>
132+
133+
<!-- /.links -->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isComplex64 = require( '@stdlib/assert/is-complex64' );
25+
var pkg = require( './../package.json' ).name;
26+
var parse = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var str;
33+
var z;
34+
var i;
35+
36+
b.tic();
37+
for ( i = 0; i < b.iterations; i++ ) {
38+
str = i + ' + ' + (i+1) + 'i';
39+
z = parse( str );
40+
if ( !(isComplex64(z)) ) {
41+
b.fail( 'should return a Complex64' );
42+
}
43+
}
44+
b.toc();
45+
if ( !(isComplex64(z)) ) {
46+
b.fail( 'should return a Complex64' );
47+
}
48+
b.pass( 'benchmark finished' );
49+
b.end();
50+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{{alias}}( str )
3+
Parse a string representation of a 64-bit complex number.
4+
5+
Parameters
6+
----------
7+
str: string
8+
String representation of a complex number.
9+
10+
Returns
11+
-------
12+
out: Complex64
13+
64-bit complex number.
14+
15+
Examples
16+
--------
17+
> var str = '5 + 3i';
18+
> var z = {{alias}}( str )
19+
<Complex64>
20+
21+
See Also
22+
--------
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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: 4.1
20+
21+
// MODULES //
22+
23+
/// <reference types="@stdlib/types"/>
24+
25+
import { Complex64 } from '@stdlib/types/complex';
26+
27+
28+
/**
29+
* Parse a string representation of a 64-bit complex number.
30+
*
31+
* @param str - string representation of a complex number
32+
* @returns Complex64 instance
33+
* @throws must provide a string recognized as a complex number
34+
*
35+
* @example
36+
* var str = '5 + 3i';
37+
*
38+
* var z = parseComplex64( str );
39+
* // returns <Complex64>
40+
*/
41+
declare function parseComplex64( str: string ): Complex64;
42+
43+
44+
// EXPORTS //
45+
46+
export = parseComplex64;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 parseComplex64 = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a complex number...
25+
{
26+
parseComplex64( '5 + 3.5i' ); // $ExpectType Complex64
27+
parseComplex64( '3' ); // $ExpectType Complex64
28+
parseComplex64( '-8i' ); // $ExpectType Complex64
29+
parseComplex64( '1e3 + 1e-3i' ); // $ExpectType Complex64
30+
parseComplex64( 'NaN + NaNi' ); // $ExpectType Complex64
31+
parseComplex64( 'Infinity - Infinityi' ); // $ExpectType Complex64
32+
}
33+
34+
// The compiler throws an error if the function is provided a first argument that is not a string...
35+
{
36+
parseComplex64( true ); // $ExpectError
37+
parseComplex64( false ); // $ExpectError
38+
parseComplex64( null ); // $ExpectError
39+
parseComplex64( undefined ); // $ExpectError
40+
parseComplex64( 5 ); // $ExpectError
41+
parseComplex64( [] ); // $ExpectError
42+
parseComplex64( {} ); // $ExpectError
43+
parseComplex64( ( x: number ): number => x ); // $ExpectError
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isComplex64 = require( '@stdlib/assert/is-complex64' );
22+
var real = require( '@stdlib/complex/real' );
23+
var imag = require( '@stdlib/complex/imag' );
24+
var parseComplex64 = require( './../lib' );
25+
26+
var str = '-0.5 + 1.25i';
27+
28+
var z = parseComplex64( str );
29+
console.log( z );
30+
// => <Complex64>
31+
32+
var bool = ( isComplex64( z ) );
33+
console.log( bool );
34+
// => true
35+
36+
bool = ( real( z ) === -0.5 );
37+
console.log( bool );
38+
// => true
39+
40+
bool = ( imag( z ) === 1.25 );
41+
console.log( bool );
42+
// => true
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
/**
22+
* Parse a string representation of a complex number and returns a Complex64 instance.
23+
*
24+
* @module @stdlib/complex/parse-float32
25+
*
26+
* @example
27+
* var parseComplex64 = require( '@stdlib/complex/parse-float32' );
28+
*
29+
* var str = '1 + 2i';
30+
*
31+
* var z = parseComplex64( str );
32+
* // returns <Complex64>
33+
*/
34+
35+
// MODULES //
36+
37+
var main = require( './main.js' );
38+
39+
40+
// EXPORTS //
41+
42+
module.exports = main;

0 commit comments

Comments
 (0)