How to generate random hermitian matrix


Hi, i want to make a hermitian matrix using random numbers. i have typed this code

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
  #include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>

using namespace std;

int const dimensi=3;
int maks=15;
double matriks[dimensi][dimensi];

int main()
{
    srand(time (0));
   for (int i=0;i<dimensi;i++)
   {
        for (int j=i;j<dimensi;j++)
       {
           matriks[i][j] = rand()% maks;
           matriks[j][i] = matriks[i][j];
           cout << setw(4) << matriks[i][j] ;
       }

       cout << endl;
   }
}


i do not have idea how to print hermitian matrix
can someone help me?
thank's
Last edited on
i have tried it, it print the matrix but not the hermitian.
what i want is the matrix like this :

2 5 6 2
5 7 3 4
6 3 4 1
2 4 1 8


where A=A_transport with random value
closed account (48T7M4Gy)
The Hermitian is a square matrix for a start.
Second, it involves matrix elements that are complex numbers. So you will have to model complex numbers, perhaps best as a Class.
Next you need a function within that class to produce conjugates.
Next you need functionality to create the (square) matrix including functionality to transpose it.

Keep the randomising lines you have. they will come in handy at the appropriate time when sorting out the items I mention above.

Oops, I nearly missed it, you'll need a print or stream function in a couple of places as well. :)

PS it's 'transpose', not 'transport'. And it's a (((complex) conjugate) transpose) - bracketing mine. :)
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
#include <iostream>
#include <complex> // http://en.cppreference.com/w/cpp/numeric/complex
#include <random>
#include <ctime>
#include <iomanip>

// generate a random complex number
std::complex<double> random_complex()
{
    static std::mt19937 rng( std::time(nullptr) ) ;
    static std::uniform_real_distribution<double> distr( 0.0, 10.0 ) ;

    return { distr(rng), distr(rng) } ;
}

template < std::size_t N > using matrix_t = std::complex<double>[N][N] ;

template < std::size_t N > void print( const matrix_t<N>& mtx )
{
    std::cout << std::fixed << std::setprecision(2) << std::showpos ;
    for( auto& row : mtx )
    {
        for( auto& cn : row ) std::cout << std::setw(16) << cn ;
        std::cout << "\n\n" ;
    }
}

int main()
{
   constexpr std::size_t N = 5 ;
   matrix_t<N> matrix ;

   for( std::size_t i = 0 ; i < N ; ++i ) for( std::size_t j = i ; j < N ; ++j )
   {
       const std::complex<double> value = random_complex() ;

       if( i == j ) matrix[i][j] = value.real() ; // diagonal elements must be real

       else
       {
           matrix[i][j] = value ;
           matrix[j][i] = std::conj(value) ; // complex conjugate of value
       }
   }

   print(matrix) ;
}

http://coliru.stacked-crooked.com/a/5917d62842dd1122
closed account (48T7M4Gy)
LOL ... Next! :)
Last edited on
Topic archived. No new replies allowed.