//: Complex.h
#ifndef _complex_h_ # define _complex_h_
Namespace numeric {
//
#include #define log10 2.30258512496948 //
// Template // Class Complex; /// Template // struct _binary_expr_cc;
//
Template struct _return_type {type;});} DOUBLE VALUE_TYPE
Template struct _return_type <_tp1, long double> {typedef long double value_type;};
Template struct _return_type {type;};
Template <> struct _return_type {typedef float value_type;
//
Template struct constant {type; _tp value_type;
PUBLIC: Const_TP & _M_CNST;
Public: constnt (const_tp & __t): _m_cnst (__ t) {} // constant (const all ": _m_cnst (__ c._m_cnst) {}
PUBLIC: INLINE VALUE_TYPE REAL () const {return_m_cnst;} inline value_type imag () const {return 0;}}
//
Struct _Abs_cc {template static inline type_ARG :: Value_type evaluate (const _arg& __a) {return sqrt (__ a.real () * __ a.Real () __ a.imag () * __ a.imag ()); }};
Struct _arg_cc {template static inline typename _arg :: value_type evaluate (const _arg & __a) {return atan2 (__ a.imag (), __a.Real ());}}
Struct _norm_cc {template static inline type, _arg :: value_type evaluate (const_ARG & __A) {return (__a.real () * __ a.real () __ a.imag () * __ a.imag ());} }
//
struct _conjg_cc {template static inline typename _arg :: value_type real (const _arg & __a) {return __a.real ();} template static inline typename _arg :: value_type imag (const _arg & __a) {return -__ a.imag ();}};
Struct_cos_cc {template static inline type_rg :: value_type real (const _arg & __a) {return cos (__ a.real ()) * Cosh (__ a.imag ());}
Template static inline type_arg :: value_type imag (const _ARG & __A) {return -sin (__ a.real ()) * sinh (__ a.imag ());}};
struct _cosh_cc {template static inline typename _arg :: value_type real (const _arg & __a) {return cosh (__ a.real ()) * cos (__ a.imag ());} template static inline typename _arg :: value_type imag (const _arg & __a) {return sinh (__ a.Real ()) * sin (__ a.imag ());}};
struct _exp_cc {template static inline typename _arg :: value_type real (const _arg & __a) {typedef typename _arg :: value_type value_type; // value_type tmp = exp (__ a.real ()); return exp (__ a.real ()) * COS (__ a.imag ());}
template static inline typename _arg :: value_type imag (const _arg & __a) {typedef typename _arg :: value_type value_type; // value_type tmp = exp (__ a.real ()); return exp (__ a.real ()) * sin (__ a.imag ());}};
struct _log_cc {template static inline typename _arg :: value_type real (const _arg & __a) {typedef typename _arg :: value_type value_type; value_type tmp = __a.real () * __ a.real () __ a.imag () * __ a.imag (); return log (tmp) * 0.5;}
template static inline typename _arg :: value_type imag (const _arg & __a) {return atan2 (__ a.imag (), __a.real ());}}; struct _log10_cc {template static inline typename _arg :: Value_Type Real (const_ARG & __A) {typedef type_ARG :: value_type value_type; value_type tmp = __a.real () * __ a.real () __ a.imag () * __ a.imag (); return log10 (TMP) * 0.5;
Template static inline type_arg :: value_type imag (const _arg & __a) {return arg (__ a) / log10;}}
Struct _sin_cc {template static inline typename _arg :: value_type real (const _arg & __a) {return sin (__ a.real ()) * Cosh (__ a.imag ());} template static inline typename _Arg :: value_type imag (const _arg & __a) {return cos (__ a.real ()) * sinh (__ a.imag ());}};
Struct_sinh_cc {template static inline type, _ARG :: Value_type real (const _arg & __a) {return sinh (__ a.real ()) * cos (__ a.imag ());}
Template static inline typename _arg :: value_type imag (const_ARG & __A) {Return Cosh (__ a.real ()) * sin (__ a.imag ());}};
//
struct _pow_cc {template static inline typename _return_type :: value_type real (const _arg1 & __a, const _arg2 & __b) {// typedef typename // _ return_type < TypeName_Arg1 :: Value_Type, // Typename_arg2 :: value_type //> :: value_type value_type; // value_type t1 = log (__ a.real () * __ a.real () __ a.imag () * __ a.imag ))) * 0.5; // value_type t2 = arg (__); // return exp (t1 * __ b.real () - t2 * __ b.imag ()) * cos (t1 * __ b.imag () t2 * __ b .real ()); return exp (__ b * log (__ a)) real ();.} template static inline typename _return_type :: value_type IMAG (const _arg1 & __a, const _arg2 & __b) {// typed_type :: value_type value_type; // value_type t1 = log (__ a.real () * __ a.Real () __ a.imag () * __ a.imag ()) * 0.5; // value_type t2 = arg (__ a); // return exp (T1 * __ b.real () - t2 * __ b.imag ()) * sin (t1 * __ b.imag () t2 * __ b.real ()); return exp (__ b * log (__ a)). IMAG (); }};
//
Struct _unary_add_cc {template static inline type_Arg :: value_type real (const _ARG & __A) {return __a.real ();}
Template static inline type_ARG :: value_type imag (const _arg & __a) {return__a.imag ();}}
//
struct _unary_sub_cc {template static inline typename _arg :: value_type real (const _arg & __a) {return -__ a.real ();} template static inline typename _arg :: value_type imag (const _arg & __a) { Return -__ a.imag ();}};
//
struct _binary_add_cc {template static inline typename _return_type :: value_type real (const _arg1 & __a, const _arg2 & __b) {return __a.real () __b .real ();
template static inline typename _return_type :: value_type imag (const _arg1 & __a, const _arg2 & __b) {return __a.imag () __b.imag ( }};
//
struct _binary_sub_cc {template static inline typename _return_type :: value_type real (const _arg1 & __a, const _arg2 & __b) {return __a.real () - __b .real ();
template static inline typename _return_type :: value_type imag (const _arg1 & __a, const _arg2 & __b) {return __a.imag () - __b.imag ( }};
//
struct _binary_mul_cc {template static inline typename _return_type :: value_type real (const _arg1 & __a, const _arg2 & __b) {return __a.real () * __ b .real () - __a.imag () * __ b.imag ();
template static inline typename _return_type :: value_type imag (const _arg1 & __a, const _arg2 & __b) {return __a.imag () * __ b.real ( ) __A.Real () * __ b.imag ();}}; //
struct _binary_div_cc {template static inline typename _return_type :: value_type real (const _arg1 & __a, const _arg2 & __b) {return (__a.real () * __B.Real () __a.imag () * __ b.imag ()) / (__b.real () * __ b.real () __b.imag () * __ b.imag ());}
template static inline typename _return_type :: value_type imag (const _arg1 & __a, const _arg2 & __b) {return (__a.imag () * __ b.real () - __A.Real () * __ b.imag ()) / (__b.real () * __ b.real () __b.imag () * __ B.Imag ());}}
//
Template struct _unary_expr_cc {type; value_type value_type;
PUBLIC: Const_Arg & _M_ARG;
Public: _UNARY_EXPR_CC (const _arg& __a): _m_arg (__ a) {}
Inline value_type real () const {return_oper :: real (_m_arg);
Inline value_type imag () const {return _oper :: imag (_m_arg);
public: _unary_expr_cc <_unary_expr_cc <_arg, _oper>, _ unary_add_cc> operator () const {typedef _unary_expr_cc <_unary_expr_cc <_arg, _oper>, _unary_add_cc> _expr; return _expr (* this);}
_unary_expr_cc <_unary_expr_cc <_arg, _oper>, _ unary_sub_cc> operator- () const {typedef _unary_expr_cc <_unary_expr_cc <_arg, _oper>, _unary_sub_cc> _expr; return _expr (* this);}}; //
Template struct _binary_expr_cc {typedef type_RETURN_TYPE :: Value_Type Value_Type;
Public: const _arg1 & _m_arg1; const _arg2 & _m_arg2; public: _binary_expr_cc (const _arg1 & __a, const _arg2 & __b): _m_arg1 (__ a), _m_arg2 (__ b) {}
Public: inline value_type real () const {return _oper :: real (_m_arg1, _m_arg2);
Inline value_type imag () const {return _oper :: imag (_m_arg1, _m_arg2);
public: _unary_expr_cc <_binary_expr_cc <_arg1, _arg2, _oper>, _ unary_add_cc> operator () const {typedef _unary_expr_cc <_binary_expr_cc <_arg1, _arg2, _oper>, _unary_add_cc> _expr; return _expr (* this);}
_unary_expr_cc <_binary_expr_cc <_arg1, _arg2, _oper>, _ unary_sub_cc> operator- () const {typedef _unary_expr_cc <_binary_expr_cc <_arg1, _arg2, _oper>, _unary_sub_cc> _expr; return _expr (* this);}};
//
#ifdef _debug // Under Debug, since the compiler is handled in strictly in accordance with the C standard in strict accordance with the C standard (that is, the temporary // variable survival problem), it is necessary to conduct specialization of the constant type. But now the compiler can make good optimization on the code under // Release, the temporary variable is optimized, so there is no special result! Template struct _binary_expr_cc , _ arg2, _oper> {typedef typename _return_type :: value_type value_type; public: const constnt <_tp> _m_arg1; const _arg2 & _m_arg2; public: _binary_expr_cc (const Constnt <_tp> & __a, const _arg2 & __b): _m_arg1 (__), _m_arg2 (__ b) {// cout << "Hello" << Endl;}
Public: inline value_type real () const {return _oper :: real (_m_arg1, _m_arg2);
Inline value_type imag () const {return _oper :: imag (_m_arg1, _m_arg2);
public: _unary_expr_cc <_binary_expr_cc , _ arg2, _oper>, _ unary_add_cc> operator () const {typedef _unary_expr_cc <_binary_expr_cc , _ arg2, _oper>, _unary_add_cc> _expr; return _expr (* this);} }
//
template struct _binary_expr_cc <_arg1, constnt <_tp>, _ oper> {typedef typename _return_type :: value_type value_type;
Public: const_arg1 & _m_arg1; const constant <_tp> _m_arg2; public: _binary_expr_cc (const _arg1 & __a, const constant <_tp> & __b): _m_arg1 (__), _m_arg2 (__ b) {}
Public: inline value_type real () const {return _oper :: real (_m_arg1, _m_arg2);
inline value_type imag () const {return _oper :: imag (_m_arg1, _m_arg2);} public: _unary_expr_cc <_binary_expr_cc <_arg1, constnt <_tp>, _ oper>, _unary_add_cc> operator () const {typedef _unary_expr_cc <_binary_expr_cc <_arg1, constnt <_tp>, _ Oper>, _unary_add_cc> _expr; return_expr (* this);
_unary_expr_cc <_binary_expr_cc <_arg1, constnt <_tp>, _ oper>, _unary_sub_cc> operator- () const {typedef _unary_expr_cc <_binary_expr_cc <_arg1, constnt <_tp>, _ oper>, _unary_sub_cc> _expr; return _expr (* this);}} ; #endif // _debug
//
Template class complex {public: typedef type_tp value_type;
protected: value_type _m_real; value_type _m_imag;
Public: complex (const_tp & __real = _tp (), const _tp & __imag = _tp ()): _m_real = TypeId (value_type)! = typeid (value)) & TypeId (long double))) {cout << "Complex template parameter type only supports floating point numbers!" << endl; exit (0); } #ENDIF / / _DEBUG}
Complex (const complex & __c): _m_real (__ c._m_real), _m_imag (__ c._m_imag) {}
Public: Template complex (const _unary_expr_cc <_arg, _pel> & __e): _m_real (__ E.Real ()), _m_imag ()) {}
Template complex (const _binary_expr_cc <_arg1, _arg2, _oper> & __e): _m_real (__ e.real ()), _m_imag (__ E.imag ()) {} public: inline complex & operator = (const complex & __c) {_m_real = __c.real (); _m_imag = __c.imag (); return * this;}
Template inline complex & operator = (const _unary_expr_cc <_ARG, _UPER> & __E) {_m_real = __e.real (); _m_imag = __e.imag (); return * this;}
Template inline complex & operator = (const _binary_expr_cc <_arg1, _arg2, _oper> & __e) {_m_real = __e.real (); _m_imag = __e.imag (); return * this;}
Public: Inline Complex & Operator = (const complex & __c) {_m_real = __c.real (); _m_imag = __c.imag (); return * this;}
Template Inline complex & operator = (const _unary_expr_cc <_arg, _peal> & __e) {_m_real = __e.real (); _m_imag = __e.imag (); return * this;}
template inline complex & operator = (const _binary_expr_cc <_arg1, _arg2, _oper> & __e) {_m_real = __e.real (); _m_imag = __e.imag (); return * this }
Inline Complex & Operator - = (const complex & __c) {_m_real - = __c.real (); _m_imag - = __c.imag (); return * this;}
Template inline complex & operator - = (const _unary_expr_cc <_ARG, _UNER> & __E) {_m_real - = __e.real (); _m_imag - = __e.imag (); return * this;} template < Class_arg1, class _arg2, class _oper> Inline complex & operator - = (const _binary_expr_cc <_arg1, _arg2, _oper> & __e) {_m_real - = __e.real (); _m_imag - = __e.imag (); return * this; }
Inline complex & operator * = (const complex & __c) {static _tp rx, iod;
Rx = _m_real * __ c.real () - _M_IMAG * __ C.Imag (); ≤ = _m_real * __ c.imag () _m_imag * __ c.real ();
_M_Real = RX; _M_IMAG = IY; RETURN * this;}
Template inline complex & operator * = (const _unary_expr_cc <_arg, _perat> & __e) {static _tp rx, ie, ex, EY;
EX = __e.real (); EY = __e.imag ();
Rx = _m_real * EX - _M_IMAG * EY; IY = _m_real * EY _M_IMAG * EX;
_M_Real = RX; _M_IMAG = IY; RETURN * this;}
Template inline complex & operator * = (const _binary_expr_cc <_arg1, _arg2, _oper> & __e) {static _tp rx, i;
RX = _m_real * __ E.Real () - _M_IMAG * __ E.imag (); ≤ = _m_real * __ E.Imag () _m_imag * __ E.REAL ();
_M_Real = RX; _M_IMAG = IY; RETURN * this;}
Inline complex & operator / = (const complex & __c) {static _tp rx, iy, cx, cy, rt;
CX = __c.real (); cy = __c.imag ();
RT = 1 / (CX * CX CY * CY);
Rx = _m_real * cx _m_imag * cy; = -_m_real * cy _m_imag * cx; _m_real = rx * rt; _m_imag = iy * rt; return * this;}
Template inline complex & operator / = (const _unary_expr_cc <_ARG, _UUNAR> & __E) {static _tp rx, ie, ex, EY, RT;
EX = __e.real (); EY = __e.imag ();
RT = 1 / (EX * EX EY * EY);
Rx = _m_real * EX _M_IMAG * EY; IY = -_m_real * EY _M_IMAG * EX;
_m_real = rx * rt; _m_imag = iy * rt; return * this;}
Template inline complex & operator / = (const _binary_expr_cc <_arg1, _arg2, _oper> & __e) {static _tp rx, ie, ex, EY, RT;
EX = __e.real (); EY = __e.imag ();
RT = 1 / (EX * EX EY * EY);
Rx = _m_real * EX _M_IMAG * EY; IY = -_m_real * EY _M_IMAG * EX;
_m_real = rx * rt; _m_imag = iy * rt; return * this;}
Public: inline value_type real () const {return_m_real;} inline value_type imag () const {return_m_imag;
Inline value_type & real () {return_m_real;} inline value_type & imag () {return _m_imag;}
Public: _UNARY_EXPR_CC , _ unary_add_cc> operator () const {return _UNARY_EXPR_CC , _ unary_add_cc> (* this);
_UNARY_EXPR_CC , _ unary_sub_cc> operator- () const {return _UNARY_EXPR_CC , _ unary_sub_cc> (* this);
}
//
// Template // _ binary_expr_cc <_arg1, _arg2, _add_cc> // Operator (Const _arg1 & a, const _arg2 & b) // {// typef _binary_expr_cc <_arg1, _arg2, _add_cc> _expr; // Return_expr (a, b); //}
#define _DEFINE_COMPLEX_BINARY_FUNC (_func, _name) / / template / inline / _binary_expr_cc <_binary_expr_cc <_arg11, _arg12, _oper1>, / _binary_expr_cc <_arg21, _arg22, _oper2>, / _name /> / _func (const _binary_expr_cc <_arg11, _arg12, _oper1> & a, / const _binary_expr_cc <_arg21, _arg22, _oper2> & b) / {/ typedef _binary_expr_cc <_binary_expr_cc <_arg11, _arg12, _oper1 >, / _BINARY_EXPR_CC <_ARG21, _arg22, _ ga _2> / / _name /> _expr; / return _expr (a, b); /} / / template / inline / _binary_expr_cc <_UNARY_EXPR_CC <_arg1 , _oper1>
, / _Binary_expr_cc <_arg21, _arg22, _oper2>, / _name /> / _func (const _unary_expr_cc <_arg1, _oper1> & a, / const _binary_expr_cc <_arg21, _arg22, _oper2> & b) / {/ typedef _binary_expr_cc <_unary_expr_cc <_arg1 , _ ga ipr1, _arg22, _oper2>, / _name /> _EXPR; / RETURN _EXPR (A, B); /} / template / inline / _binary_expr_cc <_binary_expr_cc <_arg11, _arg12, _oper1>, / _unary_expr_cc <_arg2, _oper2>, / _name /> / _func (const _binary_expr_cc <_arg11, _arg12, _oper1 > & a, / const _unary_expr_cc <_arg2, _ gaple2> &
B) / {/ typedef _binary_expr_cc <_binary_expr_cc <_beary_expr_cc <_arg11, _arg12, _oper1>, / _UNARY_EXPR_CC <_arg2, _ ga), / _name /> _expr; / Return_expr (A, B); /} / Template / inline / _binary_expr_cc <_unary_expr_cc <_arg1, _oper1>, / _UNARY_EXPR_CC <_ARG2, _ gaple2>, / _nam e /> / _func (const _unary_expr_cc <_arg1, _oper1> & a, / const _unary_expr_cc <_arg2, _oper2> & b) / {/ typedef _binary_expr_cc <_unary_expr_cc <_arg1, _oper1>, / _unary_expr_cc <_arg2, _oper2>, / _name /> _EXPR; / RETURN _EXPR (A, B);
/} / / Template / inline / _binary_expr_cc <_binary_expr_cc <_arg11, _arg12, _oper>, / complex <_tp>, / _name /> / _func (const _binary_expr_cc <_arg11, _arg12, _oper> & a, / const complex <_tp> & b) / {/ typedef _binary_expr_cc <_binary_expr_cc <_arg11, _arg12, _oper>, / complex <_tp>, / _name /> _EXPR; / RETURN _EXPR (A, B); /} / / template / inline / _binary_expr_cc , / _binary_expr_cc <_arg21, _arg22, _oper>
, / _Name /> / _func (const complex <_tp> & a, / const _binary_expr_cc <_arg21, _arg22, _oper> & b) / {/ typedef _binary_expr_cc , / _binary_expr_cc <_arg21, _arg22, _oper>, / _Name /> _expr; / return _expr (a, b); /} / template / inline / _Binary_expr_cc <_binary_expr_cc <_arg11, _arg12, _oper>, / constnt <_tp>, / _name /> / _func (const _binary_expr_cc <_arg11, _arg12, _oper> & a, const _tp & b) / {/ typedef _binary_expr_cc <_binary_expr_cc <_arg11 , _arg12, _oper>, / constnt <_tp>
, / _Name /> _EXPR; / RETURN _EXPR (A, B); /} / / template / inline / _binary_expr_cc , / _binary_expr_cc <_arg21, _arg22 , _oper>, / _name /> / _func (const_tp & a, const _binary_expr_cc <_arg21, _arg22, _oper> & b) / {/ typedef _binar Y_EXPR_CC , / _binary_expr_cc <_arg21, _arg22, _oper>, / _name /> _expr; / Return_expr (A, B); /} / / template / inline / _BINARY_EXPR_CC <_UNARY_EXPR_CC <
_arg, _ gaNaMe /> / _func (const _unary_expr_cc <_arg, _perary_expr_cc <_arg, _oper> & a, const complex <_tp> & b) / {/ typef _binary_expr_cc <_UNARY_EXPR_CC <_ARG, _ gainer>, / / / Complex <_tp>, / _name /> _expr; / return _expr (A, b); /} / / template / inline / _BINARY_EXPR_CC , / _unary_expr_cc <_ARG, _PER>, / _NAME /> / _FUNC (Const Complex <_tp> & a, const _unary_expr_cc <_arg, _oper> & b) / {/ typedef _binary_expr_cc , / _UNARY_EXPR_CC <_Arg, _oper>
, / _Name /> _expr; / return _expr (a, b); /} / / template / inline / _binary_expr_cc <_UNARY_EXPR_CC <_ARG, _OPER>, / Constnt <_tp>, / _name /> / _func (const _unary_expr_cc <_arg, _oper> & b) / {/ * This function is not used, just serving as a spare * / / typedef _binary_expr_cc <_UNARY_EXPR_CC <_ar g, _oper>, / constant <_tp>, / _name /> _expr; / return _expr (a, b); /} / template / inline / _binary_expr_cc
, / _Unary_expr_cc <_ARG, _TP> / _NAME /> / _FUNC (const _UNARY_EXPR_CC <_ARG, _UNARY> & B) / {/ typedef _binary_expr_cc , / _unary_expr_cc <_arg, _oper>, / _name /> _expr; / return _expr (a, b); /} / / template / inline / _Binary_expr_cc <_unary_expr_cc <_arg, _oper>, / constnt <_tp>, / _name /> / _func (const _unary_expr_cc <_arg, _oper> & a, const _tp & b) / {/ typedef _binary_expr_cc <_unary_expr_cc <_arg, _oper>, / Constant <_tp>
, / _Name /> _EXPR; / RETURN _EXPR (A, B); /} / / template / inline / _binary_expr_cc , / _unary_expr_cc <_arg, _ gainer>, / _name /> / _func (const _tp & a, const _unary_expr_cc <_arg, _oper> & b) / {/ typef _binar Y_EXPR_CC , / _UNARY_EXPR_CC <_ARG, _PER>, / _NAME /> _EXPR; / RETURN _EXPR (A, B); /} / / template / inline / _binary_expr_cc
, Complex <_TP2>, _ Name> / _func (const complex <_tp1> & __a, const complex <_tp2> & __b) / {/ typedef _binary_expr_cc , / complex <_tp2>, / _name /> _expr; / RETURN _EXPR (__ a, __ b); /} / / template / inline / _binary_expr_cc , / constant <_tp2>, / _Name /> / _func (const complex <_tp1> & __a, const _tp2 & __b) / {/ typedef _binary_expr_cc , / constant <_tp2>, / _name /> _expr; / return _expr (__ a, __ b);
/} / / Template / inline / _binary_expr_cc , / complex <_tp2>, / _name /> / _func (const_tp1) / {/ {/ {/ TYPEDEF _BINARY_EXPR_CC , / complex <_tp2>, / _name /> _expr; / Return _expr (a, b); /} _ DEFINE_COMPLEX_BINARY_FUNC (operator , _binary_add_cc) _DEFINE_COMPLEX_BINARY_FUNC (operator-, _binary_sub_cc) _DEFINE_COMPLEX_BINARY_FUNC (operator *, _binary_mul_cc) _DEFINE_COMPLEX_BINARY_FUNC (operator /, _binary_div_cc) _DEFINE_COMPLEX_BINARY_FUNC (pow, _pow_cc)
//
#define _DEFINE_COMPLEX_UNARY_FUNC (_func, _name) / / template / _unary_expr_cc <_binary_expr_cc <_arg1, _arg2, _oper>, / _name /> / _func (const _binary_expr_cc <_arg1, _arg2, _oper> & __e) / {/ typedef _unary_expr_cc <_binary_expr_cc <_arg1, _arg2, _oper>, / _name /> _expr; / return _expr (__); /} / / Template / _unary_expr_cc <_unary_expr_cc <_arg, _oper>, / _name /> / _func (const _unary_expr_cc <_arg, _oper> & __e) / {/ typedef _unary_expr_cc <_unary_expr_cc <_arg, _oper>, / _name /> _expr;
/ RETURN _EXPR (__ e); /} / / template / _UNARY_EXPR_CC , / _name /> / _func (const complex <_tp> & __c) / {/ typedef _unary_expr_cc , / _Name /> _expr; / return _expr (__ c); /} _ define_complex_unary_func (cos, _cos_cc) _defi NE_COMPLEX_UNARY_FUNC (cosh, _cosh_cc) _DEFINE_COMPLEX_UNARY_FUNC (exp, _exp_cc) _DEFINE_COMPLEX_UNARY_FUNC (log, _log_cc) _DEFINE_COMPLEX_UNARY_FUNC (log10, _log10_cc) _DEFINE_COMPLEX_UNARY_FUNC (sin, _sin_cc) _DEFINE_COMPLEX_UNARY_FUNC (sinh, _sinh_cc)
//
#define _DEFINE_COMPLEX_UNARY_FUNC_EX (_func, _name) / / template / typename _return_type :: value_type / _func (const _binary_expr_cc <_arg1, _arg2, _oper> & __e) / {/ return _name :: evataate (__ e); /} / / template / type_func (const _unary_expr_cc <_ARG, _UPER> & __E)/ {/ Return_name :: evataTe (__ e); /} / / template / type_tp / _func (const complex <_tp> & __c) / {/ return _name :: evataTe (__ c); /}
_Define_complex_unary_func_ex (abs, _abs_cc) _define_complex_unary_func_ex (arg, _arg_cc) _define_complex_unary_func_ex (Norm, _NORM_CC) //
}
#ENDIF / / _COMPLEX_H _ // Test.cpp # include #include using namespace std;
// # include // useming namespace std;
#include "complex.h" Using Namespace Numeric;
Void main () {Complex C1 (1, 1), C2 (2, 2), C3 (3, 3), C4 (4, 4), C5 (5, 5), CC (0, 0) Double D = 1.2, Rx = 0, IY = 0;
Clock_t time = clock (); for (int i = 0; i <10000; i) {for (int J = 0; j <10000; J) {// Due to the compiler's loop optimization =, So use @ = cc * = COS (D (C1 C2) C3);}} Time = Clock () - TIME; COUT << (double) Time / CLK_TCK << endl;
COUT << '(' << xi << ')' << xi << ')' << Endl; Cout << '(' << cc.real () << ',' << cc.imag << ')' << ENDL;
}
/ ************************************************** ***********************
The procedures were compiled with Intel C Compiler 8.0, VC7.1, and GCC 3.3.3, respectively. The ICC80 and VC71 are successfully compiled, and GCC compiles an error.
First use Intel's compiler to test programs: (1) Simple operation ( , -, *, /, =, =, =, * =, / =), by test, it is , - and = , - = The efficiency is more than twice the COMPLEX type of Compaq Visual Fortran 6.5. And in a slightly complex *, /, * = / =, its efficiency is much higher than CVF. (2) Function calculation (COS, SIN, EXP, LOG) These function calculations are more than twice as compared with CVF (Fortran does not have log10, sinh and cosh). (3) The POW function discovers an interesting question when testing: Intel compiler is very sensitive to data when processing the POW function. For example, Complex C1 (1, 1), C2 (2, 2), C3 (3, 3), CC (0, 0); Double D = 1.2; then calculate the following statement 10000 * 10000 times CC = POW (D C1 C2, C3); if the imaginary part of C2 (2, 2) is changed to any number of less than 2, it can be found that there is nearly 70 times the difference between the two calculations. Fortan does not have a POW function, but POW (x, y) can be forms an Exp (Y * log (x)). Using the VS71 compiler to test the program: (1) Program running efficiency and Visual Fortran, simple operation ( , -, *, /, =, =, =, * = / =) compiled and Visual Fortran, = - = slightly lower than fortran, and * = then is about 1/3 of Fortran, but / = actually has twice as far as Fortran. (2) Function calculation (COS, SIN, EXP, SINH, COSH) Use @ = (@ @ 可以,, *, /) operators, calculating efficiency and STL's Complex has no difference. (3) Function (log, log10) The function of the function in (2) is similar, but if an assignment expression in @ = ( = or - =) comes out, the efficiency will be much higher. However, this method is no matter the function in (2). (4) Function (POW) This function is similar to the STL's COMPLEX class if it is directly used. But the method in (3) is also effective, and more interesting is - if it is directly assigned in the cycle: cc.real () = POW (D C1 C2, C3) .Real (); cc. IMAG () = POW (D C1 C2, C3) .imag (); This efficiency is actually twice as much as Fortran. But this method is for (3) (2). *********************************************************** ********************** /