前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 中的复数

C++ 中的复数

作者头像
鲸落c
发布2022-12-16 15:07:59
7450
发布2022-12-16 15:07:59
举报
文章被收录于专栏:鲸落学习笔记鲸落学习笔记

复杂库实现复杂类以包含笛卡尔形式的复数以及多个函数和重载以对其进行操作。

  • real()  – 它返回复数的实数部分。
  • imag()  – 它返回复数的虚部。
代码语言:javascript
复制
// 演示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;
}

输出:

代码语言:javascript
复制
Real part: 10
Imaginary part: 2
  • abs()  – 它返回复数的绝对值。
  • arg()  – 它返回复数的参数。
代码语言:javascript
复制
//说明 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;
}

输出:

代码语言:javascript
复制
The absolute value of (3,4) is: 5
The argument of (3,4) is: 0.927295

polar()  – 它根据幅度和相位角构造一个复数。

实数=幅度余弦(相位角)虚数=幅度正弦(相位角)

代码语言:javascript
复制
// 演示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;
}

输出:

代码语言:javascript
复制
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 的绝对值(也称为范数)定义为:

image.png
image.png
代码语言:javascript
复制
// 说明 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;
}

输出:

代码语言:javascript
复制
The norm of (3,4) is 25.

conj()  – 它返回复数 x 的共轭。复数(实数,imag)的共轭是(实数,-imag)。

代码语言:javascript
复制
// 说明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;
}

输出:

代码语言:javascript
复制
The conjugate of (10,2) is (10,-2)

proj()  – 它返回 z(复数)在黎曼球面上的投影。z 的投影是 z,但复无穷大除外,它们映射到复数值,实数分量为无穷大,虚分量为 0.0 或 -0.0(如果支持),具体取决于 z 虚部的符号。

代码语言:javascript
复制
// 说明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;
}

输出:

代码语言:javascript
复制
proj(1,2) = (1,2)
proj(inf,-1) = (inf,-0)
proj(0,-inf) = (inf,-0)

sqrt()  – 使用主分支返回 x 的平方根,其切割沿负实轴。

代码语言:javascript
复制
// 说明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;
}

输出:

代码语言:javascript
复制
Square root of -4 is (0,2)
Square root of (-4,-0), the other side of the cut, is (0,-2)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-12-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档