作死去学了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>分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26661.html