1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#pragma once
#ifndef COMPLEXNUMBERHEADERDEF
#define COMPLEXNUMBERHEADERDEF
#include <iostream>
using namespace std;
class ComplexNumber
{
private:
double mRealPart;
double mImaginaryPart;
public:
ComplexNumber();
ComplexNumber(double x, double y);
double CalculateModulus() const;
double CalculateArgument() const;
ComplexNumber CalculatePower(double n) const;
ComplexNumber& operator=(const ComplexNumber& z);
ComplexNumber operator-() const;
ComplexNumber operator+(const ComplexNumber& z) const;
ComplexNumber operator-(const ComplexNumber& z) const;
friend std::ostream& operator<<(std::ostream& output,
const ComplexNumber& z);
};
// Override default constructor
// Set real and imaginary parts to zero
ComplexNumber::ComplexNumber()
{
mRealPart = 0.0;
mImaginaryPart = 0.0;
}
// Constructor that sets complex number z=x+iy
ComplexNumber::ComplexNumber(double x, double y)
{
mRealPart = x;
mImaginaryPart = y;
}
// Method for computing the modulus of a
// complex number
double ComplexNumber::CalculateModulus() const
{
return sqrt(mRealPart * mRealPart +
mImaginaryPart * mImaginaryPart);
}
// Method for computing the argument of a
// complex number
double ComplexNumber::CalculateArgument() const
{
return atan2(mImaginaryPart, mRealPart);
}
// Method for raising complex number to the power n
// using De Moivre's theorem - first complex
// number must be converted to polar form
ComplexNumber ComplexNumber::CalculatePower(double n) const
{
double modulus = CalculateModulus();
double argument = CalculateArgument();
double mod_of_result = pow(modulus, n);
double arg_of_result = argument * n;
double real_part = mod_of_result * cos(arg_of_result);
double imag_part = mod_of_result * sin(arg_of_result);
ComplexNumber z(real_part, imag_part);
return z;
}
// Overloading the = (assignment) operator
ComplexNumber& ComplexNumber::operator=(const ComplexNumber& z)
{
mRealPart = z.mRealPart;
mImaginaryPart = z.mImaginaryPart;
return *this;
}
// Overloading the unary - operator
ComplexNumber ComplexNumber::operator-() const
{
ComplexNumber w;
w.mRealPart = -mRealPart;
w.mImaginaryPart = -mImaginaryPart;
return w;
}
// Overloading the binary + operator
ComplexNumber ComplexNumber::operator+(const ComplexNumber& z) const
{
ComplexNumber w;
w.mRealPart = mRealPart + z.mRealPart;
w.mImaginaryPart = mImaginaryPart + z.mImaginaryPart;
return w;
}
// Overloading the binary - operator
ComplexNumber ComplexNumber::operator-(const ComplexNumber& z) const
{
ComplexNumber w;
w.mRealPart = mRealPart - z.mRealPart;
w.mImaginaryPart = mImaginaryPart - z.mImaginaryPart;
return w;
}
// Overloading the insertion << operator
std::ostream& operator<<(std::ostream& output,const ComplexNumber& z)
{
// Format as "(a + bi)" or as "(a - bi)"
output << "(" << z.mRealPart << " ";
if (z.mImaginaryPart >= 0.0)
{
output << "+ " << z.mImaginaryPart << "i)";
}
else
{
// z.mImaginaryPart < 0.0
// Replace + with minus sign
output << "- " << -z.mImaginaryPart << "i)";
}
return output;
}
class Matrix
{
private:
ComplexNumber** mArr;
int mi = 3;
int mj = 3;
public:
Matrix(int i, int j);
void DeleteMatrix(ComplexNumber**, int, int);
void DisplayMatrix(ComplexNumber**, int, int);
};
//Constructor for complex number matrix, initialising it to 0 for real and imaginary numbers
Matrix::Matrix(int mi, int mj)
{
mArr = new ComplexNumber* [mi];
for (int x = 0; x < mi; x++)
{
mArr[x] = new ComplexNumber[mj];
}
// Initialise array with 0s real and imaginary
for (int x1 = 0; x1 < mi; x1++)
{
for (int y1 = 0; y1 < mj; y1++)
{
mArr[x1][y1] = ComplexNumber(0.0, 0.0);
}
}
}
//Delete matrix
void Matrix::DeleteMatrix(ComplexNumber** Arr, int i, int j)
{
for (int x = 0; x < i; x++)
{
delete[] Arr[x];
}
delete[] Arr;
}
//Member function to display matrix
void Matrix::DisplayMatrix(ComplexNumber** A, int i, int j)
{
for (int x1 = 0; x1 < i; x1++)
{
for (int y1 = 0; y1 < j; y1++)
{
cout << A[x1][y1];
}
}
}
#endif
|