# Complex numbers libraryComplex.h

A complex number is a number that can be expressed in the form: `re + im * i`, where `re` and `im` are real numbers and `i` is the imaginary unit, that satisfies the equation `i ^{2} = −1`. In this expression,

`re`is the real part and

`im`is the imaginary part of the complex number.

Complex numbers extend the concept of the one-dimensional number line to the two-dimensional complex plane (also called Argand plane) by using the horizontal axis for the real part and the vertical axis for the imaginary part. The complex number `re + im * i` can be identified with the point (`re`, `im`) in the complex plane. A complex number whose real part is zero is said to be purely imaginary, whereas a complex number whose imaginary part is zero is a real number. In this way, the complex numbers contain the ordinary real numbers while extending them in order to solve problems that cannot be solved with real numbers alone.

Complex numbers have practical applications in many fields, including mathematics, physics, chemistry, biology, economics, electrical engineering, and statistics.

## Contents

- Complex number structure
- Arithmetic operations
- Unary operations
- Negative value
- Complex conjugate value
- Square root
- Square value
- Inverse value
- Binary operations
- Addition
- Subtraction
- Multiplication
- Division
- Complex number properties
- Magnitude value
- Argument value
- Checks
- Check for zero number
- Check for equality of the numbers
- Check for negativity of the numbers

## Function list

C function name | Functions | C++ function name | Functions |
---|---|---|---|

Add | 2 functions | Add | 2 functions |

Argument | 2 functions | Argument | 2 functions |

Conj | 2 functions | Conj | 2 functions |

Div | 2 functions | Div | 2 functions |

Inverse | 2 functions | Inverse | 2 functions |

IsEqual | 2 functions | IsEqual | 2 functions |

IsNeg | 2 functions | IsNeg | 2 functions |

IsZero | 2 functions | IsZero | 2 functions |

Magnitude | 2 functions | Magnitude | 2 functions |

Mul | 2 functions | Mul | 2 functions |

Neg | 2 functions | Neg | 2 functions |

Sqr | 2 functions | Sqr | 2 functions |

Sqrt | 2 functions | Sqrt | 2 functions |

Sub | 2 functions | Sub | 2 functions |

C function name | Functions | C++ function name | Functions |

# Complex number structure

Complex number structure has two elements, which represent real and imaginary part of complex numbers. You may set these fields directly or change their values in your code. Complex number structure is defined below.

C/C++Single precision complex number struct cmplx32_t { flt32_t re; flt32_t im; }; Double precision complex number struct cmplx64_t { flt64_t re; flt64_t im; };

Both 32-bit and 64-bit complex number structures have following members:

- re - real part of complex number
- im - imaginary part of complex number

# Arithmetic operations

Arithmetic operations to complex numbers do basic math processing such as addition, subtraction, changing sign, extracting square root and so on.

## Unary operations

Unary operations change value of complex number and store the result into the same number.

### Negative value

Cvoid Complex_Neg_flt32 (struct cmplx32_t *number); void Complex_Neg_flt64 (struct cmplx64_t *number);

C++void Complex::Neg (cmplx32_t *number); void Complex::Neg (cmplx64_t *number);

Description: Change sign of complex number.

Parameters:

- number - pointer to the complex number

Return value: None.

### Complex conjugate value

Cvoid Complex_Conj_flt32 (struct cmplx32_t *number); void Complex_Conj_flt64 (struct cmplx64_t *number);

C++void Complex::Conj (cmplx32_t *number); void Complex::Conj (cmplx64_t *number);

Description: Change sign of immaginary part of complex number.

Parameters:

- number - pointer to the complex number

Return value: None.

### Square root

Cvoid Complex_Sqrt_flt32 (struct cmplx32_t *number); void Complex_Sqrt_flt64 (struct cmplx64_t *number);

C++void Complex::Sqrt (cmplx32_t *number); void Complex::Sqrt (cmplx64_t *number);

Description: Compute positive square root of complex number and store it into the same number.

Parameters:

- number - pointer to the complex number

Return value: None.

### Square value

Cvoid Complex_Sqr_flt32 (struct cmplx32_t *number); void Complex_Sqr_flt64 (struct cmplx64_t *number);

C++void Complex::Sqr (cmplx32_t *number); void Complex::Sqr (cmplx64_t *number);

Description: Compute square value of complex number and store it into the same number.

Parameters:

- number - pointer to the complex number

Return value: None.

### Inverse value

Cvoid Complex_Inverse_flt32 (struct cmplx32_t *number); void Complex_Inverse_flt64 (struct cmplx64_t *number);

C++void Complex::Inverse (cmplx32_t *number); void Complex::Inverse (cmplx64_t *number);

Description: Find inverse value of complex number, which is equal to `1 / (re + im * i)`, and store it into the same number.

Parameters:

- number - pointer to the complex number

Return value: None.

## Binary operations

Binary operations with complex numbers take two operands, which called target and source. Then they apply complex arithmetic operations to target number, using source complex number as second operand, and then store the result into target operand.

### Addition

Cvoid Complex_Add_flt32 (struct cmplx32_t *target, const struct cmplx32_t *source); void Complex_Add_flt64 (struct cmplx64_t *target, const struct cmplx64_t *source);

C++void Complex::Add (cmplx32_t *target, const cmplx32_t *source); void Complex::Add (cmplx64_t *target, const cmplx64_t *source);

Description: Add source complex number to target complex number and store the result into the same place (into target number).

Parameters:

- target - pointer to target complex number
- source - pointer to source complex number

Return value: None.

### Subtraction

Cvoid Complex_Sub_flt32 (struct cmplx32_t *target, const struct cmplx32_t *source); void Complex_Sub_flt64 (struct cmplx64_t *target, const struct cmplx64_t *source);

C++void Complex::Sub (cmplx32_t *target, const cmplx32_t *source); void Complex::Sub (cmplx64_t *target, const cmplx64_t *source);

Description: Subtract source complex number from target complex number and store the result into the same place (into target number).

Parameters:

- target - pointer to target complex number
- source - pointer to source complex number

Return value: None.

### Multiplication

Cvoid Complex_Mul_flt32 (struct cmplx32_t *target, const struct cmplx32_t *source); void Complex_Mul_flt64 (struct cmplx64_t *target, const struct cmplx64_t *source);

C++void Complex::Mul (cmplx32_t *target, const cmplx32_t *source); void Complex::Mul (cmplx64_t *target, const cmplx64_t *source);

Description: Multiply two complex numbers and store the result into the same place (into target number).

Parameters:

- target - pointer to target complex number
- source - pointer to source complex number

Return value: None.

### Division

Cvoid Complex_Div_flt32 (struct cmplx32_t *target, const struct cmplx32_t *source); void Complex_Div_flt64 (struct cmplx64_t *target, const struct cmplx64_t *source);

C++void Complex::Div (cmplx32_t *target, const cmplx32_t *source); void Complex::Div (cmplx64_t *target, const cmplx64_t *source);

Description: Divide target complex number by source number and store the result into the same place (into target number).

Parameters:

- target - pointer to target complex number
- source - pointer to source complex number

Return value: None.

# Complex number properties

These function compute magnitude and argument value of complex numbers.

## Magnitude value

Cflt32_t Complex_Magnitude_flt32 (const struct cmplx32_t *number); flt64_t Complex_Magnitude_flt64 (const struct cmplx64_t *number);

C++flt32_t Complex::Magnitude (const cmplx32_t *number); flt64_t Complex::Magnitude (const cmplx64_t *number);

Description: Calculate magnitude value of complex number.

Parameters:

- number - pointer to the complex number

Return value:

- Magnitude value of complex number.
- Inf (infinity) if magnitude is too big and cannot be represented in selected floating-point type.
- Subnormal value in case of underflow.
- 0 (zero) if complex number magnitude can not be represented as subnormal value, or it is equal to zero.

## Argument value

Cflt32_t Complex_Argument_flt32 (const struct cmplx32_t *number); flt64_t Complex_Argument_flt64 (const struct cmplx64_t *number);

C++flt32_t Complex::Argument (const cmplx32_t *number); flt64_t Complex::Argument (const cmplx64_t *number);

Description: Calculate argument value of complex number.

Parameters:

- number - pointer to the complex number

Return value:

- Argument value of complex number in range [-Pi,+Pi].
- NaN (not a number) if both real and imaginary parts are equal to zero.

# Checks

Check functions compare complex numbers and return relation state between them. They also check complex number value for special cases like zero value.

## Check for zero number

Cbool Complex_IsZero_flt32 (const struct cmplx32_t *number); bool Complex_IsZero_flt64 (const struct cmplx64_t *number);

C++bool Complex::IsZero (const cmplx32_t *number); bool Complex::IsZero (const cmplx64_t *number);

Description: Check complex number for zero value.

Parameters:

- number - pointer to the complex number

Return value:

- TRUE (1) if both real and imaginary parts of complex number are equal to zero.
- FALSE (0) in other case.

## Check for equality of the numbers

Cbool Complex_IsEqual_flt32 (const struct cmplx32_t *target, const struct cmplx32_t *source); bool Complex_IsEqual_flt64 (const struct cmplx64_t *target, const struct cmplx64_t *source);

C++bool Complex::IsEqual (const cmplx32_t *target, const cmplx32_t *source); bool Complex::IsEqual (const cmplx64_t *target, const cmplx64_t *source);

Description: Check complex numbers for equality.

Parameters:

- target - pointer to target complex number
- source - pointer to source complex number

Return value:

- TRUE (1) if both real and imaginary part of target complex number are equal to corresponding values of source complex number.
- FALSE (0) if real and imaginary part of complex numbers are not equal.

## Check for negativity of the numbers

Cbool Complex_IsNeg_flt32 (const struct cmplx32_t *target, const struct cmplx32_t *source); bool Complex_IsNeg_flt64 (const struct cmplx64_t *target, const struct cmplx64_t *source);

C++bool Complex::IsNeg (const cmplx32_t *target, const cmplx32_t *source); bool Complex::IsNeg (const cmplx64_t *target, const cmplx64_t *source);

Description: Check complex numbers for negativity (one value is negative variant of another).

Parameters:

- target - pointer to target complex number
- source - pointer to source complex number

Return value:

- TRUE (1) if one value is negative variant of another.
- FALSE (0) in other case.