Programming

C에서 복소수로 작업하는 방법?

procodes 2020. 7. 21. 22:15
반응형

C에서 복소수로 작업하는 방법?


C에서 복소수로 작업하려면 어떻게해야합니까? 나는이 볼 complex.h헤더 파일은, 그러나 그것은 나에게 그것을 사용하는 방법에 대한 많은 정보를 제공하지 않습니다. 효율적인 방법으로 실제 및 가상 부품에 액세스하는 방법은 무엇입니까? 모듈과 위상을 얻는 기본 함수가 있습니까?


이 코드는 당신에게 도움이 될 것이며 상당히 설명이 필요합니다 :

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

  와:

creal(z1): 실수 부분을 얻으십시오 (float crealf(z1), long double의 경우 creall(z1))

cimag(z1): 허수 부 (float cimagf(z1), long double의 경우 cimagl(z1))

복잡한 숫자와 함께 작업하는 기능이 좋아하는 때 또 다른 중요한 점은 기억해야 할 cos(), exp()그리고 sqrt()예를 들어, 복잡한 형태로 교체해야합니다 ccos(), cexp(), csqrt().


복합 유형은 C99 표준 ( -std=c99GCC 옵션) 이후 C 언어로 제공됩니다 . 일부 컴파일러는 이전 모드에서도 복잡한 유형을 구현할 수 있지만 이는 비표준 및 이식 불가능한 확장입니다 (예 : IBM XL, GCC, 인텔 일 수 있음).

http://en.wikipedia.org/wiki/Complex.h 에서 시작할 수 있습니다. complex.h의 함수에 대한 설명을 제공합니다.

이 매뉴얼 http://pubs.opengroup.org/onlinepubs/009604499/basedefs/complex.h.html 은 매크로에 대한 정보도 제공합니다.

복잡한 변수를 선언하려면

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

또는

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

복잡한 값을 제공하려면 다음 _Complex_I에서 매크로를 사용하십시오 complex.h.

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

(실제로 (0,-0i)단일 절반의 숫자와 NaN에 문제가있을 수 있습니다 )

모듈은 cabs(a)/ cabsl(c)/ cabsf(b); 진짜 부분은 creal(a)상상입니다 cimag(a). carg(a)복잡한 주장에 대한 것입니다.

실제 imag 부분에 직접 액세스 (읽기 / 쓰기)하려면이 이식 불가능한 GCC 확장을 사용할 수 있습니다 .

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

Complex.h

#include <stdio.h>      /* Standard Library of Input and Output */
#include <complex.h>    /* Standart 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));
}

편의상, tgmath.h타입 생성 매크로를위한 라이브러리를 포함 할 수있다 . 모든 유형의 변수에 대해 이중 버전과 동일한 함수 이름을 작성합니다. 예를 들어, 예를 들어, 그것은 정의 sqrt()받는 확장 매크로 sqrtf(), sqrt()또는 sqrtl()제공 인자의 유형에 따라, 기능.

따라서 다른 유형의 변수에 해당하는 함수 이름을 기억할 필요가 없습니다!

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

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

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"

참고URL : https://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c

반응형