C 庫 - conj() 函式



C 的complexconj() 函式用於透過反轉虛部的符號來計算 z(複數)的共軛複數。複數的共軛是指實部相等且虛部大小相等但符號相反的數。

此函式取決於 z(複數)的型別。如果 z 是“float”型別或浮點虛數,我們可以使用conjf()計算共軛,對於 long double 型別,使用conjl(),對於 double 型別,使用conj()

語法

以下是 conj() 函式的 C 庫語法 -

double complex conj( double complex z );

引數

此函式接受一個引數 -

  • Z - 它表示我們要計算其共軛的複數。

返回值

此函式返回 z(複數)的共軛複數。

示例 1

以下是用 conj() 計算 z 的共軛的基本 c 程式。

#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;
}

輸出

以下是輸出 -

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

示例 2

讓我們看另一個例子,我們使用 conj() 函式計算兩個複數之和的共軛。

#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;
}

輸出

以下是輸出 -

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

示例 3

以下示例計算一個型別為 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;
}

輸出

以下是輸出 -

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

© . All rights reserved.