简体   繁体   English

C ++复数,什么是正确的格式?

[英]C++ complex numbers, what is the right format?

I want to use C++ with complex numbers. 我想使用复杂数字的C ++。 Therefore I included #include <complex> . 因此我加入了#include <complex> Now my question is: How do I declare a variable?(so what is the format called for let's say: 1 + i ?) 现在我的问题是:我如何声明一个变量?(那么调用的格式是什么,让我们说: 1 + i ?)

Thanks in advance :-) 提前致谢 :-)

// 1 + 2i
std::complex<double> c(1, 2);

The constructor of std::complex has two parameters: std::complex的构造函数有两个参数:

  • The first, wich has the real part of the number. 第一个,这个数字的真实部分。
  • The second, wich has the imaginary part of the number. 第二个,具有数字的虚部。

For example: 例如:

std::complex<float> my_complex(1,1); //1 + 1i 

Also, C++11 introduces user defined literals , wich allows us to implement (Or be implemented by the standard library, as in this C++14 accepted proposal ) a literal for easy-to-use complex numbers: 此外,C ++ 11引入了用户定义的文字 ,它允许我们实现(或由标准库实现,如在此C ++ 14接受的提议中 )一个易于使用的复数的文字:

constexpr std::complex<float> operator"" i(float d)
{
    return std::complex<float>{0.0L,static_cast<float>( d )};
}

You could use this as follows: 你可以使用如下:

auto my_complex = 1i; // 0 + 1i

Try this: 尝试这个:

#include <complex>
#include <iostream>
using namespace std;
int main()
{
    complex<double> a = {1,2};
    complex<double> b(3,4);

    cout << a + b << "\n";
}

You define a variable by specifying a template parameter and specifying a name for the variable, about like with most other templates: 您可以通过指定模板参数并指定变量的名称来定义变量,与大多数其他模板一样:

std::complex<double> x(1, 1);

The first parameter to the ctor is the real part, the second the imaginary part. ctor的第一个参数是实部,第二个是虚部。

Starting with C++ 14, a user-defined literal operator has been added, so you can initialize a complex variable with a somewhat more natural notation: 从C ++ 14开始,添加了一个用户定义的文字运算符,因此您可以使用更自然的表示法初始化复杂变量:

using namespace std::literals;

std::complex<double> c = 1.2 + 3.4i;

In this case, (obviously enough) the 1.2 is the real part and the 3.4 is the imaginary part. 在这种情况下,(显然足够) 1.2是实部, 3.4是虚部。

Here is an example of how to use . 这是一个如何使用的例子。 It compiles and runs under QT 它在QT下编译和运行

#include <QCoreApplication>
#include<complex>
#include<iostream>

using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::complex<double> x=3.0-3.0i;
std::complex<double> y=2.0+4.0i;

cout.precision(3);
cout<<"x="<<x<<" y="<<y<<'\n';
cout<<" OR x real="<<real(x)<<" x imagine="<<imag(x)<<"\n\n";

complex<double> sum = x + y;
cout<<"The sum: x + y = "<<sum<<'\n';

complex<double> difference = x - y;
cout<<"The difference: x - y = "<<difference<<'\n';

complex<double> product = x * y;
cout<<"The product: XY = "<<product<<'\n';

complex<double> quotient = x / y;
cout<<"The quotient: x / y = "<<quotient<<'\n';

complex<double> conjugate = conj(x);
cout<<"The conjugate of x = "<<conjugate<<'\n';

complex<double> reciprocal = 1.0/x;
cout<<"The reciprocal of x = "<<reciprocal<<'\n';

complex<double> exponential =exp(x);
cout<<"The exponential  of x = "<<exponential<<'\n';

double magnitude=2.0,phase=45;
cout<<"magintude = "<<magnitude<<" phase = "<< phase<<" degrees\n";
complex<double> pol= std::polar(2.0,(M_PI/180.0)*phase);
cout<<"The polar: x , y = "<<pol<<'\n';

return a.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM