标签:模板
作死去学了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); } }; }
标签:模板
原文地址:http://blog.csdn.net/popoqqq/article/details/42144601