How to add a diagonal elements of a complex matrix ?

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)
Last edited on
You don't seem to understand what a Hermitian matrix is: one for which the complex conjugate is equal to the transpose.

Since transposition doesn't move the diagonal elements, these are only equal to their complex conjugates if they are real. So, ALL THE DIAGONAL ELEMENTS ARE REAL: THEY HAVE NO IMAGINARY PART. So the sum will be pure real, with imaginary part zero.

BTW, your matrix is NOT Hermitian - look at the [0][1] and [1][0] components for example.

All the elements on the diagonal of your matrix have zero imaginary part, so their sum (i.e. trace) has, unsurprisingly, zero imaginary part.



nurulhudaismail wrote:
Z is assumed to be Hermitian Positive Definite where the diagonal elements are integer

That is nonsense.
Last edited on
Thank you.

I am sorry for this silly mistake. I admit that I checked it using calculator wrongly. Yes, I absolutely know that the trace of the hermitian matrix gives a real number.
Registered users can post here. Sign in or register to post.