Skip to content

Commit 1154a11

Browse files
Snehil-ShahPranavchikukgrytePlaneshifter
authored
feat: add complex/parse-float64
PR-URL: #1362 Closes: #1333 Signed-off-by: Snehil Shah <snehilshah.989@gmail.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Co-authored-by: Athan Reines <kgryte@gmail.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 4e84566 commit 1154a11

File tree

10 files changed

+728
-0
lines changed

10 files changed

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