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

📄 complex

📁 realview22.rar
💻
📖 第 1 页 / 共 3 页
字号:
// -*- C++ -*-
/***************************************************************************
 *
 * complex - Declaration for the Standard Library complex class
 *
 * $Id: complex,v 1.1.1.1 2002/01/10 17:38:29 vkorstan Exp $
 *
 ***************************************************************************
 *
 * Copyright (c) 1994-2001 Rogue Wave Software, Inc.  All Rights Reserved.
 *
 * This computer software is owned by Rogue Wave Software, Inc. and is
 * protected by U.S. copyright laws and other laws and by international
 * treaties.  This computer software is furnished by Rogue Wave Software,
 * Inc. pursuant to a written license agreement and may be used, copied,
 * transmitted, and stored only in accordance with the terms of such
 * license and with the inclusion of the above copyright notice.  This
 * computer software or any other copies thereof may not be provided or
 * otherwise made available to any other person.
 *
 * U.S. Government Restricted Rights.  This computer software is provided
 * with Restricted Rights.  Use, duplication, or disclosure by the
 * Government is subject to restrictions as set forth in subparagraph (c)
 * (1) (ii) of The Rights in Technical Data and Computer Software clause
 * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
 * Commercial Computer Software--Restricted Rights at 48 CFR 52.227-19,
 * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
 * Flatiron Parkway, Boulder, Colorado 80301 USA.
 *
 **************************************************************************/

#ifndef _RWSTD_COMPLEX_INCLUDED
#define _RWSTD_COMPLEX_INCLUDED

#include <sstream>
#include <iosfwd>
#include <utility>

#include <rw/_defs.h>
#include <rw/_math.h>


_RWSTD_NAMESPACE_BEGIN (std)

  template <class _TypeT>
  class complex;

  _RWSTD_SPECIALIZED_CLASS class complex<float>;
  _RWSTD_SPECIALIZED_CLASS class complex<double>;
#ifndef _RWSTD_NO_LONG_DOUBLE
  _RWSTD_SPECIALIZED_CLASS class complex<long double>;
#endif


  _RWSTD_SPECIALIZED_CLASS
  class complex<float>
  {
  public:
    typedef float value_type;

    complex (const float& __re=0.0f, const float& __im=0.0f) 
      : _C_re(__re), _C_im(__im)
    { ; }

    _EXPLICIT complex (const complex<double>&); 

#ifndef _RWSTD_NO_LONG_DOUBLE
    _EXPLICIT complex (const complex<long double>&);
#endif

    float imag () const { return _C_im; }
    float real () const { return _C_re; }

#ifndef _RWSTD_NO_MEMBER_TEMPLATES
    template <class _TypeX> complex<float>& operator=  (const complex<_TypeX>& __rhs) {_C_re=__rhs.real(); _C_im=__rhs.imag(); return *this;}
    template <class _TypeX> complex<float>& operator+= (const complex<_TypeX>& __rhs) {_C_re+=__rhs.real(); _C_im+=__rhs.imag(); return *this;}
    template <class _TypeX> complex<float>& operator-= (const complex<_TypeX>& __rhs) {_C_re-=__rhs.real(); _C_im-=__rhs.imag(); return *this;}
    template <class _TypeX> complex<float>& operator*= (const complex<_TypeX>& __rhs) {float __tmp=_C_re*__rhs.real()-_C_im*__rhs.imag(); _C_im=_C_im*__rhs.real()+_C_re*__rhs.imag(); _C_re=__tmp; return *this;}
    template <class _TypeX> complex<float>& operator/= (const complex<_TypeX>&); 
#else /* Have to specialize each one :-( */

      complex& operator=  (const complex &__rhs) {
	  _C_re = __rhs.real();
	  _C_im = __rhs.imag();
	  return *this;
      }

      complex& operator+= (const complex &__rhs) {
	  _C_re += __rhs.real();
	  _C_im += __rhs.imag();
	  return *this;
      }

      complex& operator-= (const complex &__rhs) {
	  _C_re -= __rhs.real();
	  _C_im -= __rhs.imag();
	  return *this;
      }

      complex& operator*= (const complex &__rhs) {
	  value_type __tmp = real () * __rhs.real () - imag () * __rhs.imag();
	  _C_im = imag () * __rhs.real () + real () * __rhs.imag();
	  _C_re = __tmp;
	  return *this;
      }

      complex& operator/= (const complex&);

    complex& operator=  (const complex<double>&); 
    complex& operator+= (const complex<double>&); 
    complex& operator-= (const complex<double>&); 
    complex& operator*= (const complex<double>&); 
    complex& operator/= (const complex<double>&); 

# ifndef _RWSTD_NO_LONG_DOUBLE
    complex& operator=  (const complex<long double>&); 
    complex& operator+= (const complex<long double>&); 
    complex& operator-= (const complex<long double>&); 
    complex& operator*= (const complex<long double>&); 
    complex& operator/= (const complex<long double>&);
# endif
#endif

    complex<float>& operator=  (float); 
    complex<float>& operator+= (float); 
    complex<float>& operator-= (float); 
    complex<float>& operator*= (float); 
    complex<float>& operator/= (float); 

  private:
    float _C_re, _C_im;
  };

  _RWSTD_SPECIALIZED_CLASS
  class complex<double>
  {
  public:
    typedef double value_type;

    complex (const double& __re=0.0, const double& __im=0.0) 
      : _C_re(__re), _C_im(__im)
    { ; } 

    complex (const complex<float>&); 

#ifndef _RWSTD_NO_LONG_DOUBLE  
    _EXPLICIT complex (const complex<long double>&); 
#endif

    double imag () const { return _C_im; }
    double real () const { return _C_re; }

#ifndef _RWSTD_NO_MEMBER_TEMPLATES
    template <class _TypeX> complex<double>& operator=  (const complex<_TypeX>& __rhs) {_C_re=__rhs.real(); _C_im=__rhs.imag(); return *this;}
    template <class _TypeX> complex<double>& operator+= (const complex<_TypeX>& __rhs) {_C_re+=__rhs.real(); _C_im+=__rhs.imag(); return *this;}
    template <class _TypeX> complex<double>& operator-= (const complex<_TypeX>& __rhs) {_C_re-=__rhs.real(); _C_im-=__rhs.imag(); return *this;}
    template <class _TypeX> complex<double>& operator*= (const complex<_TypeX>& __rhs) {double __tmp=_C_re*__rhs.real()-_C_im*__rhs.imag(); _C_im=_C_im*__rhs.real()+_C_re*__rhs.imag(); _C_re=__tmp; return *this;}
    template <class _TypeX> complex<double>& operator/= (const complex<_TypeX>&);

#else /* Have to specialize each one :-( */

    complex<double>& operator=  (const complex<float>&); 
    complex<double>& operator+= (const complex<float>&); 
    complex<double>& operator-= (const complex<float>&);
    complex<double>& operator*= (const complex<float>& __rhs); 
    complex<double>& operator/= (const complex<float>&); 

    complex<double>& operator=  (const complex<double>& __rhs); 
    complex<double>& operator+= (const complex<double>& __rhs); 
    complex<double>& operator-= (const complex<double>& __rhs); 
    complex<double>& operator*= (const complex<double>& __rhs); 
    complex<double>& operator/= (const complex<double>&); 

# ifndef _RWSTD_NO_LONG_DOUBLE  
    complex<double>& operator=  (const complex<long double>&); 
    complex<double>& operator+= (const complex<long double>&); 
    complex<double>& operator-= (const complex<long double>&); 
    complex<double>& operator*= (const complex<long double>&); 
    complex<double>& operator/= (const complex<long double>&);
# endif
#endif

    complex<double>& operator=  (double); 
    complex<double>& operator+= (double); 
    complex<double>& operator-= (double); 
    complex<double>& operator*= (double); 
    complex<double>& operator/= (double); 


  private:
    double _C_re, _C_im;
  };

#ifndef _RWSTD_NO_LONG_DOUBLE  

  _RWSTD_SPECIALIZED_CLASS
  class complex<long double>
  {
  public:
    typedef long double value_type;

    complex (const long double& __re=0.0L, const long double& __im=0.0L)
      : _C_re(__re), _C_im(__im)
    { ; } 
    complex (const complex<float>&);
    complex (const complex<double>&);

    long double imag () const { return _C_im; }
    long double real () const { return _C_re; }

# ifndef _RWSTD_NO_MEMBER_TEMPLATES
    template <class _TypeX> complex<long double>& operator=  (const complex<_TypeX>& __rhs) {_C_re=__rhs.real(); _C_im=__rhs.imag(); return *this;}
    template <class _TypeX> complex<long double>& operator+= (const complex<_TypeX>& __rhs) {_C_re+=__rhs.real(); _C_im+=__rhs.imag(); return *this;}
    template <class _TypeX> complex<long double>& operator-= (const complex<_TypeX>& __rhs) {_C_re-=__rhs.real(); _C_im-=__rhs.imag(); return *this;}
    template <class _TypeX> complex<long double>& operator*= (const complex<_TypeX>& __rhs) {long double __tmp=_C_re*__rhs.real()-_C_im*__rhs.imag(); _C_im=_C_im*__rhs.real()+_C_re*__rhs.imag(); _C_re=__tmp; return *this;}
    template <class _TypeX> complex<long double>& operator/= (const complex<_TypeX>&); 

# else /* Have to specialize each one :-( */

    complex<long double>& operator=  (const complex<float>&); 
    complex<long double>& operator+= (const complex<float>&); 
    complex<long double>& operator-= (const complex<float>&); 
    complex<long double>& operator*= (const complex<float>&); 
    complex<long double>& operator/= (const complex<float>&); 

    complex<long double>& operator=  (const complex<double>&); 
    complex<long double>& operator+= (const complex<double>&); 
    complex<long double>& operator-= (const complex<double>&); 
    complex<long double>& operator*= (const complex<double>&); 
    complex<long double>& operator/= (const complex<double>&); 

    complex<long double>& operator=  (const complex<long double>&); 
    complex<long double>& operator+= (const complex<long double>&); 
    complex<long double>& operator-= (const complex<long double>&); 
    complex<long double>& operator*= (const complex<long double>&); 
    complex<long double>& operator/= (const complex<long double>&); 
# endif

    complex<long double>& operator=  (long double); 
    complex<long double>& operator+= (long double); 
    complex<long double>& operator-= (long double); 
    complex<long double>& operator*= (long double); 
    complex<long double>& operator/= (long double); 


  private:
    long double _C_re, _C_im;
  };
#endif


  template <class _TypeT>
  class complex
  {
  public:
    typedef _TypeT value_type;

    complex (const _TypeT& __re=0, const _TypeT& __im=0) 
      : _C_re(__re), _C_im(__im)
    { ; } 

    _TypeT imag () const { return _C_im; }
    _TypeT real () const { return _C_re; }

#ifndef _RWSTD_NO_MEMBER_TEMPLATES
    template <class _TypeX> complex (const complex<_TypeX>& __rhs) {_C_re=__rhs.real(); _C_im=__rhs.imag();}

    template <class _TypeX> complex<_TypeT>& operator=  (const complex<_TypeX>& __rhs) {_C_re=__rhs.real(); _C_im=__rhs.imag(); return *this;}
    template <class _TypeX> complex<_TypeT>& operator+= (const complex<_TypeX>& __rhs) {_C_re+=__rhs.real(); _C_im+=__rhs.imag(); return *this;}
    template <class _TypeX> complex<_TypeT>& operator-= (const complex<_TypeX>& __rhs) {_C_re-=__rhs.real(); _C_im-=__rhs.imag(); return *this;}
    template <class _TypeX> complex<_TypeT>& operator*= (const complex<_TypeX>& __rhs) {_TypeT __tmp=_C_re*__rhs.real()-_C_im*__rhs.imag(); _C_im=_C_im*__rhs.real()+_C_re*__rhs.imag(); _C_re=__tmp; return *this;}
    template <class _TypeX> complex<_TypeT>& operator/= (const complex<_TypeX>&); 
#endif
    
    complex<_TypeT>& operator=  (const _TypeT&); 
    complex<_TypeT>& operator+= (const _TypeT&); 
    complex<_TypeT>& operator-= (const _TypeT&); 
    complex<_TypeT>& operator*= (const _TypeT&); 
    complex<_TypeT>& operator/= (const _TypeT&); 

  private:
    _TypeT _C_re, _C_im;
  };

//
// complex<float> specializations.
//

  inline
  complex<float>::complex (const complex<double>& cd)
  {
    _C_re = _RWSTD_STATIC_CAST(float,cd.real()); 
    _C_im = _RWSTD_STATIC_CAST(float,cd.imag());
  }

#ifndef _RWSTD_NO_LONG_DOUBLE
  inline
  complex<float>::complex (const complex<long double>& cld)
  {
    _C_re = _RWSTD_STATIC_CAST(float,cld.real()); 
    _C_im = _RWSTD_STATIC_CAST(float,cld.imag());
  }
#endif

  inline complex<float>&
  complex<float>::operator= (float __rhs) 
  {
    _C_re = __rhs; _C_im = 0.0; return *this;
  }

  inline complex<float>&
  complex<float>::operator+= (float __rhs) 
  {
    _C_re += __rhs; return *this;
  }

  inline complex<float>&
  complex<float>::operator-= (float __rhs) 
  {
    _C_re -= __rhs;  return *this;
  }

  inline complex<float>&
  complex<float>::operator*= (float __rhs) 
  {     
    float __tmp = _C_re*__rhs; 
    _C_im       = _C_im*__rhs; 
    _C_re       = __tmp;
    return *this;
  }


#ifdef _RWSTD_NO_MEMBER_TEMPLATES 

  inline complex<float>&
  complex<float>::operator= (const complex<double>& __rhs) 
  {
    _C_re = _RWSTD_STATIC_CAST(float,__rhs.real()); 
    _C_im = _RWSTD_STATIC_CAST(float,__rhs.imag()); 
    return *this;
  }

  inline complex<float>&
  complex<float>::operator+= (const complex<double>& __rhs) 
  {
    _C_re += _RWSTD_STATIC_CAST(float,__rhs.real()); 
    _C_im += _RWSTD_STATIC_CAST(float,__rhs.imag()); 
    return *this;
  }

  inline complex<float>&
  complex<float>::operator-= (const complex<double>& __rhs) 
  {     
    _C_re -= _RWSTD_STATIC_CAST(float,__rhs.real()); 
    _C_im -= _RWSTD_STATIC_CAST(float,__rhs.imag()); 
    return *this;
  }

  inline complex<float>&
  complex<float>::operator*= (const complex<double>& __rhs) 
  {     
    float __tmp = _C_re*_RWSTD_STATIC_CAST(float,__rhs.real())-
    _C_im*_RWSTD_STATIC_CAST(float,__rhs.imag()); 
    _C_im       = _C_im*_RWSTD_STATIC_CAST(float,__rhs.real())+
    _C_re*_RWSTD_STATIC_CAST(float,__rhs.imag()); 
    _C_re       = __tmp;
    return *this;
  }

# ifndef _RWSTD_NO_LONG_DOUBLE
  inline complex<float>&
  complex<float>::operator= (const complex<long double>& __rhs) 
  {     
    _C_re = _RWSTD_STATIC_CAST(float,__rhs.real()); 
    _C_im = _RWSTD_STATIC_CAST(float,__rhs.imag()); 
    return *this;
  }

  inline complex<float>&
  complex<float>::operator+= (const complex<long double>& __rhs) 
  {     
    _C_re += _RWSTD_STATIC_CAST(float,__rhs.real()); 
    _C_im += _RWSTD_STATIC_CAST(float,__rhs.imag()); 
    return *this;
  }

  inline complex<float>&
  complex<float>::operator-= (const complex<long double>& __rhs) 
  {
    _C_re -= _RWSTD_STATIC_CAST(float,__rhs.real()); 
    _C_im -= _RWSTD_STATIC_CAST(float,__rhs.imag()); 
    return *this;
  }

  inline complex<float>&
  complex<float>::operator*= (const complex<long double>& __rhs) 
  {     
    float __tmp = _C_re*_RWSTD_STATIC_CAST(float,__rhs.real())-
    _C_im*_RWSTD_STATIC_CAST(float,__rhs.imag()); 
    _C_im      = _C_im*_RWSTD_STATIC_CAST(float,__rhs.real())+
    _C_re*_RWSTD_STATIC_CAST(float,__rhs.imag()); 
    _C_re      = __tmp; 
    return *this;
  }
# endif
#endif /* _RWSTD_NO_MEMBER_TEMPLATES */
//
// complex<double> specializations.
//
  inline
  complex<double>::complex (const complex<float>& cf)
  : _C_re(cf.real()), _C_im(cf.imag()) {}

#ifndef _RWSTD_NO_LONG_DOUBLE
  inline 
  complex<double>::complex (const complex<long double>& cld)
  : _C_re(_RWSTD_STATIC_CAST(double,cld.real())), 
  _C_im(_RWSTD_STATIC_CAST(double,cld.imag())) {}
#endif

  inline complex<double>&
  complex<double>::operator= (double __rhs) 
  {
    _C_re = __rhs; _C_im = 0.0; return *this;
  }

  inline complex<double>&
  complex<double>::operator+= (double __rhs) 
  {
    _C_re += __rhs; return *this;
  }

  inline complex<double>&
  complex<double>::operator-= (double __rhs) 
  {
    _C_re -= __rhs;  return *this;
  }

  inline complex<double>&
  complex<double>::operator*= (double __rhs) 
  {     
    double __tmp = _C_re*__rhs; 
    _C_im       = _C_im*__rhs; 
    _C_re       = __tmp;
    return *this;
  }


#ifdef _RWSTD_NO_MEMBER_TEMPLATES 
  inline complex<double>&
  complex<double>::operator= (const complex<float>& __rhs) 
  {
    _C_re = __rhs.real(); _C_im = __rhs.imag(); return *this;
  }

⌨️ 快捷键说明

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