Hi,
I would like to add the diagonal elements of a complex matrix. Unfortunately, it only computes the sum of the real part only, while the imaginary part equal to zero.
Then, I would like to calculate the average of its sum.
Thanks for your help.
I don't know why I cannot paste my code in an appropriate place. Sorry, I can just paste it here.
//Add elements in a diagonal line of a matrix
#include <conio.h>
#include<iostream>
#include<cmath>
#include<fstream>
#include <complex>
using namespace std;
//Main function
//All the operations is done here
int main(int argc, char** argv)
{
int order = 4;
int i, j;
complex <double > d1sum = 0;
//Z is assumed to be Hermitian Positive Definite where the diagonal elements are integer
complex <double> Z[4][4] = { { 18.44 , -4.2 - 3.8i, 3.5 - 2.3i, 8.0 + 4.2i },
{ -4.2 - 3.8i, 20.5, 0.3 + 4.0i, 7.8 + 2.2i },{ 3.4 - 10.1i, 8.1 + 3.2i, 17.3, 6.2 - 1.8i },{ 0.3 + 3.8i, 10.4 - 12.1i, 5.4 - 8.4i, 26.3 } };
for (i = 0; i < order; i++)
{
for (j = 0; j < order; j++)
{
if (i == j)
d1sum = d1sum + Z[i][j];
}
cout << "\nSum of 1st diagonal is " << d1sum;
//cout << "\n";
system("pause");
return 0;
}
*The answer: Sum of 1st diagonal is (82.54,0)