码迷,mamicode.com
首页 > 其他好文 > 详细

模板:<bits/_Complex.h>

时间:2014-12-25 14:41:50      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:模板

作死去学了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); }
	};
}


模板:<bits/_Complex.h>

标签:模板

原文地址:http://blog.csdn.net/popoqqq/article/details/42144601

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!