C Standard Library Complex H
`` is a header file in the C standard library used to support complex number operations.
`` was introduced in the C99 standard and provides a set of types, macros, and functions for defining and manipulating complex numbers.
### 1. **Complex Number Types**
`` defines the following complex number types:
* `float complex`: Single-precision complex number.
* `double complex`: Double-precision complex number.
* `long double complex`: Long double-precision complex number.
These types are actually macros defined in the C standard library, which expand to `_Complex float`, `_Complex double`, and `_Complex long double` respectively.
* * *
### 2. **Complex Constants**
`` defines the following macro to represent the imaginary unit `i`:
* `I`: Represents the imaginary unit `i`, which is `sqrt(-1)`.
## Example
double complex z =3.0+4.0* I;// Represents the complex number 3 + 4i
### 3. **Complex Number Manipulation Functions**
`` provides a set of functions for manipulating complex numbers, including arithmetic operations, trigonometric functions, exponential functions, and logarithmic functions. Here are some commonly used functions:
#### Arithmetic Operations
| Function | Description |
| --- | --- |
| `creal(z)` | Returns the real part of complex number `z` |
| `cimag(z)` | Returns the imaginary part of complex number `z` |
| `cabs(z)` | Returns the modulus (absolute value) of complex number `z` |
| `carg(z)` | Returns the argument (phase) of complex number `z` |
| `conj(z)` | Returns the complex conjugate of complex number `z` |
| `cproj(z)` | Returns the projection of complex number `z` |
#### Trigonometric Functions
| Function | Description |
| --- | --- |
| `csin(z)` | Returns the sine of complex number `z` |
| `ccos(z)` | Returns the cosine of complex number `z` |
| `ctan(z)` | Returns the tangent of complex number `z` |
#### Exponential and Logarithmic Functions
| Function | Description |
| --- | --- |
| `cexp(z)` | Returns the exponential value of complex number `z` |
| `clog(z)` | Returns the natural logarithm of complex number `z` |
#### Power Functions
| Function | Description |
| --- | --- |
| `cpow(z1, z2)` | Returns `z2` power of complex number `z1` |
#### Square Root Functions
| Function | Description |
| --- | --- |
| `csqrt(z)` | Returns the square root of complex number `z` |
* * *
### 4. **Example**
The following is an example using `, demonstrating how to define and manipulate complex numbers:
## Example
#include
#include
int main(){
// Define complex numbers
double complex z1 =3.0+4.0* I;
double complex z2 =1.0-2.0* I;
// Arithmetic operations
double complex sum = z1 + z2;
double complex product = z1 * z2;
// Output results
printf("z1 = %.2f + %.2fin",creal(z1),cimag(z1));
printf("z2 = %.2f + %.2fin",creal(z2),cimag(z2));
printf("Sum = %.2f + %.2fin",creal(sum),cimag(sum));
printf("Product = %.2f + %.2fin",creal(product),cimag(product));
// Modulus and argument
printf("|z1| = %.2fn",cabs(z1));
printf("Phase of z1 = %.2f radiansn",carg(z1));
// Exponential function
double complex exp_z1 =cexp(z1);
printf("exp(z1) = %.2f + %.2fin",creal(exp_z1),cimag(exp_z1));
return 0;
}
**Output**:
z1 = 3.00 + 4.00i z2 = 1.00 + -2.00iSum = 4.00 + 2.00iProduct = 11.00 + -2.00i|z1| = 5.00Phase of z1 = 0.93 radians exp(z1) = -13.13 + -15.20i
* * *
### 5. **Notes**
* `` is only available in C99 and later versions.
* The use of complex number types and functions requires including the `` header file.
* Complex number operations may have lower performance than real number operations, especially when involving large amounts of computation.
* * *
### 6. **Mathematical Background of Complex Number Operations**
The general form of a complex number is `a + bi`, where:
* `a` is the real part, `b` is the imaginary part.
* `i` is the imaginary unit, satisfying `iΒ² = -1`.
The modulus (absolute value) of a complex number is:
|z| = sqrt(aΒ² + bΒ²)
The argument (phase) of a complex number is:
arg(z) = atan2(b, a)
The exponential form of a complex number is:
exp(z) = exp(a) * (cos(b) + i * sin(b))
YouTip