Results 1 to 3 of 3

Thread: Complex Numbers

  1. #1
    Join Date
    Apr 2007
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Complex Numbers

    Anyone have a good QtRegEx expression for parsing a string into a complex number?
    I'm looking for formats including:

    A+Bi
    A
    Bi
    i

    where A and B are floating point numbers including embedded signs and exponents, i is either "i" or "j". + is either "+" or "-". And spaces can be sprinkled between the patterns.

    I've tried "^\\s*(\\S*)\\s*([+-]{0,1})\\s*(\\S*)\\s*([iIjJ])\\s$" but that fails, I think because all the patterns are optional.

    I can also match several special cases in sequence but that seems inelegant.

    Thanks,
    Max

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Complex Numbers

    First of all you can strip all spaces, so that it is easier to parse the string.

    Then try something like this:
    ((\\d+)|(\\d*(i|j)))?((+|-)((\\d+)|(\\d*(i|j))))+

    Seems complicated but should match even a sequence of complex numbers. If you want to match at most one Re and at most one Im component, you can simplify it a bit, but I can't tell you how because my head aches Maybe someone else will continue from here...

  3. #3
    Join Date
    Apr 2007
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Complex Numbers

    Thanks for your help. I came up with something similar yesterday after I posted the note here:

    static QRegExp rx1("^\\s*([+-]?)\\s*(\\d+(?:\\.\\d+)?)([eE][+-]?\\d+)?([iIjJ])?\\s*"); // matches floating point number w/ possible imag. qualifier
    if (rx1.indexIn(str) == -1) return false;
    QString sFull = rx1.cap(0);
    QString sSign = rx1.cap(1);
    QString sMant = rx1.cap(2);
    QString sExp = rx1.cap(3);
    QString sImag = rx1.cap(4);

    QString sNew = sSign+sMant+sExp;

    bool ok;
    double x = sNew.toDouble(&ok);

    So basically I find the separate sign, mantissa, exponent and imaginary portions of a single term, skipping whitespace. Then paste the first 3 back together and let .toDouble decide weather it qualifies as a valid double. Then I call this again on the last section.

    Thanks again for your help

Similar Threads

  1. Replies: 1
    Last Post: 13th March 2007, 18:33
  2. Column with numbers
    By ederbs in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2006, 23:03
  3. QT4 layout of complex dialog is very slow
    By cboles in forum Qt Programming
    Replies: 15
    Last Post: 28th April 2006, 20:57
  4. a Text Editor with line numbers...
    By fullmetalcoder in forum Qt Programming
    Replies: 47
    Last Post: 5th April 2006, 12:10
  5. QTextBrowser with line numbers
    By sreedhar in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2006, 14:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.