//+------------------------------------------------------------------+ //| complex.mqh | //| Copyright 2003-2012 Sergey Bochkanov (ALGLIB project) | //| Copyright 2012-2017, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ //| Implementation of ALGLIB library in MetaQuotes Language 5 | //| | //| The features of the library include: | //| - Linear algebra (direct algorithms, EVD, SVD) | //| - Solving systems of linear and non-linear equations | //| - Interpolation | //| - Optimization | //| - FFT (Fast Fourier Transform) | //| - Numerical integration | //| - Linear and nonlinear least-squares fitting | //| - Ordinary differential equations | //| - Computation of special functions | //| - Descriptive statistics and hypothesis testing | //| - Data analysis - classification, regression | //| - Implementing linear algebra algorithms, interpolation, etc. | //| in high-precision arithmetic (using MPFR) | //| | //| This file is free software; you can redistribute it and/or | //| modify it under the terms of the GNU General Public License as | //| published by the Free Software Foundation (www.fsf.org); either | //| version 2 of the License, or (at your option) any later version. | //| | //| This program is distributed in the hope that it will be useful, | //| but WITHOUT ANY WARRANTY; without even the implied warranty of | //| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | //| GNU General Public License for more details. | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Complex numbers | //+------------------------------------------------------------------+ struct complex { public: double re; // real part double im; // imaginary part public: complex(void); complex(const double x); complex(const double x,const double y); ~complex(void); //--- operations void Copy(const complex &rhs); bool Eq(const complex &lhs,const complex &rhs); bool NotEq(const complex &lhs,const complex &rhs); complex Add(const complex &lhs,const complex &rhs); complex Sub(const complex &lhs,const complex &rhs); complex Mul(const complex &lhs,const complex &rhs); complex Div(const complex &lhs,const complex &rhs); //--- overloading void operator=(const double rhs); void operator=(const complex &rhs); void operator+=(const complex &rhs); void operator-=(const complex &rhs); bool operator==(const complex &rhs); bool operator==(const double rhs); bool operator!=(const complex &rhs); bool operator!=(const double rhs); complex operator+(const complex &rhs); complex operator+(const double rhs); complex operator+(void); complex operator-(const complex &rhs); complex operator-(const double rhs); complex operator-(void); complex operator*(const complex &rhs); complex operator*(const double rhs); complex operator/(const complex &rhs); complex operator/(const double rhs); }; //+------------------------------------------------------------------+ //| Constructor without parameters | //+------------------------------------------------------------------+ complex::complex(void): re(0),im(0) { } //+------------------------------------------------------------------+ //| Constructor with one parameter | //+------------------------------------------------------------------+ complex::complex(const double x): re(x),im(0) { } //+------------------------------------------------------------------+ //| Constructor with two parameters | //+------------------------------------------------------------------+ complex::complex(const double x,const double y): re(x),im(y) { } //+------------------------------------------------------------------+ //| Destructor | //+------------------------------------------------------------------+ complex::~complex(void) { } //+------------------------------------------------------------------+ //| Copy complex | //+------------------------------------------------------------------+ void complex::Copy(const complex &rhs) { re=rhs.re; im=rhs.im; } //+------------------------------------------------------------------+ //| Comparison (==) | //+------------------------------------------------------------------+ bool complex::Eq(const complex &lhs,const complex &rhs) { //--- comparison if(lhs.re==rhs.re && lhs.im==rhs.im) return(true); //--- numbers are not equal return(false); } //+------------------------------------------------------------------+ //| Comparison (!=) | //+------------------------------------------------------------------+ bool complex::NotEq(const complex &lhs,const complex &rhs) { //--- comparison if(lhs.re!=rhs.re || lhs.im!=rhs.im) return(true); //--- numbers are equal return(false); } //+------------------------------------------------------------------+ //| Sum | //+------------------------------------------------------------------+ complex complex::Add(const complex &lhs,const complex &rhs) { complex res; //--- sum res.re=lhs.re+rhs.re; res.im=lhs.im+rhs.im; //--- return result return(res); } //+------------------------------------------------------------------+ //| Subtraction | //+------------------------------------------------------------------+ complex complex::Sub(const complex &lhs,const complex &rhs) { complex res; //--- subtraction res.re=lhs.re-rhs.re; res.im=lhs.im-rhs.im; //--- return result return(res); } //+------------------------------------------------------------------+ //| Multiplication | //+------------------------------------------------------------------+ complex complex::Mul(const complex &lhs,const complex &rhs) { complex res; //--- multiplication res.re=lhs.re*rhs.re-lhs.im*rhs.im; res.im=lhs.re*rhs.im+lhs.im*rhs.re; //--- return result return(res); } //+------------------------------------------------------------------+ //| Division | //+------------------------------------------------------------------+ complex complex::Div(const complex &lhs,const complex &rhs) { //--- empty complex value complex res(EMPTY_VALUE,EMPTY_VALUE); //--- check if(rhs.re==0 && rhs.im==0) { Print(__FUNCTION__+": number is zero"); return(res); } //--- create variables double e; double f; //--- division if(MathAbs(rhs.im)