user
Suppose that a matrix 

M
{\displaystyle {\displaystyle M}}
 contains complex numbers. Then the conjugate transpose of 

M
{\displaystyle {\displaystyle M}}
 is a matrix 

M

H
{\displaystyle {\displaystyle M^{H}}}
 containing the complex conjugates of the matrix transposition of 

M
{\displaystyle {\displaystyle M}}
.

:   :   :   (

            M

            H

            )

            j
            i
            =

            M

            i
            j
            ¯
            {\displaystyle {\displaystyle (M^{H})\_{ji}={\overline {M\_{ij}}}}}

This means that row 

j
{\displaystyle {\displaystyle j}}
, column 

i
{\displaystyle {\displaystyle i}}
 of the conjugate transpose equals the
  
complex conjugate of row 

i
{\displaystyle {\displaystyle i}}
, column 

j
{\displaystyle {\displaystyle j}}
 of the original matrix.

In the next list, 

M
{\displaystyle {\displaystyle M}}
 must also be a square matrix.

* A Hermitian matrix equals its own conjugate transpose: 

  M

  H
  =
  M
  {\displaystyle {\displaystyle M^{H}=M}}
  .
* A normal matrix is commutative in multiplication with its conjugate transpose: 

  M

  H
  M
  =
  M

  M

  H
  {\displaystyle {\displaystyle M^{H}M=MM^{H}}}
  .
* A unitary matrix has its inverse equal to its conjugate transpose: 

  M

  H
  =

  M

  −
  1
  {\displaystyle {\displaystyle M^{H}=M^{-1}}}
  .   
   This is true iff 

  M

  H
  M
  =

  I

  n
  {\displaystyle {\displaystyle M^{H}M=I\_{n}}}
   and iff 

  M

  M

  H
  =

  I

  n
  {\displaystyle {\displaystyle MM^{H}=I\_{n}}}
  , where 

  I

  n
  {\displaystyle {\displaystyle I\_{n}}}
   is the identity matrix.

Task

Given some matrix of complex numbers, find its conjugate transpose.

Also determine if the matrix is a:

:   :   * Hermitian matrix,
        * normal matrix, or
        * unitary matrix.

See also

* MathWorld entry: conjugate transpose
* MathWorld entry: Hermitian matrix
* MathWorld entry: normal matrix
* MathWorld entry: unitary matrix
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2015-12-13

```
for [ # Test Matrices
       [   1, 1+i, 2i],
       [ 1-i,   5, -3],
       [0-2i,  -3,  0]
    ],
    [
       [1, 1, 0],
       [0, 1, 1],
       [1, 0, 1]
    ],
    [
       [0.707 ,    0.707,  0],
       [0.707i, 0-0.707i,  0],
       [0     ,        0,  i]
    ]
    -> @m {
        say "\nMatrix:";
        @m.&say-it;
        my @t = @m».conj.&mat-trans;
        say "\nTranspose:";
        @t.&say-it;
        say "Is Hermitian?\t{is-Hermitian(@m, @t)}";
        say "Is Normal?\t{is-Normal(@m, @t)}";
        say "Is Unitary?\t{is-Unitary(@m, @t)}";
    }

sub is-Hermitian (@m, @t, --> Bool) {
    so @m».Complex eqv @t».Complex
 }

sub is-Normal (@m, @t, --> Bool) {
    so mat-mult(@m, @t)».Complex eqv mat-mult(@t, @m)».Complex
}

sub is-Unitary (@m, @t, --> Bool) {
    so mat-mult(@m, @t, 1e-3)».Complex eqv mat-ident(+@m)».Complex;
}

sub mat-trans (@m) { map { [ @m[*;$_] ] }, ^@m[0] }

sub mat-ident ($n) { [ map { [ flat 0 xx $_, 1, 0 xx $n - 1 - $_ ] }, ^$n ] }

sub mat-mult (@a, @b, \ε = 1e-15) {
    my @p;
    for ^@a X ^@b[0] -> ($r, $c) {
        @p[$r][$c] += @a[$r][$_] * @b[$_][$c] for ^@b;
        @p[$r][$c].=round(ε); # avoid floating point math errors
    }
    @p
}

sub say-it (@array) { $_».fmt("%9s").say for @array }

```

Output:

```
Matrix:
[        1      1+1i      0+2i]
[     1-1i         5        -3]
[     0-2i        -3         0]

Transpose:
[        1      1+1i      0+2i]
[     1-1i         5        -3]
[     0-2i        -3         0]
Is Hermitian?	True
Is Normal?	True
Is Unitary?	False

Matrix:
[        1         1         0]
[        0         1         1]
[        1         0         1]

Transpose:
[        1         0         1]
[        1         1         0]
[        0         1         1]
Is Hermitian?	False
Is Normal?	True
Is Unitary?	False

Matrix:
[    0.707     0.707         0]
[ 0+0.707i  0-0.707i         0]
[        0         0      0+1i]

Transpose:
[    0.707  0-0.707i         0]
[    0.707  0+0.707i         0]
[        0         0      0-1i]
Is Hermitian?	False
Is Normal?	True
Is Unitary?	True

```