STL算法库-数字运算(一)

一、定义
包括区间的内容累积、内部乘积、小计、计算相邻对象差等函数
二、复数运算
2.1 头文件
#include "complex"

2.2 复数成员函数
2.2.1 构造函数:
complex(const T& re=0,complex T& im=0);
complex(const complex& x);
2.2.2 实部和虚部函数
T real()const;
T image() const;

示例代码如下:

#include "stdafx.h"
#include "complex"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 complex<double> c1(3.0,4.0);//实?部?,?虚?部?
 complex<float> c2(polar(5.0,1.0));//模?,?角?度?
 cout<<"c1:"<<c1<<endl;
 cout<<"c2:"<<c2<<endl;
 cout<<"c1:magnitude: "<<abs(c1)<<"(square magnitude:"<<norm(c1)<<")"<<"phase angle:"<<arg(c1)<<endl;
 cout<<"c2:magnitude: "<<abs(c2)<<"(square magnitude:"<<norm(c2)<<")"<<"phase angle:"<<arg(c2)<<endl;
 cout<<"c1 conjugated:"<<conj(c1)<<endl;//相?反2数y
 cout<<"c2 conjugated:"<<conj(c2)<<endl;
 cout<<"c1*2.0:"<<(c1*2.0)<<endl;
 cout<<"c1+c2:"<<(c1+complex<double>(c2.real(),c2.imag()))<<endl;
 return 0;
}
2.3 复数运算符
支持+、-等各种数学运算符

2.4 复数的运算函数
2.4.1 绝对值函数abs
T abs(const complex<T>& X);
2.4.2 绝对值的平方norm
T norm(const complex<T>& X);
2.4.3 复数的相位
T arg(const complex<T>& X);
2.4.4 输入与输出
>>和<<
2.4.5共轭函数conj
complex<T> conj(const complex& X);
2.4.6极坐标函数polar
complex<T> polar(const T& rho,const T& theta=0);
示例代码如下:

#include "stdafx.h"
#include "complex"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 complex<double> c1(3.0,4.0);//实?部?,?虚?部?
 complex<float> c2(polar(5.0,1.0));//模?,?角?度?
 float a1=abs(c1);
 float a2=abs(c2);
 cout.precision(3);
 cout<<"abs(c1) "<<a1<<" ;"<<" abs(c2): "<<a2<<endl;
 a1=norm(c1);
 a2=norm(c2);
 cout.precision(3);
 cout<<"norm(c1) "<<a1<<" ;"<<" norm(c2): "<<a2<<endl;
 a1=arg(c1);
 a2=arg(c2);
 cout<<"arg(c1) "<<(a1*180/3.1415926)<<" ;"<<" arg(c2): "<<(a2*180/3.1415926)<<endl;
 complex<float> cl1;
 complex<float> cl2;
 cl1=conj(c1);
 cl2=conj(c2);
 cout<<"conj(c1): "<<cl1<<endl;
 cout<<"conj(c2): "<<cl2<<endl;
 return 0;
}

2.5 复数的超越函数(三角函数和指数函数等)
2.5.1 三角函数
complex<T> sin(const complex<T>& x);
complex<T> cos(const complex<T>& x);
complex<T> tan(const complex<T>& x);
complex<T> sinh(const complex<T>& x);
complex<T> cosh(const complex<T>& x);
complex<T> tanh(const complex<T>& x);
2.5.2 幂运算(指数运算)
complex<T> pow(const complex<T>& x,int y);
complex<T> pow(const complex<T>& x,const T& y);
complex<T> pow(const complex<T>& x,const complex<T>& y);
complex<T> pow(const T& x,const complex<T>& y);
2.5.3 以e为底的幂函数exp()
complex<T> exp(const complex<T>& x);
2.5.4 平方根函数
complex<T> sqrt(const complex<T>& x);
2.5.5 对数函数
自然对数函数:complex<T> log(const complex<T>& x);
以10为底的对数函数:complex<T> log10(const complex<T>& x);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值