Hướng dẫn complex numbers in c++ - số phức trong c ++

Hướng dẫn complex numbers in c++ - số phức trong c ++

Nhận cuốn sách này -> Vấn đề về Array: Đối với các cuộc phỏng vấn và lập trình cạnh tranh

Một số phức là một số có thể được viết trong Mẫu X+Yi trong đó X và Y là số thực và tôi là một số tưởng tượng.

  • Do đó, một số phức là sự kết hợp của:
  • số thực.
  • số tưởng tượng.

Example:

6+2i    //here i=√-1
        //6 is real part and 2i is imaginary

Biểu diễn các số phức trong c

Bằng ngôn ngữ C, chúng ta có thể đại diện hoặc có thể làm việc trên các số phức tạp bằng cách sử dụng

I) Cấu trúc, tính năng C i) Thư viện

#include
2structure, a C feature
II)
#include
2 library

Đại diện cho số phức tạp sử dụng cấu trúc

Trong đó, chúng tôi sẽ tạo ra một cấu trúc của số phức sẽ giữ phần thực và phần tưởng tượng.

Example:

struct complex
{
    int real, imag; 
};

Ở đây, struct là một từ khóa để tạo một cấu trúc và phức tạp là tên của cấu trúc. Real là biến để giữ phần thực và hình ảnh là một biến để giữ phần tưởng tượng. Real và hình ảnh có thể thuộc bất kỳ loại dữ liệu nào (int, float, gấp đôi).
real is variable to hold the real part and imag is a variable to hold the imaginary part.
real and imag can be of any data type (int, float, double).

Đại diện cho các số phức tạp bằng thư viện

Thư viện chứa nhiều chức năng sẵn có giúp tạo hoặc tính toán các số phức.

#include

Như chúng ta biết rằng các số phức được biểu diễn dưới dạng x+yi. Tùy thuộc vào loại x và y, có ba loại dữ liệu trong C cho các số phức: -double phức -phức hợp phức tạp -long kép của khởi tạo các số phức:
Depending on type of x and y there are three data types in C for complex numbers:
-double complex
-float complex
-long double complex
Example of initialization of complex numbers:

double complex c1=5.0+2.0*I;   //I is imaginary part
double complex c2=7.0-5.0*I;

Nó cung cấp các hàm hàm mũ, chức năng công suất, hàm lượng giác và một số hàm thao tác.exponential functions, power functions, trigonometric functions, and some manipulation function.

**Manipulation functions**
  creal() :computes the real part of the funtion.
  crealf()     //for float
  creall()     //for long double.

  cimag() :computes the imaginary part of the function.
  cimagf()     //for float
  cimagl()     //for long double

**Exponential functions**
  cexp() :computes the complex base-e exponential
  cexpf()      //for float
  cexpl()      //for double

**Trigonometric functions**
  csin() :computes the complex sine
  csinf()      //for float
  csinl()      //for double

  ccos() :computes the complex cosine
  ccosf()      //for float
  ccosl()      //for double
  
  ctan() :computes the complex tangent
  ctanf()      //for float
  ctanl()      //for double
  
  casin() :computes the complex arc sine
  casinf()    //for float
  casinl()    //for double

  cacos() :computes the complex arc cosine
  cacosf()    //for float
  cacosl()    //for double
  
  catan() :computes the complex arc tangent
  catanf()    //for float
  catanl()    //for double
  
**Power function**
  cpow() :computes the complex power function
  cpowf()     //for float
  cpowl()     //for double
  
  csqrt() :computes the complex square root
  csqrtf()    //for float
  csqrtl()    //for double

Hoạt động trên các số phức tạp

Bổ sung hai số phức (sử dụng cấu trúc)

Trong chương trình này, chúng tôi sẽ thêm hai số phức tạp sẽ được người dùng nhập. Người dùng sẽ nhập các phần thực và tưởng tượng của hai số phức và in kết quả. Ở đây chúng tôi sẽ sử dụng cấu trúc để lưu trữ một số phức.
Here we will use structure to store a complex number.

#include        //standard input output library
struct complex           //structure for coplex number
{
	double real,imag;     //variables holding real and imaginary part of type double
};
int main()
{
	struct complex x,y,c;
	printf("enter the value of x and y for first complex number: ");
	scanf("%lf%lf",&x.real, &x.imag);
	printf("enter the value of x and y for second complex number: ");
	scanf("%lf%lf",&y.real, &y.imag);
	c.real=x.real+y.real;     //addition of real part
	c.imag=x.imag+y.imag;     //addition of imaginary part
	printf("Sum of complex numbers: %.2lf%+.2lfi",c.real,c.imag);
	return 0;
}

OUTPUT:

enter the value of x and y for first complex number: 4 6
enter the value of x and y for second complex number: 2 3
Sum of complex numbers: 6.00+9.00i

Bổ sung hai số phức (sử dụng thư viện)

Trong chương trình này, chúng tôi sẽ thêm hai số phức tạp bằng thư viện. Trong đó, chúng tôi sẽ tính toán phần thực và phần tưởng tượng của hàm bằng các hàm Creal () và cimag () tương ứng. library.
In this we will compute the real part and imaginary part of the function using creal() and cimag() functions respectively.

#include       //standard input output library
#include     //standard complex number library

int main() {

    double complex c1=5.0+3.0*I;     //declaration of a complex number 
    double complex c2=7.0-4.0*I;
    printf("Addition of complex numbers:\n");
    printf("values of complex number c1:c1=%.2lf+%.2lfi\n", creal(c1), cimag(c1));   //computing real and imaginary part of c1
    printf("values of complex number c2:c2=%.2lf+%.2lfi\n", creal(c2), cimag(c2));   //computing real and imaginary part of c2
    double complex sum =c1+c2;
    printf("The sum: c1+c2= %.2lf%+.2lfi\n", creal(sum), cimag(sum));
    return 0;
}

OUTPUT:

Addition of complex numbers:
values of complex number c1: c1=5.00+3.00i
values of complex number c2: c2=7.00+-4.00i
The sum: c1+c2=12.00-1.00i

Phép trừ hai số phức (sử dụng cấu trúc)

Trong chương trình này, chúng tôi sẽ trừ hai số phức tạp sẽ được người dùng nhập. Người dùng sẽ nhập các phần thực và tưởng tượng của hai số phức và in kết quả. Ở đây chúng tôi sẽ sử dụng cấu trúc để lưu trữ một số phức.
Here we will use structure to store a complex number.

#include        //standard input output library
struct complex           //structure for coplex number
{
	double real,imag;     //variables holing real and imaginary part of type double
};
int main()
{
	struct complex x,y,c;
	printf("enter the value of x and y for first complex number: ");
	scanf("%lf%lf",&x.real, &x.imag);
	printf("enter the value of x and y for second complex number: ");
	scanf("%lf%lf",&y.real, &y.imag);
	c.real=x.real-y.real;     //subtraction of real part
	c.imag=x.imag-y.imag;     //subtraction of imaginary part
	printf("Subtraction of complex numbers: %.2lf%+.2lfi",c.real,c.imag);
	return 0;
}

OUTPUT:

struct complex
{
    int real, imag; 
};
0

Phép trừ hai số phức (sử dụng thư viện)

Trong chương trình này, chúng tôi sẽ trừ hai số phức tạp bằng thư viện. Trong đó, chúng tôi sẽ tính toán phần thực và phần tưởng tượng của hàm bằng các hàm Creal () và cimag () tương ứng. library.
In this we will compute the real part and imaginary part of the function using creal() and cimag() functions respectively.

struct complex
{
    int real, imag; 
};
1

OUTPUT:

struct complex
{
    int real, imag; 
};
2

Phép nhân hai số phức (sử dụng cấu trúc)

Trong chương trình này, chúng tôi sẽ nhân hai số phức tạp sẽ được người dùng nhập. Người dùng sẽ nhập các phần thực và tưởng tượng của hai số phức và in kết quả. Ở đây chúng tôi sẽ sử dụng cấu trúc để lưu trữ một số phức.
Here we will use structure to store a complex number.

struct complex
{
    int real, imag; 
};
3
struct complex
{
    int real, imag; 
};
4

OUTPUT:

struct complex
{
    int real, imag; 
};
5

Phép nhân hai số phức (sử dụng thư viện)

Trong chương trình này, chúng tôi sẽ nhân hai số phức tạp bằng thư viện. Trong đó, chúng tôi sẽ tính toán phần thực và phần tưởng tượng của hàm bằng các hàm Creal () và cimag () tương ứng. library.
In this we will compute the real part and imaginary part of the function using creal() and cimag() functions respectively.

struct complex
{
    int real, imag; 
};
6

OUTPUT:

struct complex
{
    int real, imag; 
};
7

Bây giờ chúng tôi sẽ thực hiện tất cả các hoạt động trong một mã duy nhất bằng cấu trúc.

struct complex
{
    int real, imag; 
};
8

OUTPUT:

struct complex
{
    int real, imag; 
};
9

Bây giờ chúng tôi sẽ thực hiện tất cả các hoạt động bằng thư viện

#include
0

OUTPUT:

#include
1