Sign in to follow this  
Followers 0
Kaster

Complex number identification by RegExp

4 posts in this topic

Hi guys.

I want to operate with complex numbers due to lack of such functions in native AutoIt and UDF's as well.

For this purpose, I've decided to perform steps below

1. Take some string to check for is it complex number or not.

In general, for flexibility, strings could be like '1 + 2i', ' 1.1 + 2.3i', ' 5 + 2,3j', '6 + 2*j', '5 + 3_i' and so on.

2. Delete all whitespaces (Just for convenience).

$new_string = StringRegExpReplace($string, '\s', '')

3. Check new string obtained with some pattern for reliability.

At the moment I could make as follows:

$pattern = '\d+[\.,\d*]?[^\.]?\+\d+[\.,]?\d*[_\*x][ij]'
StringRegExp($new_string, $pattern)

With strings in examples above such a check works perfect. But, if one wants to check string like '1.1.1 + 2i' one can find that it's meet the pattern either, which is wrong in terms of requirements for real and imaginary parts of complex number (there is no real number with 3 parts).

That's how I see true complex numbers:

1. Any amount of whitespaces in string (they'll be deleted before comparison for match).

2. Leading real number with integer and fraction parts (not necessary that there will be a fraction if number is integer itself) separated by period or comma - Real part.

3. "Plus" or "Minus" signs (+/-).

4. Real number with integer and fraction parts again separated with period or comma - Imaginary part (After all all commas will converted to periods, anyway).

5. "i" or "j" for imaginary unit notation - will be converted to "i" if "j".

I'll be happy to hear/see any advice in right way.

Thanks in advance.

Kaster


Signature

Share this post


Link to post
Share on other sites



nobody knows? :)


Signature

Share this post


Link to post
Share on other sites

Hi guys.

I want to operate with complex numbers due to lack of such functions in native AutoIt and UDF's as well.

For this purpose, I've decided to perform steps below

1. Take some string to check for is it complex number or not.

In general, for flexibility, strings could be like '1 + 2i', ' 1.1 + 2.3i', ' 5 + 2,3j', '6 + 2*j', '5 + 3_i' and so on.

2. Delete all whitespaces (Just for convenience).

$new_string = StringRegExpReplace($string, '\s', '')

3. Check new string obtained with some pattern for reliability.

At the moment I could make as follows:

$pattern = '\d+[\.,\d*]?[^\.]?\+\d+[\.,]?\d*[_\*x][ij]'
StringRegExp($new_string, $pattern)

With strings in examples above such a check works perfect. But, if one wants to check string like '1.1.1 + 2i' one can find that it's meet the pattern either, which is wrong in terms of requirements for real and imaginary parts of complex number (there is no real number with 3 parts).

That's how I see true complex numbers:

1. Any amount of whitespaces in string (they'll be deleted before comparison for match).

2. Leading real number with integer and fraction parts (not necessary that there will be a fraction if number is integer itself) separated by period or comma - Real part.

3. "Plus" or "Minus" signs (+/-).

4. Real number with integer and fraction parts again separated with period or comma - Imaginary part (After all all commas will converted to periods, anyway).

5. "i" or "j" for imaginary unit notation - will be converted to "i" if "j".

I'll be happy to hear/see any advice in right way.

Thanks in advance.

Kaster

You might be able to use some of the regular expression patterns from this example to enable you to do what you want to do.

;
#include <Array.au3>

$new_string = "1 + 2i 3.2i 1.1 - 2.3i 9.8.7 + 2i -2,3j 6.5 + 2*j 10 5 - 3_i " ; "( 1.2+ 3.4i + 5.6- 7j ) /8 - 9i = " ;

#cs
;Remove nn.nn.nn type (n is number.) Removes all alhpa characters except i and j.  Removes all i's and j's if followed by alpha character.
$sREResult = StringRegExpReplace($new_string, "(\d+\.\d+\.\d+\h*[+-]*\h*\d*[.,]*\d*[*_]*[ij])|([a-hk-zA-Z]+)|([ij][a-z])", "")
ConsoleWrite($sREResult & @CRLF)

$sREResult = StringRegExpReplace($sREResult, "((\d*\.*\d*)\h*([+-]*)\h*(\d*[.,]*\d*)([*_]*)([ij]))", "\2\3\4i ")
ConsoleWrite($sREResult & @CRLF)

$sREResult = StringRegExpReplace($sREResult, "([^0-9.,ij+-])", " \1 ")

$sREResult = StringStripWS(StringReplace($sREResult, ",", "."), 7) ; String of complex numbers only.
ConsoleWrite($sREResult & @CRLF)

$sREResult = StringRegExp($sREResult, "(\S+)", 3); Array of complex numbers.

#ce

$sREResult = StringStripWS(StringReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace($new_string, _
        "(\d+\.\d+\.\d+\h*[+-]*\h*\d*[.,]*\d*[*_]*[ij])|([a-hk-zA-Z]+)|([ij][a-z])", ""), _
        "((\d*\.*\d*)\h*([+-]*)\h*(\d*[.,]*\d*)([*_]*)([ij]))", "\2\3\4i "), _
        "([^0-9.,ij+-])", " \1 "), ",", "."), 7) ; String of complex numbers only.
ConsoleWrite($sREResult & @CRLF)

$sREResult = StringRegExp(StringStripWS(StringReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace($new_string, _
        "(\d+\.\d+\.\d+\h*[+-]*\h*\d*[.,]*\d*[*_]*[ij])|([a-hk-zA-Z]+)|([ij][a-z])", ""), _
        "((\d*\.*\d*)\h*([+-]*)\h*(\d*[.,]*\d*)([*_]*)([ij]))", "\2\3\4\6 "), _
        "([^0-9.,ij+-])", " \1 "), ",", "."), 7), "(\S+)", 3); Array of complex numbers.

_ArrayDisplay($sREResult)
;

Share this post


Link to post
Share on other sites

awesome! :)

Many thanks, Malkey. Really great job


Signature

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0