Banner/complex.h
2023-12-08 17:20:48 +01:00

201 lines
5 KiB
C++

// complex.h - declaration of class
// of complex number
//
// The code is property of LIBROW
// You can use it on your own
// When utilizing credit LIBROW site
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
typedef double real;
class complex {
protected:
// Internal presentation - real and imaginary parts
real m_re;
real m_im;
public:
// Imaginary unity
//static const complex i;
//static const complex j;
// Constructors
complex() : m_re (0.), m_im (0.) {}
complex (real re, real im) : m_re (re), m_im (im) {}
complex (real val) : m_re (val), m_im (0.) {}
// Assignment
complex& operator= (const real val) {
m_re = val;
m_im = 0.;
return *this;
}
// Basic operations - taking parts
real re() const { return m_re; }
real im() const { return m_im; }
// Conjugate number
complex conjugate() const {
return complex (m_re, -m_im);
}
// Norm
real norm() const {
return m_re * m_re + m_im * m_im;
}
// Arithmetic operations
complex operator+ (const complex& other) const {
return complex (m_re + other.m_re, m_im + other.m_im);
}
complex operator- (const complex& other) const {
return complex (m_re - other.m_re, m_im - other.m_im);
}
complex operator* (const complex& other) const {
return complex (m_re * other.m_re - m_im * other.m_im,
m_re * other.m_im + m_im * other.m_re);
}
complex operator/ (const complex& other) const {
const real denominator = other.m_re * other.m_re + other.m_im * other.m_im;
return complex ( (m_re * other.m_re + m_im * other.m_im) / denominator,
(m_im * other.m_re - m_re * other.m_im) / denominator);
}
complex& operator+= (const complex& other) {
m_re += other.m_re;
m_im += other.m_im;
return *this;
}
complex& operator-= (const complex& other) {
m_re -= other.m_re;
m_im -= other.m_im;
return *this;
}
complex& operator*= (const complex& other) {
const real temp = m_re;
m_re = m_re * other.m_re - m_im * other.m_im;
m_im = m_im * other.m_re + temp * other.m_im;
return *this;
}
complex& operator/= (const complex& other) {
const real denominator = other.m_re * other.m_re + other.m_im * other.m_im;
const real temp = m_re;
m_re = (m_re * other.m_re + m_im * other.m_im) / denominator;
m_im = (m_im * other.m_re - temp * other.m_im) / denominator;
return *this;
}
complex& operator++ () {
++m_re;
return *this;
}
complex operator++ (int) {
complex temp (*this);
++m_re;
return temp;
}
complex& operator-- () {
--m_re;
return *this;
}
complex operator-- (int) {
complex temp (*this);
--m_re;
return temp;
}
complex operator+ (const real val) const {
return complex (m_re + val, m_im);
}
complex operator- (const real val) const {
return complex (m_re - val, m_im);
}
complex operator* (const real val) const {
return complex (m_re * val, m_im * val);
}
complex operator/ (const real val) const {
return complex (m_re / val, m_im / val);
}
complex& operator+= (const real val) {
m_re += val;
return *this;
}
complex& operator-= (const real val) {
m_re -= val;
return *this;
}
complex& operator*= (const real val) {
m_re *= val;
m_im *= val;
return *this;
}
complex& operator/= (const real val) {
m_re /= val;
m_im /= val;
return *this;
}
friend complex operator+ (const real left, const complex& right) {
return complex (left + right.m_re, right.m_im);
}
friend complex operator- (const real left, const complex& right) {
return complex (left - right.m_re, -right.m_im);
}
friend complex operator* (const real left, const complex& right) {
return complex (left * right.m_re, left * right.m_im);
}
friend complex operator/ (const real left, const complex& right) {
const real denominator = right.m_re * right.m_re + right.m_im * right.m_im;
return complex (left * right.m_re / denominator,
-left * right.m_im / denominator);
}
// Boolean operators
bool operator== (const complex &other) const {
return m_re == other.m_re && m_im == other.m_im;
}
bool operator!= (const complex &other) const {
return m_re != other.m_re || m_im != other.m_im;
}
bool operator== (const real val) const {
return m_re == val && m_im == 0.;
}
bool operator!= (const real val) const {
return m_re != val || m_im != 0.;
}
friend bool operator== (const real left, const complex& right) {
return left == right.m_re && right.m_im == 0.;
}
friend bool operator!= (const real left, const complex& right) {
return left != right.m_re || right.m_im != 0.;
}
};
#endif