模板:<bits/_Complex.h>

作死去学了FFT。。。

系统自带的complex真是慢。。。比手写的慢了整整0.8s。。。于是果断手写了一发Complex的模板。。。。

原型:

template<typename T>class Complex;

使用方法:

using namespace PoPoQQQ_Complex;
	Complex<double> x;cin>>x;
	x+=Complex<double>(1,0);
	x-=Complex<double>(0,1);
	x*=Complex<double>(2,3);
	x/=Complex<double>(2);
	if( x==Complex<double>(3.500000003,2) )//重载==与!=,自带精度判断 
		cout<<x<<endl;
	cout<<Abs(x)<<endl;//模值 
	cout<<Arg(x)<<endl;//极角
	cout<<Norm(x)<<endl;//模值的平方
	cout<<Conj(x)<<endl;//共轭复数
	cout<<Exp(x)<<endl;//自然对数
	cout<<Log(x)<<endl;//自然对数底的对数
	cout<<Pow(x,10)<<endl;//幂
	cout<<Sqrt(x)<<endl;//平方根 

代码:

//writen by PoPoQQQ
//not finished yet
#define _COMPLEX_

#ifndef _MATH_
#include<cmath>
#endif

#ifndef _GLIBCXX_IOSTREAM
#include<iostream>
#endif

#ifndef EPS
#define EPS 1e-7
#endif

namespace PoPoQQQ_Complex{
	using namespace std;
	template<typename T>class Complex{
	public:
		T real,imaginary;
		Complex() {}
		Complex(T _,T __=0.0):real(_),imaginary(__) {}
		Complex& operator += (const Complex<T> &x) { real+=x.real;imaginary+=x.imaginary;return *this; }
		Complex& operator -= (const Complex<T> &x) { real-=x.real;imaginary-=x.imaginary;return *this; }
		Complex& operator *= (const T &x) { real*=x;imaginary*=x;return *this; }
		Complex& operator /= (const T &x) { real/=x;imaginary/=x;return *this; }
		friend Complex operator + (const Complex<T> &x,const Complex<T> &y) { Complex re=x;re+=y;return re; }
		friend Complex operator - (const Complex<T> &x,const Complex<T> &y) { Complex re=x;re-=y;return re; }
		friend Complex operator * (const Complex<T> &x,const T y) { Complex re=x;re*=y;return re; }
		friend Complex operator / (const Complex<T> &x,const T y) { Complex re=x;re/=y;return re; }
		friend Complex operator * (const Complex<T> &x,const Complex<T> &y)
		{ return Complex(x.real*y.real-x.imaginary*y.imaginary,x.real*y.imaginary+x.imaginary*y.real); }
		friend Complex operator / (const Complex<T> &x,const Complex<T> &y)
		{ return x*Complex(y.real,-y.imaginary)/(y.real*y.real+y.imaginary*y.imaginary); }
		Complex& operator *= (const Complex &x) { return *this=(*this)*x; }
		Complex& operator /= (const Complex &x) { return *this=(*this)/x; }
		bool operator == (const Complex &x) { return fabs(real-x.real)<EPS && fabs(imaginary-x.imaginary)<EPS; }
		bool operator != (const Complex &x) { return fabs(real-x.real)>EPS || fabs(imaginary-x.imaginary)>EPS; }
		friend istream& operator >> (istream& _is,Complex &x) { _is>>x.real>>x.imaginary;return _is; }
		friend ostream& operator << (ostream& _os,const Complex &x) { _os<<'('<<x.real<<','<<x.imaginary<<')';return _os; }
		friend T Abs(const Complex<T> &x) { return sqrt(x.real*x.real+x.imaginary*x.imaginary); }
		friend T Arg(const Complex<T> &x) { return atan2(x.imaginary,x.real); }
		friend T Norm(const Complex<T> &x) { return x.real*x.real+x.imaginary*x.imaginary; }
		friend Complex<T> Conj(const Complex<T> &x) { return Complex(x.real,-x.imaginary); }
		friend Complex<T> Exp(const Complex<T> &x) { return exp(x.real)*Complex<T>(cos(x.imaginary),sin(x.imaginary)); }
		friend Complex<T> Log(const Complex<T> &x) { return Complex<T>(log(Norm(x))/2.0,atan2(x.imaginary,x.real)); }
		friend Complex<T> Pow(const Complex<T> &x,const T y) { return Exp(y*Log(x)); }
		friend Complex<T> Sqrt(const Complex<T> &x) { return Pow(x,0.5); }
	};
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值