00001 /* 00002 FALCON - The Falcon Programming Language. 00003 FILE: complex.h 00004 00005 Complex class for Falcon 00006 ------------------------------------------------------------------- 00007 Author: Giancarlo Niccolai 00008 Begin: Wed, 09 Sep 2009 23:09:25 +0200 00009 00010 ------------------------------------------------------------------- 00011 (C) Copyright 2009: the FALCON developers (see list in AUTHORS file) 00012 00013 See LICENSE file for licensing details. 00014 */ 00015 00021 #ifndef FALCON_complex_H 00022 #define FALCON_complex_H 00023 00024 #include <falcon/setup.h> 00025 #include <falcon/types.h> 00026 #include <falcon/basealloc.h> 00027 00028 namespace Falcon { 00029 00030 00031 class Complex: public BaseAlloc 00032 { 00033 numeric m_real; 00034 numeric m_imag; 00035 00036 void throw_div_by_zero(); 00037 00038 public: 00039 00040 Complex( numeric r=0, numeric i=0 ): 00041 m_real(r), 00042 m_imag(i) 00043 {} 00044 00045 Complex( const Complex& other ): 00046 m_real( other.m_real ), 00047 m_imag( other.m_imag ) 00048 {} 00049 00050 ~Complex ( ) {} 00051 00052 inline numeric real() const { return m_real; } 00053 inline numeric imag() const { return m_imag; } 00054 inline void real( numeric r ) { m_real = r; } 00055 inline void imag( numeric i ) { m_imag = i; } 00056 00057 //============================================= 00058 // Math operators 00059 // 00060 00061 inline Complex operator +( const Complex &other ) 00062 { 00063 return Complex( m_real + other.m_real, m_imag + other.m_imag ); 00064 } 00065 00066 inline Complex operator -( const Complex &other ) 00067 { 00068 return Complex( m_real - other.m_real, m_imag - other.m_imag ); 00069 } 00070 00071 // (ac−bd,bc+ad) 00072 inline Complex operator *( const Complex &other ) 00073 { 00074 return Complex( m_real * other.m_real - m_imag * other.m_imag, 00075 m_imag * other.m_real + m_real * other.m_imag ); 00076 } 00077 00078 //(ac+bd+i(bc-ad))/(c2+d2) 00079 inline Complex operator /( const Complex &other ) 00080 { 00081 numeric divisor = other.m_real*other.m_real + other.m_imag * other.m_imag; 00082 if ( divisor == 0 ) 00083 throw_div_by_zero(); // don't want this inline. 00084 00085 return Complex( 00086 (m_real * other.m_real + m_imag * other.m_imag) / divisor, 00087 (m_imag * other.m_real - m_real * other.m_imag) / divisor ); 00088 } 00089 00090 numeric abs() const; 00091 Complex conj() const; 00092 00093 //============================================= 00094 // Assignment operators 00095 // 00096 00097 inline Complex &operator =( const Complex &other ) 00098 { 00099 m_real = other.m_real; 00100 m_imag = other.m_imag; 00101 return *this; 00102 } 00103 00104 inline Complex& operator +=( const Complex &other ) 00105 { 00106 m_real += other.m_real; 00107 m_imag += other.m_imag; 00108 return *this; 00109 } 00110 00111 inline Complex& operator -=( const Complex &other ) 00112 { 00113 m_real -= other.m_real; 00114 m_imag -= other.m_imag; 00115 return *this; 00116 } 00117 00118 inline Complex& operator *=( const Complex &other ) 00119 { 00120 m_real = m_real * other.m_real - m_imag * other.m_imag; 00121 m_imag = m_imag * other.m_real + m_real * other.m_imag; 00122 return *this; 00123 } 00124 00125 inline Complex& operator /=( const Complex &other ) 00126 { 00127 *this = *this / other; 00128 return *this; 00129 } 00130 00131 //============================================= 00132 // Relational operators 00133 // 00134 00135 inline bool operator ==( const Complex &other ) 00136 { 00137 return m_real == other.m_real && m_imag == other.m_imag; 00138 } 00139 00140 inline bool operator !=( const Complex &other ) 00141 { 00142 return m_real != other.m_real || m_imag != other.m_imag; 00143 } 00144 00145 inline bool operator >( const Complex &other ) 00146 { 00147 return m_real > other.m_real || (m_real == other.m_real && m_imag > other.m_imag); 00148 } 00149 00150 inline bool operator >=( const Complex &other ) 00151 { 00152 return m_real >= other.m_real || (m_real == other.m_real && m_imag >= other.m_imag); 00153 } 00154 00155 inline bool operator <( const Complex &other ) 00156 { 00157 return m_real < other.m_real || (m_real == other.m_real && m_imag < other.m_imag); 00158 } 00159 00160 inline bool operator <=( const Complex &other ) 00161 { 00162 return m_real < other.m_real || (m_real == other.m_real && m_imag < other.m_imag); 00163 } 00164 00165 }; 00166 00167 } 00168 00169 #endif 00170 00171 /* end of complex.h */