diff --git a/lib/node_modules/@stdlib/complex/parse-float32/README.md b/lib/node_modules/@stdlib/complex/parse-float32/README.md new file mode 100644 index 000000000000..8026eb219b15 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/README.md @@ -0,0 +1,133 @@ + + +# parseComplex64 + +> Parse a string representation of a 64-bit [complex number][@stdlib/complex/float32]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var parseComplex64 = require( '@stdlib/complex/parse-float32' ); +``` + +#### parseComplex64( str ) + +Parse a string representation of a 64-bit [complex number][@stdlib/complex/float32]. + +```javascript +var parseComplex64 = require( '@stdlib/complex/parse-float32' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); + +var str = '5 + 3i'; + +var z = parseComplex64( str ); +// returns + +var re = real( z ); +// returns 5.0 + +var im = imag( z ); +// returns 3.0 +``` + +For details on the string format, see [Complex64][@stdlib/complex/float32]. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var parseComplex64 = require( '@stdlib/complex/parse-float32' ); +var isComplex64 = require( '@stdlib/assert/is-complex64' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); + +var str = '1e3 - 2.75i'; + +var z = parseComplex64( str ); +var bool = isComplex64( 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-float32/benchmark/benchmark.js b/lib/node_modules/@stdlib/complex/parse-float32/benchmark/benchmark.js new file mode 100644 index 000000000000..a75453f549ba --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/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 isComplex64 = require( '@stdlib/assert/is-complex64' ); +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 ( !(isComplex64(z)) ) { + b.fail( 'should return a Complex64' ); + } + } + b.toc(); + if ( !(isComplex64(z)) ) { + b.fail( 'should return a Complex64' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/complex/parse-float32/docs/repl.txt b/lib/node_modules/@stdlib/complex/parse-float32/docs/repl.txt new file mode 100644 index 000000000000..a63f2014727f --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/docs/repl.txt @@ -0,0 +1,22 @@ + +{{alias}}( str ) + Parse a string representation of a 64-bit complex number. + + Parameters + ---------- + str: string + String representation of a complex number. + + Returns + ------- + out: Complex64 + 64-bit complex number. + + Examples + -------- + > var str = '5 + 3i'; + > var z = {{alias}}( str ) + + + See Also + -------- diff --git a/lib/node_modules/@stdlib/complex/parse-float32/docs/types/index.d.ts b/lib/node_modules/@stdlib/complex/parse-float32/docs/types/index.d.ts new file mode 100644 index 000000000000..0e5c757cab54 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/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 { Complex64 } from '@stdlib/types/complex'; + + +/** +* Parse a string representation of a 64-bit complex number. +* +* @param str - string representation of a complex number +* @returns Complex64 instance +* @throws must provide a string recognized as a complex number +* +* @example +* var str = '5 + 3i'; +* +* var z = parseComplex64( str ); +* // returns +*/ +declare function parseComplex64( str: string ): Complex64; + + +// EXPORTS // + +export = parseComplex64; diff --git a/lib/node_modules/@stdlib/complex/parse-float32/docs/types/test.ts b/lib/node_modules/@stdlib/complex/parse-float32/docs/types/test.ts new file mode 100644 index 000000000000..5638828d5949 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/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 parseComplex64 = require( './index' ); + + +// TESTS // + +// The function returns a complex number... +{ + parseComplex64( '5 + 3.5i' ); // $ExpectType Complex64 + parseComplex64( '3' ); // $ExpectType Complex64 + parseComplex64( '-8i' ); // $ExpectType Complex64 + parseComplex64( '1e3 + 1e-3i' ); // $ExpectType Complex64 + parseComplex64( 'NaN + NaNi' ); // $ExpectType Complex64 + parseComplex64( 'Infinity - Infinityi' ); // $ExpectType Complex64 +} + +// The compiler throws an error if the function is provided a first argument that is not a string... +{ + parseComplex64( true ); // $ExpectError + parseComplex64( false ); // $ExpectError + parseComplex64( null ); // $ExpectError + parseComplex64( undefined ); // $ExpectError + parseComplex64( 5 ); // $ExpectError + parseComplex64( [] ); // $ExpectError + parseComplex64( {} ); // $ExpectError + parseComplex64( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/complex/parse-float32/examples/index.js b/lib/node_modules/@stdlib/complex/parse-float32/examples/index.js new file mode 100644 index 000000000000..c7d69192abc4 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/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 isComplex64 = require( '@stdlib/assert/is-complex64' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); +var parseComplex64 = require( './../lib' ); + +var str = '-0.5 + 1.25i'; + +var z = parseComplex64( str ); +console.log( z ); +// => + +var bool = ( isComplex64( 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-float32/lib/index.js b/lib/node_modules/@stdlib/complex/parse-float32/lib/index.js new file mode 100644 index 000000000000..5ff6842ed922 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/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 Complex64 instance. +* +* @module @stdlib/complex/parse-float32 +* +* @example +* var parseComplex64 = require( '@stdlib/complex/parse-float32' ); +* +* var str = '1 + 2i'; +* +* var z = parseComplex64( str ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/complex/parse-float32/lib/main.js b/lib/node_modules/@stdlib/complex/parse-float32/lib/main.js new file mode 100644 index 000000000000..6cf871fba48e --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var Complex64 = require( '@stdlib/complex/float32' ); +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 Complex64 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 {Complex64} 64-bit complex number +* +* @example +* var str = '1 + 2i'; +* var z = parseComplex64( str ); +* // returns +*/ +function parseComplex64( 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 Complex64( re, im ); +} + + +// EXPORTS // + +module.exports = parseComplex64; diff --git a/lib/node_modules/@stdlib/complex/parse-float32/package.json b/lib/node_modules/@stdlib/complex/parse-float32/package.json new file mode 100644 index 000000000000..6552d018278e --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/complex/parse-float32", + "version": "0.0.0", + "description": "Parse a string representation of a 64-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", + "complex64", + "cmplx", + "parse", + "string", + "convert", + "object", + "obj" + ] +} diff --git a/lib/node_modules/@stdlib/complex/parse-float32/test/test.js b/lib/node_modules/@stdlib/complex/parse-float32/test/test.js new file mode 100644 index 000000000000..04d0d62bf619 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float32/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/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var Complex64 = require( '@stdlib/complex/float32' ); +var isComplex64 = require( '@stdlib/assert/is-complex64' ); +var isSameComplex64 = require( '@stdlib/assert/is-same-complex64' ); +var isNan = require( '@stdlib/math/base/assert/is-nan' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); +var parseComplex64 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof parseComplex64, '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() { + parseComplex64( 'beep boop' ); + }, Error, 'throws an Error for invalid input' ); + t.throws( function invalidType() { + parseComplex64( 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 Complex64( 5.0, 12.0 ); + w = parseComplex64( '5 + 12i' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( 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 Complex64( -2.5, -4.0 ); + w = parseComplex64( '-2.5 - 4i' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( 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 Complex64( 3.0, 0.0 ); + w = parseComplex64( '3' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( 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 Complex64( 0.0, 8.5 ); + w = parseComplex64( '8.5i' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( 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 Complex64( -3.753, 0.0 ); + w = parseComplex64( '-3.753' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( 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 Complex64( 0.0, -0.3 ); + w = parseComplex64( '-0.3i' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( 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 Complex64( 5.0, 3.0 ); + w = parseComplex64( '5+3i' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( 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 Complex64( 5.0, 3.0 ); + w = parseComplex64( '5+ 3i' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( 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 = parseComplex64( 'NaN + NaNi' ); + + t.ok( isComplex64(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 Complex64( PINF, NINF ); + w = parseComplex64( 'Infinity - Infinityi' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( 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 Complex64( 1e3, 4.1e-3 ); + w = parseComplex64( '1E3 + 4.1e-3i' ); + + t.ok( isComplex64( w ), 'is an instance' ); + t.ok( isSameComplex64( w, z ), 'has expected property value' ); + + t.end(); +});