C++ 中的复数

本文涉及的产品
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介: 复杂库实现复杂类以包含笛卡尔形式的复数以及多个函数和重载以对其进行操作。

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第7天,点击查看活动详情


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


  • real()  – 它返回复数的实数部分。

  • imag()  – 它返回复数的虚部。
// 演示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
复制代码

  • abs()  – 它返回复数的绝对值。

  • arg()  – 它返回复数的参数。
//说明 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 的绝对值(也称为范数)定义为:

40.png



// 说明 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)



目录
相关文章
|
Kubernetes Linux Windows
第二章 Linux和windows部署helm 客户端
第二章 Linux和windows部署helm 客户端
264 0
|
Linux
centos7 如何处理 libQt5Widgets.so.5 问题
今天一同事发过来一个报错信息,提示如下:error while loading shared libraries: libQt5Widgets.so.5: cannot open shared object file: No such file or directory查了几个博客分享...
5704 0
|
12月前
|
安全 前端开发
CMS建站系统如何选择?
现在有很多的人在建设网站的时候采用的都是cms系统,我国比较经常使用的是PageAdmin、织梦CMS、帝国系统等等不同的CMS系统使用的方向也是有所差异的,今天给大家分析一下三大CMS的优缺点。
195 4
|
7月前
|
机器学习/深度学习 人工智能 智能设计
破界·共生:生成式人工智能(GAI)认证重构普通人的AI进化图谱
本文探讨人工智能未来十大趋势及其对普通人的影响,涵盖神经形态计算、多模态认知融合等前沿领域。同时,文章重点介绍生成式人工智能(GAI)认证体系,帮助普通人从认知重构、能力进化到职业转型和伦理自觉全面学习AI技术,成为人机共生时代的智能伙伴。GAI认证作为加速器,提供系统培训与专业交流平台,助力个体在AI浪潮中把握机遇,共创未来。
|
11月前
|
机器学习/深度学习 人工智能 缓存
最佳实践!使用 GraphRAG + GLM-4 对《红楼梦》全文构建中文增强检索
特别介绍`graphrag-practice-chinese`项目,这是一个针对中文优化的GraphRAG应用实例,通过改进文本切分策略、使用中文提示词及选择更适合中文的模型等手段,显著提升了处理中文内容的能力。项目不仅包括详细的搭建指南,还提供了《红楼梦》全文的索引构建与查询测试示例,非常适合个人学习和研究。
1984 1
|
11月前
|
机器学习/深度学习 人工智能 算法
从数据增强的隐藏作用出发,揭示视觉强化学习可塑性损失的独特机制
【10月更文挑战第22天】视觉强化学习(VRL)通过智能体与环境的交互学习最优策略,但可塑性损失是其关键挑战。近期一篇论文《Revisiting Plasticity in Visual Reinforcement Learning: Data, Modules and Training Stages》通过实证研究,揭示了数据增强、评论家可塑性损失及早期干预在维持智能体可塑性方面的作用,并提出了一种动态调整重放率的方法,为解决高重放率困境提供了新思路。
183 2
|
机器学习/深度学习 数据采集 算法
Scikit-Learn基础教程
Scikit-Learn基础教程
443 2
|
存储 缓存 算法
Python缓存神器cachetools:提高程序性能的利器,一文详解其缓存算法
Python缓存神器cachetools:提高程序性能的利器,一文详解其缓存算法
Python缓存神器cachetools:提高程序性能的利器,一文详解其缓存算法