Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 473 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to work with complex numbers in C?

#1
How can I work with complex numbers in C? I see there is a `complex.h` header file, but it doesn't give me much information about how to use it. How to access real and imaginary parts in an efficient way? Is there native functions to get module and phase?
Reply

#2
Complex types are in the C language since C99 standard (`-std=c99` option of GCC). Some compilers may implement complex types even in more earlier modes, but this is non-standard and non-portable extension (e.g. IBM XL, GCC, may be intel,... ).

You can start from

[To see links please register here]

- it gives a description of functions from complex.h

This manual

[To see links please register here]

also gives some info about macros.

To declare a complex variable, use

double _Complex a; // use c* functions without suffix

or

float _Complex b; // use c*f functions - with f suffix
long double _Complex c; // use c*l functions - with l suffix

To give a value into complex, use `_Complex_I` macro from `complex.h`:

float _Complex d = 2.0f + 2.0f*_Complex_I;

*(actually there can be some problems here with `(0,-0i)` numbers and NaNs in single half of complex)*

Module is `cabs(a)`/`cabsl©`/`cabsf(b)`; Real part is `creal(a)`, Imaginary is `cimag(a)`. `carg(a)` is for complex argument.

To directly access (read/write) real an imag part you may use this **unportable** [GCC-extension][1]:

__real__ a = 1.4;
__imag__ a = 2.0;
float b = __real__ a;


[1]:

[To see links please register here]

Reply

#3
The notion of complex numbers was introduced in mathematics, from the need of calculating negative quadratic roots. Complex number concept was taken by a variety of engineering fields.

Today that complex numbers are widely used in advanced engineering domains such as physics, electronics, mechanics, astronomy, etc...

**Real and imaginary part, of a negative square root example:**

#include <stdio.h>
#include <complex.h>

int main()
{
int negNum;

printf("Calculate negative square roots:\n"
"Enter negative number:");

scanf("%d", &negNum);

double complex negSqrt = csqrt(negNum);

double pReal = creal(negSqrt);
double pImag = cimag(negSqrt);

printf("\nReal part %f, imaginary part %f"
", for negative square root.(%d)",
pReal, pImag, negNum);

return 0;
}
Reply

#4
To extract the real part of a complex-valued expression `z`, use the notation as `__real__ z`.
Similarly, use `__imag__` attribute on the `z` to extract the imaginary part.

For example;

__complex__ float z;
float r;
float i;
r = __real__ z;
i = __imag__ z;

r is the real part of the complex number "z"
i is the imaginary part of the complex number "z"
Reply

#5
This code will help you, and it's fairly self-explanatory:

#include <stdio.h> /* Standard Library of Input and Output */
#include <complex.h> /* Standard Library of Complex Numbers */

int main() {

double complex z1 = 1.0 + 3.0 * I;
double complex z2 = 1.0 - 4.0 * I;

printf("Working with complex numbers:\n\v");

printf("Starting values: Z1 = %.2f + %.2fi\tZ2 = %.2f %+.2fi\n", creal(z1), cimag(z1), creal(z2), cimag(z2));

double complex sum = z1 + z2;
printf("The sum: Z1 + Z2 = %.2f %+.2fi\n", creal(sum), cimag(sum));

double complex difference = z1 - z2;
printf("The difference: Z1 - Z2 = %.2f %+.2fi\n", creal(difference), cimag(difference));

double complex product = z1 * z2;
printf("The product: Z1 x Z2 = %.2f %+.2fi\n", creal(product), cimag(product));

double complex quotient = z1 / z2;
printf("The quotient: Z1 / Z2 = %.2f %+.2fi\n", creal(quotient), cimag(quotient));

double complex conjugate = conj(z1);
printf("The conjugate of Z1 = %.2f %+.2fi\n", creal(conjugate), cimag(conjugate));

return 0;
}
 
with:

**`creal(z1)`:** get the real part (for float `crealf(z1)`, for long double `creall(z1)`)

**`cimag(z1)`:** get the imaginary part (for float `cimagf(z1)`, for long double `cimagl(z1)`)

Another important point to remember when working with complex numbers is that functions like `cos()`, `exp()` and `sqrt()` must be replaced with their complex forms, e.g. `ccos()`, `cexp()`, `csqrt()`.
Reply

#6
For convenience, one may include `tgmath.h` library for the type generate macros. It creates the same function name as the double version for all type of variable. For example, For example, it defines a `sqrt()` macro that expands to the `sqrtf()` , `sqrt()` , or `sqrtl()` function, depending on the type of argument provided.

So one don't need to remember the corresponding function name for different type of variables!


#include <stdio.h>
#include <tgmath.h>//for the type generate macros.
#include <complex.h>//for easier declare complex variables and complex unit I

int main(void)
{
double complex z1=1./4.*M_PI+1./4.*M_PI*I;//M_PI is just pi=3.1415...
double complex z2, z3, z4, z5;

z2=exp(z1);
z3=sin(z1);
z4=sqrt(z1);
z5=log(z1);

printf("exp(z1)=%lf + %lf I\n", creal(z2),cimag(z2));
printf("sin(z1)=%lf + %lf I\n", creal(z3),cimag(z3));
printf("sqrt(z1)=%lf + %lf I\n", creal(z4),cimag(z4));
printf("log(z1)=%lf + %lf I\n", creal(z5),cimag(z5));

return 0;
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through