C library - conj() function



The C complex library conj() function used to calculate the complex conjugate of z (complex number) by reversing the sign of the imaginary part. The conjugate of a complex number is the number with an equal real part and an imaginary part equal in magnitude but opposite in sign.

This function is depends on the type of z(complex number). If the z is of the "float" type or float imaginary, we can use conjf() to compute the conjugate, For type long double, use conjl(), and for type double, use conj().

Syntax

Following is the C library syntax of conj() function −

double complex conj( double complex z );

Parameters

This function accepts a single parameter −

  • Z − It represent a complex number for which we want to compute the conjugate.

Return Value

This function returns the complex conjugate of z(complex number).

Example 1

Following is the basic c program to demonstrate the use of conj() to calculate the conjugate of z.

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

int main() {
   double real = 3.0;
   double imag = 4.0;

   // Use the CMPLX function to create complex number
   double complex z = CMPLX(real, imag);

   printf("The complex number is: %.2f + %.2fi\n", creal(z), cimag(z));

   // Calculate conjugate
   double complex conjugate = conj(z);

   printf("The conjugate of the complex number is: %.2f + %.2fi\n", creal(conjugate), cimag(conjugate));

   return 0;
}

Output

Following is the output −

The complex number is: 3.00 + 4.00i
The conjugate of the complex number is: 3.00 + -4.00i

Example 2

Let's see another example, we uses the conj() function to calculate conjugate of sum of two complex number.

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

int main() {
   double complex z1 = 1.0 + 3.0 * I;
   double complex z2 = 1.0 - 4.0 * I;
   
   printf("Complex numbers: Z1 = %.2f + %.2fi\tZ2 = %.2f %+.2fi\n", creal(z1), cimag(z1), creal(z2), cimag(z2));

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


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

   return 0;
}

Output

Following is the output −

Complex numbers: Z1 = 1.00 + 3.00i	Z2 = 1.00 -4.00i
The Z: Z1 + Z2 = 2.00 -1.00i
The conjugate of Z = 2.00 +1.00i

Example 3

The below example calculates the conjugate of a complex number which is of type long double.

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

int main(void) {
   long double complex z = 3.0 + -4.0*I;
   
   // Calculate the conjugate
   long double conj = conjl(z);
   
   printf("Complex number: %.2Lf + %.2Lfi\n", creall(z), cimagl(z));
   printf("Conjugate of z: %.2Lf + %.2Lfi\n", creall(conj), cimagl(conj));
   return 0;
}

Output

Following is the output −

Complex number: 3.00 + -4.00i
Conjugate of z: 3.00 + 0.00i
c_library_complex_h.htm
Advertisements