#include #include //#include "mem.h" #include "MATRIX.hpp" // Friend functions VECTOR operator * ( _COMPLEX lhs ,VECTOR rhs ) { return VECTOR( rhs * lhs ); } VECTOR operator / ( _COMPLEX lhs ,VECTOR rhs ) { return VECTOR( rhs / lhs ); } // Class functions & operators VECTOR::VECTOR( ) { m_Local = 0; m_pVals = NULL; vector( NULL ,0 ); } VECTOR::VECTOR( _COMPLEX *pVals ,uint Rows ) { m_Local = 0; m_pVals = NULL; vector( pVals ,Rows ); } VECTOR::VECTOR( uint Rows ) { m_Local = 0; m_pVals = NULL; vector( NULL ,Rows ); } VECTOR::VECTOR( VECTOR &refVEC ) { if( this == &refVEC ) return; m_Local = 0; m_pVals = NULL; vector( refVEC.m_pVals ,refVEC.m_Rows ); } VECTOR::VECTOR( VECTOR *pVECTOR ) { if( this == pVECTOR ) return; m_Local = 0; m_pVals = NULL; vector( pVECTOR-> m_pVals ,pVECTOR-> m_Rows ); } VECTOR::VECTOR( cmplx64 *pVals ,uint Rows ) { m_pVals = NULL; vector( NULL ,Rows ); if( m_pVals ) for( int nRow = m_Rows; nRow--; m_pVals[nRow] = _COMPLEX( pVals[nRow].re ,pVals[nRow].im ) ); } VECTOR::~VECTOR( ) { if( m_pVals && m_Local ) delete [] m_pVals; } void VECTOR::vector( _COMPLEX *pVals ,uint Rows ) { _COMPLEX Zero( 0.0 ,0.0 ); // ----------------------------- if( m_pVals && m_Local && (m_pVals != pVals) ) { delete [] m_pVals; m_pVals = NULL; m_Rows = 0; m_Local = 0; } if( (m_Rows = Rows) && !m_pVals ) { m_pVals = new _COMPLEX[ Rows ]; if( pVals ) for( ; Rows--; m_pVals[Rows] = pVals[Rows] ); else for( ; Rows--; m_pVals[Rows] = Zero ); m_Local = 1; } else m_pVals = pVals; } VECTOR VECTOR::operator *= ( _COMPLEX rhs ){ if( m_pVals ) for( int Row = m_Rows; Row--; m_pVals[Row] *= rhs ); return *this; } VECTOR VECTOR::operator * ( _COMPLEX rhs ) { VECTOR Temp( this ); return VECTOR( Temp *= rhs ); } VECTOR VECTOR::operator *= ( VECTOR rhs ) { if( !m_pVals || (m_Rows != rhs.m_Rows) ) vector( NULL ,rhs.m_Rows ); else for( int Row = m_Rows; Row--; m_pVals[Row] *= rhs.m_pVals[Row] ); return *this; } VECTOR VECTOR::operator * ( VECTOR rhs ) { VECTOR Temp( this ); return VECTOR( Temp *= rhs ); } VECTOR VECTOR::operator /= ( _COMPLEX rhs ) { if( m_pVals ) for( int Row = m_Rows; Row--; m_pVals[Row] /= rhs ); return *this; } VECTOR VECTOR::operator / ( _COMPLEX rhs ) { VECTOR Temp( this ); return VECTOR( Temp /= rhs ); } VECTOR VECTOR::operator /= ( VECTOR rhs ) { if( !m_pVals || (m_Rows != rhs.m_Rows) ) vector( NULL ,rhs.m_Rows ); else for( int Row = m_Rows; Row--; m_pVals[Row] /= rhs.m_pVals[Row] ); return *this; } VECTOR VECTOR::operator / ( VECTOR rhs ) { VECTOR Temp( this ); return VECTOR( Temp /= rhs ); } VECTOR VECTOR::operator += ( VECTOR &rhs ) { if( !m_pVals || (m_Rows != rhs.m_Rows) ) vector( NULL ,rhs.m_Rows ); for( int Row = m_Rows; Row--; m_pVals[Row] += rhs.m_pVals[Row] ); return *this; } VECTOR VECTOR::operator + ( VECTOR &rhs ) { VECTOR Temp( this ); return VECTOR( Temp += rhs ); } VECTOR VECTOR::operator -=( VECTOR &rhs ) { if( !m_pVals || (m_Rows != rhs.m_Rows) ) vector( NULL ,rhs.m_Rows ); for( int Row = m_Rows; Row--; m_pVals[Row] -= rhs.m_pVals[Row] ); return *this; } VECTOR VECTOR::operator - ( VECTOR &rhs ) { VECTOR Temp( this ); return VECTOR( Temp -= rhs ); } VECTOR VECTOR::operator - ( ) { VECTOR Temp; return VECTOR( Temp -= *this ); } VECTOR VECTOR::operator = ( VECTOR &rhs ) { if( this != &rhs ) { if( !m_pVals || (m_Rows != rhs.m_Rows) ) vector( rhs.m_pVals ,rhs.m_Rows ); else for( int Row = m_Rows; Row--; m_pVals[Row] = rhs.m_pVals[Row] ); } return *this; } VECTOR VECTOR::operator = ( double rhs ) { if( !m_pVals || m_Rows < 1 ) vector( NULL ,1 ); for( int Row = m_Rows; Row--; m_pVals[Row] = rhs ); return *this; } VECTOR VECTOR::operator = ( _COMPLEX rhs ) { if( !m_pVals || m_Rows < 1 ) vector( NULL ,1 ); for( int Row = m_Rows; Row--; m_pVals[Row] = rhs ); return *this; } uint VECTOR::operator == ( VECTOR &rhs ) { int Row = __max( m_Rows ,rhs.m_Rows ); if( !Row ) return 1; if( m_Rows == rhs.m_Rows ) for( ; --Row && (m_pVals[Row] == rhs.m_pVals[Row]); ); return Row < 0; } uint VECTOR::operator != ( VECTOR &rhs ) { return !((*this) == rhs); } VECTOR VECTOR::operator ~ ( ) { VECTOR temp( this ); if( m_pVals ) for( int Row = m_Rows; Row--; temp.m_pVals[Row] = _COMPLEX( 1.0 ,0.0 ) / temp.m_pVals[Row] ); return VECTOR( temp ); } _COMPLEX &VECTOR::operator [] (uint Element){ if( !m_pVals || !m_Rows ) vector( NULL ,1 ); Element = Element > (m_Rows-1) ? m_Rows-1 : Element; return m_pVals[ Element ]; } void VECTOR::show( ) { for( int Row = m_Rows; Row--; m_pVals[Row].Show() ); } int VECTOR::len( ) { return m_Rows; } double VECTOR::sumMag( ) { double Sum = 0.0; for( int Row = m_Rows; Row--; Sum += m_pVals[Row].Mag() ); return Sum; } VECTOR VECTOR::re( ) { VECTOR temp( this-> m_Rows ); for( int Row = m_Rows; Row--; temp[ Row ].m_X = (*this)[Row].m_X ); return VECTOR( temp ); } VECTOR VECTOR::im() { VECTOR temp( this-> m_Rows ); for( int Row = m_Rows; Row--; temp[ Row ].m_Y = (*this)[Row].m_Y ); return VECTOR( temp ); }