⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cx.h

📁 VC环境下的实用的复数库函数,编译后可作为库函数使用.
💻 H
📖 第 1 页 / 共 2 页
字号:
//$ cx.h                     complex numbers definition file

// "WHAT THE STANDARD SHOULD HAVE BEEN LIKE"

#ifndef CX_LIB
#define CX_LIB

#include "include.h"
#include "myexcept.h"

#ifdef use_namespace
namespace RBD_COMPLEX { using namespace RBD_COMMON; }
namespace RBD_LIBRARIES { using namespace RBD_COMPLEX; }
namespace RBD_COMPLEX {
using RBD_COMMON::Real;
using RBD_COMMON::long_Real;
#endif



class ConvertFromReal;      // used for stopping conversions by a constructor
class ImaginaryUnit;        // i or j in complex algebra
class CX;                   // complex class
class Imag;                 // pure imaginary numbers
class Polar;                // imaginary in polar coordinates
class Quadrant;             // mod 4 arithmetic

extern Real pi, pi_times_2, pi_over_2, pi_over_4;

Real ipow(Real x, int n);   // if pow with integer n isn't available



class ConvertFromReal
{
   Real CFR;
public:
   ConvertFromReal(Real cfr) : CFR(cfr) {}
   Real Value() const { return CFR; }
};

class ImaginaryUnit
{
public:
   Real operator*(ImaginaryUnit) { return -1.0; }
   Real operator/(ImaginaryUnit) { return 1.0; }
   ImaginaryUnit operator+();
   Imag operator-();
   bool operator==(ImaginaryUnit) { return true; }
   bool operator!=(ImaginaryUnit) { return false; }
   friend bool operator==(Real, ImaginaryUnit) { return false; }
   friend bool operator!=(Real, ImaginaryUnit) { return true; }
   friend bool operator==(ImaginaryUnit, Real) { return false; }
   friend bool operator!=(ImaginaryUnit, Real) { return true; }
};

extern ImaginaryUnit _I_;         // what is usually written as i or j


class CX
{
   Real X;
   Real Y;
public:
   CX(Real x1, Real y1) : X(x1), Y(y1) {}
   CX(Real x1) : X(x1), Y(0) {}
   CX() {}
   CX(const CX& z) : X(z.X), Y(z.Y) {}
   CX(ImaginaryUnit) : X(0.0), Y(1.0) {}
   CX(Imag y);
   CX(const Polar& p);

   void operator=(const CX& z1) { X = z1.X; Y = z1.Y; }
   void operator=(Real x1) { X = x1; Y = 0.0; }
   void operator=(ImaginaryUnit) { X = 0; Y = 1; }
   void operator=(Imag y);
   void operator=(const Polar& p);

   Real real() const { return X; }
   Real imag() const { return Y; }
   Real& real() { return X; }
   Real& imag() { return Y; }
   Imag imag_as_imag() const;
   CX conj() const { return CX(X,-Y); }
   CX operator+() const { return *this; }
   CX operator-() const { return CX(-X,-Y); }
   Real cabs() const;
   Real arg() const;
   Real sum_square() const { return X * X + Y * Y; }


   friend CX operator*(const CX& z1, const CX& z2);
   friend CX operator/(const CX& z1, const CX& z2);
   friend CX operator+(const CX& z1, const CX& z2);
   friend CX operator-(const CX& z1, const CX& z2);

   friend CX operator*(const CX& z1, Real x2);
   friend CX operator/(const CX& z1, Real x2);
   friend CX operator+(const CX& z1, Real x2);
   friend CX operator-(const CX& z1, Real x2);

   friend CX operator*(Real x1, const CX& z2);
   friend CX operator/(Real x1, const CX& z2);
   friend CX operator+(Real x1, const CX& z2);
   friend CX operator-(Real x1, const CX& z2);

   friend CX operator*(const CX& z1, ImaginaryUnit);
   friend CX operator/(const CX& z1, ImaginaryUnit);
   friend CX operator+(const CX& z1, ImaginaryUnit);
   friend CX operator-(const CX& z1, ImaginaryUnit);

   friend CX operator*(ImaginaryUnit, const CX& z2);
   friend CX operator/(ImaginaryUnit, const CX& z2);
   friend CX operator+(ImaginaryUnit, const CX& z2);
   friend CX operator-(ImaginaryUnit, const CX& z2);

   friend CX operator*(const CX& z1, Imag y2);
   friend CX operator/(const CX& z1, Imag y2);
   friend CX operator+(const CX& z1, Imag y2);
   friend CX operator-(const CX& z1, Imag y2);

   friend CX operator*(Imag y1, const CX& z2);
   friend CX operator/(Imag y1, const CX& z2);
   friend CX operator+(Imag y1, const CX& z2);
   friend CX operator-(Imag y1, const CX& z2);

   friend bool operator==(const CX& z1, const CX& z2);
   friend bool operator==(Real r1, const CX& z2);
   friend bool operator==(Imag y1, const CX& z2);
   friend bool operator==(ImaginaryUnit, const CX& z2);
   friend bool operator==(const CX& z1, Real r2);
   friend bool operator==(const CX& z1, Imag y2);
   friend bool operator==(const CX& z1, ImaginaryUnit);


   void operator*=(const CX& z1)
      { Real t = X; X = X*z1.X-Y*z1.Y; Y = t*z1.Y+Y*z1.X; }
   void operator/=(const CX& z1) { *this = *this/z1; }
   void operator+=(const CX& z1) { X += z1.X; Y += z1.Y; }
   void operator-=(const CX& z1) { X -= z1.X; Y -= z1.Y; }

   void operator*=(Real x1) { X *= x1; Y *= x1; }
   void operator/=(Real x1) { X /= x1; Y /= x1; }
   void operator+=(Real x1) { X += x1; }
   void operator-=(Real x1) { X -= x1; }

   void operator*=(ImaginaryUnit) { Real t = X; X = -Y; Y = t; }
   void operator/=(ImaginaryUnit) { Real t = X; X = Y; Y = -t; }
   void operator+=(ImaginaryUnit) { Y += 1; }
   void operator-=(ImaginaryUnit) { Y -= 1; }

   void operator*=(Imag y);
   void operator/=(Imag y);
   void operator+=(Imag y);
   void operator-=(Imag y);

   void operator*=(const Polar& p1);
   void operator/=(const Polar& p1);
   void operator+=(const Polar& p1);
   void operator-=(const Polar& p1);


   friend CX exp(const CX&);
   friend CX log(const CX&);
   friend Polar polar_exp(const CX&);

   friend Real norm1(const CX& z) { return ::fabs(z.X) + ::fabs(z.Y); }

   friend CX sqrt(const CX&);

   friend CX square(const CX& z)
      { return CX(z.X * z.X - z.Y * z.Y, 2 * z.X * z.Y); }

   friend CX pow(const CX& z1, int n2);
   friend CX pow(const CX& z1, Real r2);
   friend CX pow(const CX& z1, Imag y2);
   friend CX pow(const CX& z1, const CX& z2);
   friend CX pow(Real r1, const CX& z2);
   friend CX pow(Imag y1, const CX& z2);
   friend Polar pow(const Polar& p1, const CX& z2);

   friend CX sin(const CX&);
   friend CX cos(const CX&);
   friend CX tan(const CX&);
   friend CX sinh(const CX&);
   friend CX cosh(const CX&);
   friend CX tanh(const CX&);

   friend class Imag;
   friend class Polar;

};

class Imag
{
   Real Y;
//   Imag(Real z) : Y(z) {}
public:
   Imag(ConvertFromReal x) : Y(x.Value()) {}
   Imag(const Imag& z) : Y(z.Y) {}
   Imag() {}
   Imag(ImaginaryUnit) : Y(1.0) {}

   void operator=(const Imag& y1) { Y = y1.Y; }
   void operator=(ImaginaryUnit) { Y = 1.0; }

   Real real() const { return 0; }
   Real imag() const { return Y; }
   Real& imag() { return Y; }
   Imag imag_as_imag() const { return *this; }
   Imag conj() const { return Imag(-Y); }
   Imag operator+() const { return Imag(Y); }
   Imag operator-() const { return Imag(-Y); }
   Real cabs() const { return ::fabs(Y); }
   Real arg() const;
   Real sum_square() const { return Y * Y; }

   friend Imag operator*(Real x1, ImaginaryUnit);
   friend Imag operator/(Real x1, ImaginaryUnit);
   friend Imag operator*(ImaginaryUnit, Real x2);
   friend Imag operator/(ImaginaryUnit, Real x2);
   friend Imag operator+(ImaginaryUnit, ImaginaryUnit);

   friend Real operator*(Imag y1, Imag y2);
   friend Real operator/(Imag y1, Imag y2);
   friend Imag operator+(Imag y1, Imag y2);
   friend Imag operator-(Imag y1, Imag y2);

   friend Imag operator*(Imag y1, Real x2);
   friend Imag operator/(Imag y1, Real x2);
   friend CX operator+(Imag y1, Real x2);
   friend CX operator-(Imag y1, Real x2);

   friend Imag operator*(Real x1, Imag y2);
   friend Imag operator/(Real x1, Imag y2);
   friend CX operator+(Real x1, Imag y2);
   friend CX operator-(Real x1, Imag y2);

   friend CX operator*(const CX& z1, Imag y2);
   friend CX operator/(const CX& z1, Imag y2);
   friend CX operator+(const CX& z1, Imag y2);
   friend CX operator-(const CX& z1, Imag y2);

   friend CX operator*(Imag y1, const CX& z2);
   friend CX operator/(Imag y1, const CX& z2);
   friend CX operator+(Imag y1, const CX& z2);
   friend CX operator-(Imag y1, const CX& z2);

   friend Real operator*(Imag y1, ImaginaryUnit);
   friend Real operator/(Imag y1, ImaginaryUnit);
   friend Imag operator+(Imag y1, ImaginaryUnit);
   friend Imag operator-(Imag y1, ImaginaryUnit);

   friend Real operator*(ImaginaryUnit, Imag y2);
   friend Real operator/(ImaginaryUnit, Imag y2);
   friend Imag operator+(ImaginaryUnit, Imag y2);
   friend Imag operator-(ImaginaryUnit, Imag y2);

   friend Polar operator*(const Polar& p1, Imag y2);
   friend Polar operator/(const Polar& p1, Imag y2);
   friend Polar operator*(Imag y1, const Polar& p2);
   friend Polar operator/(Imag y1, const Polar& p2);

   friend bool operator==(Imag y1, Imag y2);
   friend bool operator==(Real r1, Imag y2);
   friend bool operator==(const CX& z1, Imag y2);
   friend bool operator==(const Polar& p1, Imag y2);
   friend bool operator==(ImaginaryUnit, Imag y2);
   friend bool operator==(Imag y1, const CX& z2);
   friend bool operator==(Imag y1, const Polar& p2);
   friend bool operator==(Imag y1, Real r2);
   friend bool operator==(Imag y1, ImaginaryUnit);



   void operator*=(Real x1) { Y *= x1; }
   void operator/=(Real x1) { Y /= x1; }
   void operator+=(Imag z) { Y += z.Y; }
   void operator-=(Imag z) { Y -= z.Y; }
   void operator+=(ImaginaryUnit) { Y += 1; }
   void operator-=(ImaginaryUnit) { Y -= 1; }

   friend CX exp(Imag);
   friend CX log(Imag);
   friend Polar polar_exp(Imag z);

   friend CX sqrt(Imag y);

   friend Real square(const Imag& y) { return - y.Y * y.Y; }

   friend CX pow(Imag y1, int n2);
   friend CX pow(Imag y1, Real r2);
   friend CX pow(Imag y1, Imag y2);
   friend CX pow(Real r1, Imag y2);


   friend Imag sin(Imag y) { return Imag(::sinh(y.Y)); }
   friend Real cos(Imag y) { return ::cosh(y.Y); }
   friend Imag tan(Imag y) { return Imag(::tanh(y.Y)); }
   friend Imag sinh(Imag y) { return Imag(::sin(y.Y)); }
   friend Real cosh(Imag y) { return ::cos(y.Y); }
   friend Imag tanh(Imag y) { return Imag(::tan(y.Y)); }

   friend class CX;
   friend class Polar;

};

inline ImaginaryUnit ImaginaryUnit::operator+() { return _I_; }
inline Imag ImaginaryUnit::operator-() { return Imag(-1); }

inline Real real(const CX& z) { return z.real(); }
inline Real imag(const CX& z) { return z.imag(); }
inline Imag imag_as_imag(const CX& z) { return z.imag_as_imag(); }
inline CX conj(const CX& z) { return z.conj(); }
inline Real cabs(const CX& z) { return z.cabs(); }
inline Real fabs(const CX& z) { return z.cabs(); }
inline Real arg(const CX& z) { return z.arg(); }
inline Real sum_square(const CX& z) { return z.sum_square(); }

inline Real real(Imag y) { return y.real(); }
inline Real imag(Imag y) { return y.imag(); }
inline Imag imag_as_imag(const Imag y) { return y; }
inline Imag conj(Imag y) { return y.conj(); }
inline Real cabs(Imag y) { return y.cabs(); }
inline Real fabs(Imag y) { return y.cabs(); }
inline Real arg(Imag y) { return y.arg(); }
inline Real sum_square(Imag y) { return y.sum_square(); }


// Real OP CX

inline CX operator*(Real x1, const CX& z2) { return CX(x1*z2.X, x1*z2.Y); }
//     CX operator/(Real x1, const CX& z2);
inline CX operator+(Real x1, const CX& z2) { return CX(x1+z2.X, z2.Y); }
inline CX operator-(Real x1, const CX& z2) { return CX(x1-z2.X, -z2.Y); }

// Real OP Imag

inline Imag operator*(Real x1, Imag y2) { return Imag(x1*y2.Y); }
inline Imag operator/(Real x1, Imag y2) { return Imag(-x1/y2.Y); }
inline CX operator+(Real x1, Imag y2) { return CX(x1, y2.Y); }
inline CX operator-(Real x1, Imag y2) { return CX(x1, -y2.Y); }

// Real OP ImaginaryUnit

inline Imag operator*(Real x1, const ImaginaryUnit) { return Imag(x1); }
inline Imag operator/(Real x1, const ImaginaryUnit) { return Imag(-x1); }
inline CX operator+(Real x1, const ImaginaryUnit) { return CX(x1, 1); }
inline CX operator-(Real x1, const ImaginaryUnit) { return CX(x1, -1); }

// CX OP Real

inline CX operator*(const CX& z1, Real x2) { return CX(z1.X*x2, z1.Y*x2); }
inline CX operator/(const CX& z1, Real x2) { return CX(z1.X/x2, z1.Y/x2); }
inline CX operator+(const CX& z1, Real x2) { return CX(z1.X+x2, z1.Y); }
inline CX operator-(const CX& z1, Real x2) { return CX(z1.X-x2, z1.Y); }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -