using namespace std;
class Complex
{
private:
float re, im; //default is double
public:
Complex(float re = 0, float im = 0): re(re), im(im){};
Complex& operator = (const Complex&);
Complex& operator += (const Complex&);
Complex& operator -= (const Complex&);
Complex& operator *= (const Complex&);
Complex& operator /= (const Complex&);
Complex& operator = (float);
Complex& operator += (float);
Complex& operator -= (float);
Complex& operator *= (float);
Complex& operator /= (float);
float real() const { return re; }
float imag() const { return im; }
friend float real(const Complex&);
friend float imag(const Complex&);
friend float abs(const Complex&);
friend float arg(const Complex&);
friend Complex polar(float, float);
friend float norm(const Complex&);
friend Complex conj(const Complex& x);
friend Complex cos(const Complex& x);
friend Complex cosh(const Complex& x);
friend Complex exp(const Complex& x);
// friend Complex log(const Complex& x);
friend Complex sin(const Complex& x);
friend Complex sinh(const Complex& x);
// friend Complex pow(const Complex&, const Complex&);
// friend Complex pow(const Complex&, double);
// friend Complex pow(double, const Complex&);
friend Complex pow(const Complex&, int);
// friend Complex sqrt(const Complex& x);
friend Complex operator + (const Complex&);
friend Complex operator - (const Complex&);
friend Complex operator + (const Complex&, const Complex&);
friend Complex operator + (const Complex&, float);
friend Complex operator + (float, const Complex&);
friend Complex operator - (const Complex&, const Complex&);
friend Complex operator - (const Complex&, float);
friend Complex operator - (float, const Complex&);
friend Complex operator * (const Complex&, const Complex&);
friend Complex operator * (const Complex&, float);
friend Complex operator * (float, const Complex&);
friend Complex operator / (const Complex&, const Complex&);
friend Complex operator / (const Complex&, float);
friend Complex operator / (float, const Complex&);
friend int operator == (const Complex&, const Complex&);
friend int operator == (const Complex&, float);
friend int operator == (float, const Complex&);
friend int operator != (const Complex&, const Complex&);
friend int operator != (const Complex&, float);
friend int operator != (float, const Complex&);
// friend istream& operator >> (istream&, Complex&);
friend ostream& operator << (ostream &os, const Complex &val);
};
inline Complex& Complex::operator = (const Complex& r)
{
re = r.re;
im = r.im;
return *this;
}
inline Complex& Complex::operator += (const Complex& r)
{
re += r.re;
im += r.im;
return *this;
}
inline Complex& Complex::operator -= (const Complex& r)
{
re -= r.re;
im -= r.im;
return *this;
}
inline Complex& Complex::operator *= (const Complex& r)
{
float f = re * r.re - im * r.im;
im = re * r.im + im * r.re;
re = f;
return *this;
}
inline Complex& Complex::operator /= (const Complex& y)
{
float ar = fabs(y.re);
float ai = fabs(y.im);
float nr, ni;
float t, d;
if (ar <= ai)
{
t = y.re / y.im;
d = y.im * (1 + t * t);
nr = (re * t + im) / d;
ni = (im * t - re) / d;
}
else
{
t = y.im / y.re;
d = y.re * (1 + t * t);
nr = (re + im * t) / d;
ni = (im - re * t) / d;
}
re = nr;
im = ni;
return *this;
}
inline Complex& Complex::operator = (float x)
{
re = x;
im = 0.0;
return *this;
}
inline Complex& Complex::operator += (float x)
{
re += x;
return *this;
}
inline Complex& Complex::operator -= (float x)
{
re -= x;
return *this;
}
inline Complex& Complex::operator *= (float x)
{
re *= x;
im *= x;
return *this;
}
inline Complex& Complex::operator /= (float x)
{
re /= x;
im /= x;
return *this;
}
inline float real(const Complex& x)
{
return x.real();
}
inline float imag(const Complex& x)
{
return x.imag();
}
inline float abs(const Complex& x)
{
return _hypotf(real(x), imag(x));
}
inline float arg(const Complex& x)
{
return atan2(imag(x), real(x)); // atan2 returns a value in the range -pi to pi radians
}
inline Complex polar(float r, float t)
{
return Complex(r * cos(t), r * sin(t));
}
inline float norm(const Complex& x)
{
return real(x) * real(x) + imag(x) * imag(x); // returns magnitude square
}
inline Complex conj(const Complex& x)
{
return Complex(real(x), -imag(x));
}
inline Complex cos(const Complex& x)
{
return Complex(cos(real(x)) * cosh(imag(x)), -sin(real(x)) * sinh(imag(x)));
}
inline Complex cosh(const Complex& x)
{
return Complex(cosh(real(x)) * cos(imag(x)), sinh(real(x)) * sin(imag(x)));
}
inline Complex exp(const Complex& x)
{
return polar(float(exp(real(x))), imag(x));
}
//inline Complex log(const Complex& x)
//{
// return Complex(log(abs(x)), arg(x));
//}
inline Complex sin(const Complex& x)
{
return Complex(sin(real(x)) * cosh(imag(x)), cos(real(x)) * sinh(imag(x)));
}
inline Complex sinh(const Complex& x)
{
return Complex(sinh(real(x)) * cos(imag(x)), cosh(real(x)) * sin(imag(x)));
}
//inline Complex pow(const Complex& x, const Complex& y)
//{
// double logr = log(abs(x));
// double t = arg(x);
//
// return polar(double(exp(logr * real(y) - imag(y) * t)), double(imag(y) * logr + real(y) * t));
//}
//
//inline Complex pow(const Complex& x, double y)
//{
// return exp(double(y) * log(x));
//}
//
//inline Complex pow(double x, const Complex& y)
//{
// return exp(y * double(log(x)));
//}
inline Complex pow(const Complex& xin, int y)
{
if (y == 0)
return Complex(1.0);
Complex r(1.0);
Complex x(xin);
if (y < 0)
{
y = -y;
x = 1/x;
}
for (;;)
{
if (y & 1)
r *= x;
if (y >>= 1)
x *= x;
else
return r;
}
}
//inline Complex sqrt(const Complex& x)
//{
// double r = abs(x);
// double nr, ni;
//
// if (r == 0.0)
// nr = ni = r;
// else if (real(x) > 0)
// {
// nr = sqrt(0.5 * (r + real(x)));
// ni = imag(x) / nr / 2;
// }
// else
// {
// ni = sqrt(0.5 * (r - real(x)));
//
// if (imag(x) < 0)
// ni = -ni;
//
// nr = imag(x) / ni / 2;
// }
//
// return Complex(nr, ni);
//}
inline Complex operator + (const Complex& x)
{
return x;
}
inline Complex operator - (const Complex& x)
{
return Complex (-real(x), -imag(x));
}
inline Complex operator + (const Complex& x, const Complex& y)
{
return Complex(real(x) + real(y), imag(x) + imag(y));
}
inline Complex operator + (const Complex& x, float y)
{
return Complex(real(x) + y, imag(x));
}
inline Complex operator + (float x, const Complex& y)
{
return Complex(x + real(y), imag(y));
}
inline Complex operator - (const Complex& x, const Complex& y)
{
return Complex(real(x) - real(y), imag(x) - imag(y));
}
inline Complex operator - (const Complex& x, float y)
{
return Complex(real(x) - y, imag(x));
}
inline Complex operator - (float x, const Complex& y)
{
return Complex(x - real(y), -imag(y));
}
inline Complex operator * (const Complex& x, const Complex& y)
{
return Complex(real(x) * real(y) - imag(x) * imag(y), real(x) * imag(y) + imag(x) * real(y));
}
inline Complex operator * (const Complex& x, float y)
{
return Complex(real(x) * y, imag(x) * y);
}
inline Complex operator * (float x, const Complex& y)
{
return Complex(x * real(y), x * imag(y));
}
//inline Complex operator / (const Complex& x, const Complex& y)
//{
// return Complex(real(x) * real(y) + imag(x) * imag(y), -real(x) * imag(y) + imag(x) * real(y)) / norm(y);
//}
inline Complex operator / (const Complex& x, const Complex& y)
{
float ar = fabs(real(y));
float ai = fabs(imag(y));
float nr, ni;
float t, d;
if (ar <= ai)
{
t = real(y) / imag(y);
d = imag(y) * (1 + t * t);
nr = (real(x) * t + imag(x)) / d;
ni = (imag(x) * t - real(x)) / d;
}
else
{
t = imag(y) / real(y);
d = real(y) * (1 + t * t);
nr = (real(x) + imag(x) * t) / d;
ni = (imag(x) - real(x) * t) / d;
}
return Complex(nr, ni);
}
inline Complex operator / (const Complex& x, float y)
{
return Complex(real(x) / y, imag(x) / y);
}
inline Complex operator / (float x, const Complex& y)
{
float d = norm(y);
return Complex((x * real(y)) / d, (-x * imag(y)) / d);
}
inline int operator == (const Complex& x, const Complex& y)
{
return real(x) == real(y) && imag(x) == imag(y);
}
inline int operator == (const Complex& x, float y)
{
return real(x) == y && imag(x) == 0;
}
inline int operator == (float x, const Complex& y)
{
return x == real(y) && imag(y) == 0;
}
inline int operator != (const Complex& x, const Complex& y)
{
return real(x) != real(y) || imag(x) != imag(y);
}
inline int operator != (const Complex& x, float y)
{
return real(x) != y || imag(x) != 0;
}
inline int operator != (float x, const Complex& y)
{
return x != real(y) || imag(y) != 0;
}
//inline istream& operator >> (istream& is, Complex& x)
//{
// double re, im = 0;
// char ch = 0;
//
// if (is.ipfx())
// {
// if (is.peek() == '(')
// is >> ch;
//
// is >> re;
//
// if (ch == '(')
// {
// is >> ch;
//
// if (ch == ',')
// is >> im >> ch;
// }
// }
//
// is.isfx();
//
// if (ch != 0 && ch != ')')
// is.setstate(ios::failbit);
// else if (is.good())
// x = Complex(re, im);
//
// return is;
//}
//
inline ostream& operator << (ostream &os, const Complex &val)
{
cout << val.real();
if (val.imag() < 0.0)
cout << " - " << -val.imag() << "i";
else
cout << " + " << val.imag() << "i";
return os;
}
修改这段代码,要求把complex类转化到一个新的namespace, 以避免于std冲突
最新发布