---
+ Post New Thread
Results 1 to 9 of 9
  1. #1
    Full Member level 2
    Points: 2,523, Level: 11
    jakjoud's Avatar
    Join Date
    Feb 2005
    Location
    Marrakech
    Posts
    129
    Helped
    11 / 11
    Points
    2,523
    Level
    11

    Programming complex numbers in C

    Hi every body,
    OK, so this is my newest problem. In fact I'm trying to make an algorithm for resolution of an equation of nonlinear propagation called KZK, in Matlab I've made already the program, but the problem is that's so slow so I'm searching for programming in C, I've found an easy algorithm for the inverse of a tri-diagonal matrix it's called Thomas but I'm in need of complex numbers I'm hearding about a librarie in C can you give a link or something else for this. I'm open for other solutions, And I'm programmin under turbo C in windows.
    Thanks a lot.

  2. #2
    Advanced Member level 5
    Points: 40,208, Level: 49

    Join Date
    Apr 2002
    Location
    USA
    Posts
    3,944
    Helped
    649 / 649
    Points
    40,208
    Level
    49

    Programming complex numbers in C

    How about a different approach? Since speed is important to you, consider using a C compiler that generates faster code and has native support for complex arithmetic. I use MinGW, a version of GCC for Windows. You can do complex arithmetic easily with the + - * / operators, and call various complex math functions such as csin, ccos, catan, cexp, cpow, etc. MinGW is freeware.
    http://www.mingw.org/
    http://gcc.gnu.org/onlinedocs/gcc-3....x.html#Complex

    Example:
    Code:
    #include <stdio.h>
    #include <complex.h>
    #include <math.h>
    
    int main(void)
    {
      _Complex double a=1.3+2.5i, b=-2.7-0.3i, sum, prod, foo;
    
      printf("a = %g + %gi\n", creal(a), cimag(a));
      printf("b = %g + %gi\n", creal(b), cimag(b));
      sum  = a + b;
      prod = a * b;
      printf("a+b = %g + %gi\n", creal(sum), cimag(sum));
      printf("a*b = %g + %gi\n", creal(prod), cimag(prod));
      foo = csin(a);
      printf("csin(a) = %g + %gi\n", creal(foo), cimag(foo));
      return 0;
    }
    Compiled with this command line: gcc -Wall example.c

    Outputs this:

    a = 1.3 + 2.5i
    b = -2.7 + -0.3i
    a+b = -1.4 + 2.2i
    a*b = -2.76 + -7.14i
    csin(a) = 5.90882 + 1.61842i

    Too bad printf() doesn't support complex, so you have to use creal() and cimag().

    There are other Windows GCC packages, such as cygwin, but I prefer MinGW. If you've never used any version of GCC, expect some learning curve.


    1 members found this post helpful.

    •   Alt26th January 2006, 17:13

      advertising

        
       

  3. #3
    Full Member level 2
    Points: 2,523, Level: 11
    jakjoud's Avatar
    Join Date
    Feb 2005
    Location
    Marrakech
    Posts
    129
    Helped
    11 / 11
    Points
    2,523
    Level
    11

    Programming complex numbers in C

    Thank you a lot but I've never used such editor, please I need some informations about, and I didn't find some links to download the 'Mingw'



  4. #4
    Advanced Member level 5
    Points: 40,208, Level: 49

    Join Date
    Apr 2002
    Location
    USA
    Posts
    3,944
    Helped
    649 / 649
    Points
    40,208
    Level
    49

    Programming complex numbers in C

    To get the MinGW base installtion, go to http://www.mingw.org/, click the "Download" link, scroll down to MinGW-3.1.0-1.exe, download it, and install it. I think that's sufficient, although it's been a few years since I installed mine, so I forget if I had to set any environment variables.

    Now use your favorite programmer's editor to create your source code (or copy my example.c), and then run that short blue command line to compile it.

    There is a lot of stuff on that MinGW "Download" page. If you find the instructions overwhelming or too confusing, then maybe you should not attempt to use MinGW. Much of GCC documentation is just like that. Some people don't have spare time to learn it.



    •   Alt29th January 2006, 01:55

      advertising

        
       

  5. #5
    Banned
    Points: 5,790, Level: 18

    Join Date
    Jun 2005
    Location
    scotland uk
    Posts
    646
    Helped
    95 / 95
    Points
    5,790
    Level
    18

    Re: Programming complex numbers in C

    i think some answers you had are missleading

    much faster native code in c++ can be made
    using other compilers

    even microsoft free compilers work better

    and have access to complex mmx and sse instructions that do these kind of algurithms and math in hardware with little or none noticable
    'instertion loss'
    in your code wrapper

    my advice is dont limit your scope with redundant c code examples
    other than to gain insight
    and move on

    be exploritory and experiment using simple exe programs set up to one facet of
    each part much like workings in a maths expression
    joining them together to fuse your code to a working expression
    in c++ once you have this
    you can find ways to optimise this

    like learing the rubix cube you soon realise there are 'shortcuts'
    to a solve situation



    look further than c only

    floating point is handy ,,,, c isnt nor is mingw langauge

    its a step back ... i realise you were offererd becouse

    you didnt express you need's too well to the point

    the other posters think your a bit less cleaver than you need to be

    i realise your not becouse i read your request

    better stick to fast high level langauage


    if you format a drive in dos it takes 10 - 20 mins per 10 GB

    in windows 1 - 2 seconds using high level drivers and hardware

    so this makes the point

    dos = c , windows = c++, windows + internet = java etc...
    and java codec is coming so be warned
    learn java NOW...!!!



  6. #6
    Advanced Member level 5
    Points: 40,208, Level: 49

    Join Date
    Apr 2002
    Location
    USA
    Posts
    3,944
    Helped
    649 / 649
    Points
    40,208
    Level
    49

    Programming complex numbers in C

    If you don't want to switch compilers or language, you can store the complex numbers in structs, and write functions that help you do complex operations. A good optimizing compiler will automatically inline the function calls and eliminate the temporary structs. (However, Turbo C has a weak optimizer.)

    Here's a hastily-written example (could have bugs):
    Code:
    #include <stdio.h>
    
    typedef struct
    {
      double real, imag;
    } complex;
    
    complex cadd(complex a, complex b)
    {
      complex add;
      add.real = a.real + b.real;
      add.imag = a.imag + b.imag;
      return add;
    }
    
    complex csub(complex a, complex b)
    {
      complex sub;
      sub.real = a.real - b.real;
      sub.imag = a.imag - b.imag;
      return sub;
    }
    
    complex cmul(complex a, complex b)
    {
      complex mul;
      mul.real = a.real * b.real - a.imag * b.imag;
      mul.imag = a.real * b.imag + a.imag * b.real;
      return mul;
    }
    
    complex cdiv(complex a, complex b)
    {
      complex div;
      div.real = (a.real * b.real + a.imag * b.imag) / (b.real * b.real + b.imag * b.imag);
      div.imag = (a.imag * b.real - a.real * b.imag) / (b.real * b.real + b.imag * b.imag);
      return div;
    }
    
    int main(void)
    {
      complex a={1.3, 2.5}, b={-2.7, -0.3}, add, sub, mul, div;
    
      printf("a   = %g + %gi\n", a.real, a.imag);
      printf("b   = %g + %gi\n", b.real, b.imag);
      add = cadd(a, b);
      sub = csub(a, b);
      mul = cmul(a, b);
      div = cdiv(a, b);
      printf("a+b = %g + %gi\n", add.real, add.imag);
      printf("a-b = %g + %gi\n", sub.real, sub.imag);
      printf("a*b = %g + %gi\n", mul.real, mul.imag);
      printf("a/b = %g + %gi\n", div.real, div.imag);
      return 0;
    }
    Output:

    a = 1.3 + 2.5i
    b = -2.7 + -0.3i
    a+b = -1.4 + 2.2i
    a-b = 4 + 2.8i
    a*b = -2.76 + -7.14i
    a/b = -0.577236 + -0.861789i



  7. #7
    Full Member level 2
    Points: 2,523, Level: 11
    jakjoud's Avatar
    Join Date
    Feb 2005
    Location
    Marrakech
    Posts
    129
    Helped
    11 / 11
    Points
    2,523
    Level
    11

    Programming complex numbers in C

    Ok thanks.
    I think that I'm gonna use VC++, that's because I can program the +,-,... for either complex numbers and matrix. It d help me too to make easily graphics
    Thanks friends



  8. #8
    Advanced Member level 5
    Points: 40,208, Level: 49

    Join Date
    Apr 2002
    Location
    USA
    Posts
    3,944
    Helped
    649 / 649
    Points
    40,208
    Level
    49

    Programming complex numbers in C

    Yes, that's a good approach too. You can learn C++ and use operator overloading, like this:
    http://msdn.microsoft.com/library/de..._operators.asp

    By the way, that C++ example compiles fine in MinGW using this command line:
    g++ -Wall example.cpp

    MinGW can build Windows GUI apps, but it doesn't support some Microsoft stuff like MFC.



  9. #9
    Full Member level 4
    Points: 2,402, Level: 11

    Join Date
    Feb 2006
    Posts
    199
    Helped
    19 / 19
    Points
    2,402
    Level
    11

    Re: Programming complex numbers in C

    Ok thanks.
    I think that I'm gonna use VC++, that's because I can program the +,-,... for either complex numbers and matrix. It d help me too to make easily graphics
    Thanks friends
    I believe there is a complex class in the standard C++ Library. I used it briefly in VC++ 6. I don't know if .NET has a complex class implemented in it. If it does try using it in C#. You can get a free C# compiler with a very good GUI front end that is also free. Just google for it. C++ is a dead end. C will always be around for micros, C# and Java for computers and C++ maybe for OS low-level guys, both of whom will be working for Microsoft in the cellar.



+ Post New Thread
Please login