349 lines
8.2 KiB
C++
349 lines
8.2 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2013-2014
|
|
//----------------------------------------------------------------------------
|
|
// File : ENkComplex.h Data : 08.01.14 Versione : 1.5a1
|
|
// Contenuto : Dichiarazione classe dei numeri complessi.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 08.01.14 DS Creazione modulo.
|
|
//
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
#pragma once
|
|
|
|
//----------------------- Macro per import/export ----------------------------
|
|
#undef ENK_EXPORT
|
|
#if defined( I_AM_ENK) // da definirsi solo nella DLL
|
|
#define ENK_EXPORT __declspec( dllexport)
|
|
#else
|
|
#define ENK_EXPORT __declspec( dllimport)
|
|
#endif
|
|
|
|
//---------------------------Include------------------------------------------
|
|
#include <math.h>
|
|
#include <float.h>
|
|
|
|
|
|
//---------------------------- Class Complex ---------------------------------
|
|
class ENK_EXPORT Complex
|
|
{
|
|
public :
|
|
Complex( double x, double y) : re( x), im( x) {}
|
|
Complex( double x) : re( x), im( 0) {}
|
|
Complex( void) : re( 0), im( 0) {}
|
|
void Set( double x, double y) { re = x ; im = y ; }
|
|
|
|
public :
|
|
Complex operator +=( double dVal) ;
|
|
Complex operator +=( Complex& cVal) ;
|
|
Complex operator -=( double dVal) ;
|
|
Complex operator -=( Complex& cVal) ;
|
|
Complex operator *=( double dVal) ;
|
|
Complex operator *=( Complex& cVal) ;
|
|
Complex operator /=( double dVal) ;
|
|
Complex operator /=( Complex& cVal) ;
|
|
Complex operator >>=( int n) ;
|
|
Complex operator <<=( int n) ;
|
|
|
|
public :
|
|
double re ;
|
|
double im ;
|
|
} ;
|
|
|
|
//------------------------------ Functions -----------------------------------
|
|
ENK_EXPORT Complex sqrt( Complex& cVal) ;
|
|
ENK_EXPORT Complex log( Complex& cVal) ;
|
|
ENK_EXPORT Complex exp( Complex & cVal) ;
|
|
ENK_EXPORT Complex cosh( Complex& cVal) ;
|
|
ENK_EXPORT Complex sinh( Complex& cVal) ;
|
|
ENK_EXPORT Complex tanh( Complex& cVal) ;
|
|
ENK_EXPORT Complex cos( Complex& cVal) ;
|
|
ENK_EXPORT Complex isin( Complex& cVal) ;
|
|
ENK_EXPORT Complex sin( Complex& cVal) ;
|
|
ENK_EXPORT Complex itan( Complex& cVal) ;
|
|
ENK_EXPORT Complex tan( Complex& cVal) ;
|
|
ENK_EXPORT Complex acosh( Complex& cVal) ;
|
|
ENK_EXPORT Complex asinh( Complex& cVal) ;
|
|
ENK_EXPORT Complex atanh( Complex& cVal) ;
|
|
ENK_EXPORT Complex acos( Complex& cVal) ;
|
|
ENK_EXPORT Complex asin( Complex& cVal) ;
|
|
ENK_EXPORT Complex atan( Complex& cVal) ;
|
|
|
|
|
|
//------------------------------ Functions inline -----------------------------------
|
|
// !z and !!z to use as boolean
|
|
inline int
|
|
operator !( Complex& cVal)
|
|
{
|
|
return ( ( cVal.re || cVal.im) ? 0 : 1) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// ~z = z
|
|
inline Complex
|
|
operator ~( Complex& cVal)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = cVal.re ;
|
|
cRes.im = - cVal.im ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline double
|
|
Re( Complex& cVal)
|
|
{
|
|
return cVal.re ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline double
|
|
Im( Complex& cVal)
|
|
{
|
|
return cVal.im ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// negation
|
|
inline Complex
|
|
operator -( Complex& cVal)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = - cVal.re ;
|
|
cRes.im = - cVal.im ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline Complex
|
|
operator -( Complex& cVal1, Complex& cVal2)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = cVal1.re - cVal2.re ;
|
|
cRes.im = cVal1.im - cVal2.im ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline Complex
|
|
operator -( double dVal1, Complex& cVal2)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = dVal1 - cVal2.re ;
|
|
cRes.im = - cVal2.im ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline Complex
|
|
operator -( Complex& cVal1, double dVal2)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = cVal1.re - dVal2 ;
|
|
cRes.im = cVal1.im ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// addition
|
|
inline Complex
|
|
operator +( Complex& cVal)
|
|
{
|
|
return cVal ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline Complex
|
|
operator +( double dVal1, Complex& cVal2)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = dVal1 + cVal2.re ;
|
|
cRes.im = cVal2.im ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline Complex
|
|
operator +( Complex& cVal1, double dVal2)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = cVal1.re + dVal2 ;
|
|
cRes.im = cVal1.im ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline Complex
|
|
operator +( Complex& cVal1, Complex& cVal2)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = cVal1.re + cVal2.re ;
|
|
cRes.im = cVal1.im + cVal2.im ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// multiplication
|
|
inline Complex
|
|
operator *( Complex& cVal1, Complex& cVal2)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = cVal1.re * cVal2.re - cVal1.im * cVal2.im ;
|
|
cRes.im = cVal1.re * cVal2.im + cVal1.im * cVal2.re ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline Complex
|
|
operator *( double dVal1, Complex& cVal2)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = dVal1 * cVal2.re ;
|
|
cRes.im = dVal1 * cVal2.im ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline Complex
|
|
operator *( Complex& cVal1, double dVal2)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = cVal1.re * dVal2 ;
|
|
cRes.im = cVal1.im * dVal2 ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// 2
|
|
// m2(z) = |z|...(used by / )
|
|
inline double
|
|
m2( Complex& cVal)
|
|
{
|
|
return ( cVal.re * cVal.re + cVal.im * cVal.im) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// mod(z) = |z|..............
|
|
inline double
|
|
mod( Complex& cVal)
|
|
{
|
|
if ( fabs( cVal.im) < DBL_EPSILON)
|
|
return fabs( cVal.re) ;
|
|
if ( fabs( cVal.re) < DBL_EPSILON)
|
|
return fabs( cVal.im) ;
|
|
|
|
return sqrt( m2( cVal)) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline Complex
|
|
operator /( Complex& cVal1, double dVal2)
|
|
{
|
|
double dInv ;
|
|
Complex cRes ;
|
|
|
|
|
|
dInv = 1.0 / dVal2 ;
|
|
cRes.re = cVal1.re * dInv ;
|
|
cRes.im = cVal1.im * dInv ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// inversion
|
|
inline Complex
|
|
inv( Complex& cVal)
|
|
{
|
|
return ( ~ cVal / m2( cVal)) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// division as mult for inv
|
|
inline Complex
|
|
operator /( Complex& cVal1, Complex& cVal2)
|
|
{
|
|
|
|
if ( fabs( cVal2.im) < DBL_EPSILON)
|
|
return ( cVal1 / cVal2.re) ;
|
|
|
|
return ( cVal1 * inv( cVal2)) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// fast multiply by +i
|
|
inline Complex
|
|
itimes( Complex& cVal)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = - cVal.im ;
|
|
cRes.im = cVal.re ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// right shift
|
|
inline Complex
|
|
operator >>( Complex& cVal, int n)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = ldexp( cVal.re, -n) ;
|
|
cRes.im = ldexp( cVal.im, -n) ;
|
|
|
|
return cRes ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// left shift
|
|
inline Complex
|
|
operator <<( Complex& cVal, int n)
|
|
{
|
|
Complex cRes ;
|
|
|
|
|
|
cRes.re = ldexp( cVal.re, +n) ;
|
|
cRes.im = ldexp( cVal.im, +n) ;
|
|
|
|
return cRes ;
|
|
}
|