diff --git a/lib/node_modules/@stdlib/complex/parse-float64/README.md b/lib/node_modules/@stdlib/complex/parse-float64/README.md new file mode 100644 index 00000000000..1ab736461a4 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/README.md @@ -0,0 +1,132 @@ + + +# parseComplex128 + +> Parse a string representation of a 128-bit [complex number][@stdlib/complex/float64]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var parseComplex128 = require( '@stdlib/complex/parse-float64' ); +``` + +#### parseComplex128( str ) + +Parse a string representation of a 128-bit [complex number][@stdlib/complex/float64]. + +```javascript +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); + +var str = '5 + 3i'; + +var z = parseComplex128( str ); +// returns + +var re = real( z ); +// returns 5.0 + +var im = imag( z ); +// returns 3.0 +``` + +For details on the string format, see [Complex128][@stdlib/complex/float64]. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var parseComplex128 = require( '@stdlib/complex/parse-float64' ); +var isComplex128 = require( '@stdlib/assert/is-complex128' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); + +var str = '1e3 - 2.75i'; + +var z = parseComplex128( str ); +var bool = isComplex128( z ); +// returns true + +bool = ( real( z ) === 1e3 ); +// returns true + +bool = ( imag( z ) === -2.75 ); +// returns true +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js b/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js new file mode 100644 index 00000000000..ab0e8cf7744 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isComplex128 = require( '@stdlib/assert/is-complex128' ); +var pkg = require( './../package.json' ).name; +var parse = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var str; + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = i + ' + ' + (i+1) + 'i'; + z = parse( str ); + if ( !(isComplex128(z)) ) { + b.fail( 'should return a Complex128' ); + } + } + b.toc(); + if ( !(isComplex128(z)) ) { + b.fail( 'should return a Complex128' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt new file mode 100644 index 00000000000..6605e96de41 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt @@ -0,0 +1,23 @@ + +{{alias}}( str ) + Parse a string representation of a 128-bit complex number. + + Parameters + ---------- + str: string + String representation of a complex number. + + Returns + ------- + out: Complex128 + 128-bit complex number. + + Examples + -------- + > var str = '5 + 3i'; + > var z = {{alias}}( str ) + + + + See Also + -------- diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts new file mode 100644 index 00000000000..0458b0e9cfb --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts @@ -0,0 +1,46 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +// MODULES // + +/// + +import { Complex128 } from '@stdlib/types/complex'; + + +/** +* Parse a string representation of a 128-bit complex number. +* +* @param str - string representation of a complex number +* @returns Complex128 instance +* @throws must provide a string recognized as a complex number +* +* @example +* var str = '5 + 3i'; +* +* var z = parseComplex128( str ); +* // returns +*/ +declare function parseComplex128( str: string ): Complex128; + + +// EXPORTS // + +export = parseComplex128; diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts new file mode 100644 index 00000000000..ad8968439b1 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import parseComplex128 = require( './index' ); + + +// TESTS // + +// The function returns a complex number... +{ + parseComplex128( '5 + 3.5i' ); // $ExpectType Complex128 + parseComplex128( '3' ); // $ExpectType Complex128 + parseComplex128( '-8i' ); // $ExpectType Complex128 + parseComplex128( '1e3 + 1e-3i' ); // $ExpectType Complex128 + parseComplex128( 'NaN + NaNi' ); // $ExpectType Complex128 + parseComplex128( 'Infinity - Infinityi' ); // $ExpectType Complex128 +} + +// The compiler throws an error if the function is provided a first argument that is not a string... +{ + parseComplex128( true ); // $ExpectError + parseComplex128( false ); // $ExpectError + parseComplex128( null ); // $ExpectError + parseComplex128( undefined ); // $ExpectError + parseComplex128( 5 ); // $ExpectError + parseComplex128( [] ); // $ExpectError + parseComplex128( {} ); // $ExpectError + parseComplex128( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js new file mode 100644 index 00000000000..88ac8218ecf --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isComplex128 = require( '@stdlib/assert/is-complex128' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); +var parseComplex128 = require( './../lib' ); + +var str = '-0.5 + 1.25i'; + +var z = parseComplex128( str ); +console.log( z ); +// => + +var bool = ( isComplex128( z ) ); +console.log( bool ); +// => true + +bool = ( real( z ) === -0.5 ); +console.log( bool ); +// => true + +bool = ( imag( z ) === 1.25 ); +console.log( bool ); +// => true diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js new file mode 100644 index 00000000000..0234c6a86bf --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Parse a string representation of a complex number and returns a Complex128 instance. +* +* @module @stdlib/complex/parse-float64 +* +* @example +* var parseComplex128 = require( '@stdlib/complex/parse-float64' ); +* +* var str = '1 + 2i'; +* +* var z = parseComplex128( str ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js new file mode 100644 index 00000000000..8a2193e7d21 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Complex128 = require( '@stdlib/complex/float64' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var replace = require( '@stdlib/string/base/replace' ); +var format = require( '@stdlib/string/format' ); + + +// FUNCTIONS // + +/** +* Matches a complex number string. +* +* @private +* @returns {RegExp} regular expression +* +* @example +* var re = regexp(); +* // returns /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i)?$/ +*/ +function regexp() { + return /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i)?$/; +} + + +// MAIN // + +/** +* Parse a string representation of a complex number and returns a Complex128 instance. +* +* @param {string} str - string representation of a complex number +* @throws {TypeError} must provide a string +* @throws {Error} must provide a valid string representation of a complex number +* @returns {Complex128} 128-bit complex number +* +* @example +* var str = '1 + 2i'; +* var z = parseComplex128( str ); +* // returns +*/ +function parseComplex128( str ) { + var match; + var re; + var im = 0; + + if ( !isString( str ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); + } + + match = replace( str, /\s/g, '' ).match( regexp() ); + if ( !match ) { + throw new Error( format( 'invalid argument. Unable to parse input string as a complex number. Value: `%s`.', str ) ); + } + + // Real part: + re = ( match[1] && !match[1].endsWith( 'i' ) ) ? parseFloat( match[1] ) : 0; + + // Imaginary part: + if ( match[4] ) { + im = ( ( match[3] === '-' ) ? -1 : 1 ) * parseFloat( replace( match[4], /i$/, '' ) ); + } else if ( match[1] && match[1].endsWith( 'i' ) ) { + im = parseFloat( replace( match[1], /i$/, '' ) ); + } + return new Complex128( re, im ); +} + + +// EXPORTS // + +module.exports = parseComplex128; diff --git a/lib/node_modules/@stdlib/complex/parse-float64/package.json b/lib/node_modules/@stdlib/complex/parse-float64/package.json new file mode 100644 index 00000000000..37e0a63ab09 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/complex/parse-float64", + "version": "0.0.0", + "description": "Parse a string representation of a 128-bit complex number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "utils", + "util", + "utilities", + "utility", + "complex", + "complex128", + "cmplx", + "parse", + "string", + "convert", + "object", + "obj" + ] +} diff --git a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js new file mode 100644 index 00000000000..865c9dc59ab --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js @@ -0,0 +1,192 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex128 = require( '@stdlib/assert/is-complex128' ); +var isSameComplex128 = require( '@stdlib/assert/is-same-complex128' ); +var isNan = require( '@stdlib/math/base/assert/is-nan' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); +var parseComplex128 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof parseComplex128, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error for inputs not recognized as complex numbers', function test( t ) { + t.throws( function invalidInput() { + parseComplex128( 'beep boop' ); + }, Error, 'throws an Error for invalid input' ); + t.throws( function invalidType() { + parseComplex128( true ); + }, TypeError, 'throws TypeError for non string input type' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number', function test( t ) { + var z; + var w; + + z = new Complex128( 5.0, 12.0 ); + w = parseComplex128( '5 + 12i' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value'); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number (negative values)', function test( t ) { + var z; + var w; + + z = new Complex128( -2.5, -4.0 ); + w = parseComplex128( '-2.5 - 4i' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number (only real part)', function test( t ) { + var z; + var w; + + z = new Complex128( 3.0, 0.0 ); + w = parseComplex128( '3' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number (only imaginary part)', function test( t ) { + var z; + var w; + + z = new Complex128( 0.0, 8.5 ); + w = parseComplex128( '8.5i' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number (only real part, negative)', function test( t ) { + var z; + var w; + + z = new Complex128( -3.753, 0.0 ); + w = parseComplex128( '-3.753' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number (only imaginary part, negative)', function test( t ) { + var z; + var w; + + z = new Complex128( 0.0, -0.3 ); + w = parseComplex128( '-0.3i' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function correctly parses a string representation of a complex number with no space (a+ib format)', function test( t ) { + var z; + var w; + + z = new Complex128( 5.0, 3.0 ); + w = parseComplex128( '5+3i' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function correctly parses a string representation of a complex number with unconventional spacing (a+ ib format)', function test( t ) { + var z; + var w; + + z = new Complex128( 5.0, 3.0 ); + w = parseComplex128( '5+ 3i' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number with NaNs', function test( t ) { + var w = parseComplex128( 'NaN + NaNi' ); + + t.ok( isComplex128(w), 'is an instance' ); + t.ok( isNan(real( w )), 'has expected property value' ); + t.ok( isNan(imag( w )), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number with Infinity', function test( t ) { + var z; + var w; + + z = new Complex128( PINF, NINF ); + w = parseComplex128( 'Infinity - Infinityi' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number in scientific notation', function test( t ) { + var z; + var w; + + z = new Complex128( 1e3, 4.1e-3 ); + w = parseComplex128( '1E3 + 4.1e-3i' ); + + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); + + t.end(); +});