Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Programming complex numbers in C

Status
Not open for further replies.

jakjoud

Full Member level 2
Joined
Feb 6, 2005
Messages
129
Helped
10
Reputation
20
Reaction score
1
Trophy points
1,298
Location
Marrakech
Activity points
954
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.
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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.
https://www.mingw.org/
https://gcc.gnu.org/onlinedocs/gcc-3.4.5/gcc/Complex.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.
 

    jakjoud

    Points: 2
    Helpful Answer Positive Rating

jakjoud

Full Member level 2
Joined
Feb 6, 2005
Messages
129
Helped
10
Reputation
20
Reaction score
1
Trophy points
1,298
Location
Marrakech
Activity points
954
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'
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
To get the MinGW base installtion, go to https://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.
 

VSMVDD

Banned
Joined
Jun 12, 2005
Messages
646
Helped
92
Reputation
184
Reaction score
34
Trophy points
1,308
Location
scotland uk
Activity points
0
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...!!!
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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
 

jakjoud

Full Member level 2
Joined
Feb 6, 2005
Messages
129
Helped
10
Reputation
20
Reaction score
1
Trophy points
1,298
Location
Marrakech
Activity points
954
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
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176

jhbbunch

Full Member level 4
Joined
Feb 21, 2006
Messages
199
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
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.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top