复杂库实现复杂类以包含笛卡尔形式的复数以及多个函数和重载以对其进行操作。
// 演示real()和imag()函数使用的程序
#include <iostream>
// 对于std::complex、std::real、std::imag
#include <complex>
using namespace std;
// 驱动器功能
int main()
{
// 定义复数:(10+2i)
std::complex<double> mycomplex(10.0, 2.0);
// 使用实函数打印实零件
cout << "Real part: " << real(mycomplex) << endl;
cout << "Imaginary part: " << imag(mycomplex) << endl;
return 0;
}
输出:
Real part: 10
Imaginary part: 2
//说明 arg() 和 abs() 用法的程序
#include <iostream>
// 对于 std::complex, std::abs, std::atg
#include <complex>
using namespace std;
// 驱动器功能
int main ()
{
// 驱动程序函数定义复数:(3.0+4.0i)
std::complex<double> mycomplex (3.0, 4.0);
// 打印复数的绝对值
cout << "The absolute value of " << mycomplex << " is: ";
cout << abs(mycomplex) << endl;
// 打印复数的参数
cout << "The argument of " << mycomplex << " is: ";
cout << arg(mycomplex) << endl;
return 0;
}
输出:
The absolute value of (3,4) is: 5
The argument of (3,4) is: 0.927295
polar() – 它根据幅度和相位角构造一个复数。
实数=幅度余弦(相位角)虚数=幅度正弦(相位角)
// 演示polar()用法的程序
#include <iostream>
// std::complex, std::polar
#include <complex>
using namespace std;
// 驱动器功能
int main ()
{
cout << "The complex whose magnitude is " << 2.0;
cout << " and phase angle is " << 0.5;
// polar()的使用
cout << " is " << polar (2.0, 0.5) << endl;
return 0;
}
输出:
The complex whose magnitude is 2 and phase angle is 0.5 is (1.75517,0.958851)
norm() – 它用于查找复数的范数(绝对值)。如果 z = x + iy 是实部 x 和虚部 y 的复数,则 z 的复共轭定义为 z'(z bar) = x – iy,z 的绝对值(也称为范数)定义为:
// 说明 norm() 用法的示例
#include <iostream>
// 对于 std::complex, std::norm
#include <complex>
using namespace std;
// 驱动器功能
int main ()
{
// 初始化复合体:(3.0+4.0i)
std::complex<double> mycomplex (3.0, 4.0);
// norm() 的使用
cout << "The norm of " << mycomplex << " is "
<< norm(mycomplex) <<endl;
return 0;
}
输出:
The norm of (3,4) is 25.
conj() – 它返回复数 x 的共轭。复数(实数,imag)的共轭是(实数,-imag)。
// 说明cong()的用法
#include <iostream>
using namespace std;
// std::complex, std::conj
#include <complex>
// 驱动程序
int main ()
{
std::complex<double> mycomplex (10.0,2.0);
cout << "The conjugate of " << mycomplex << " is: ";
// 使用cong()
cout << conj(mycomplex) << endl;
return 0;
}
输出:
The conjugate of (10,2) is (10,-2)
proj() – 它返回 z(复数)在黎曼球面上的投影。z 的投影是 z,但复无穷大除外,它们映射到复数值,实数分量为无穷大,虚分量为 0.0 或 -0.0(如果支持),具体取决于 z 虚部的符号。
// 说明proj()的用法
#include <iostream>
using namespace std;
// For std::complex, std::proj
#include <complex>
// 驱动程序
int main()
{
std::complex<double> c1(1, 2);
cout << "proj" << c1 << " = " << proj(c1) << endl;
std::complex<double> c2(INFINITY, -1);
cout << "proj" << c2 << " = " << proj(c2) << endl;
std::complex<double> c3(0, -INFINITY);
cout << "proj" << c3 << " = " << proj(c3) << endl;
}
输出:
proj(1,2) = (1,2)
proj(inf,-1) = (inf,-0)
proj(0,-inf) = (inf,-0)
sqrt() – 使用主分支返回 x 的平方根,其切割沿负实轴。
// 说明sqrt() 的用法
#include <iostream>
using namespace std;
// For std::ccomplex, stdc::sqrt
#include <complex>
// 驱动程序
int main()
{
// sqrt() 的使用
cout << "Square root of -4 is "
<< sqrt(std::complex<double>(-4, 0)) << endl
<< "Square root of (-4,-0), the other side of the cut, is "
<< sqrt(std::complex<double>(-4, -0.0)) << endl;
}
输出:
Square root of -4 is (0,2)
Square root of (-4,-0), the other side of the cut, is (0,-2)