Conjugate transpose: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Ruby, but mark {{incorrect}} for Matrix#hermitian?.)
(+J)
Line 44: Line 44:
print ' unitary? false'
print ' unitary? false'
end</lang>
end</lang>


=={{header|J}}==

'''Solution''': <lang j> ct =: +@|: NB. Conjugate transpose (ct A is A_ct)</lang>
'''Examples''':<lang j> mm =: +/ . * NB. Matrix Multiply (x)

HERMATIAN =: 3 2j1 ,: 2j_1 1 NB. In J, AjB is A + B*i
(-: ct) HERMATIAN NB. A_ct = A
1

NORMAL =: 1 1 0 , 0 1 1 ,: 1 0 1
((mm~ -: mm) ct) NORMAL NB. A_ct x A = A x A_ct
1

UNITARY =: (-:%:2) * 1 1 0 , 0j_1 0j1 0 ,: 0 0 0j1 * %:2
(ct -: %.) UNITARY NB. A_ct = A^-1
1</lang>

Revision as of 19:49, 30 January 2012

Conjugate transpose is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Suppose that a matrix contains complex numbers. Then the conjugate transpose of is a matrix containing the complex conjugates of the matrix transposition of .

This means that row , column of the conjugate transpose equals the complex conjugate of row , column of the original matrix.

In the next list, must also be a square matrix.

  • A Hermitian matrix equals its own conjugate transpose: .
  • A normal matrix is commutative in multiplication with its conjugate transpose: .
  • A unitary matrix has its inverse equal to its conjugate transpose: . This is true iff and iff , where is the identity matrix.

Given some matrix of complex numbers, find its conjugate transpose. Also determine if it is a Hermitian matrix, normal matrix, or a unitary matrix.

Ruby

This example is incorrect. Please fix the code and remove this message.

Details: Matrix#hermitian? in MRI uses a different definition of Hermitian matrix: it only checks for .

Works with: Ruby version 1.9.3

<lang ruby>require 'matrix'

  1. Start with some matrix.

i = Complex::I matrix = Matrix[[i, 0, 0],

               [0, i, 0],
               [0, 0, i]]
  1. Find the conjugate transpose.
  2. Matrix#conjugate appeared in Ruby 1.9.2.

conjt = matrix.conj.t # aliases for matrix.conjugate.tranpose print 'conjugate tranpose: '; puts conjt

if matrix.square?

 # These predicates appeared in Ruby 1.9.3.
 print 'Hermitian? '; puts matrix.hermitian?
 print '   normal? '; puts matrix.normal?
 print '  unitary? '; puts matrix.unitary?

else

 # Matrix is not square. These predicates would
 # raise ExceptionForMatrix::ErrDimensionMismatch.
 print 'Hermitian? false'
 print '   normal? false'
 print '  unitary? false'

end</lang>


J

Solution: <lang j> ct =: +@|: NB. Conjugate transpose (ct A is A_ct)</lang> Examples:<lang j> mm =: +/ . * NB. Matrix Multiply (x)

  HERMATIAN =:  3 2j1 ,: 2j_1 1   NB. In J, AjB is A + B*i
  (-: ct) HERMATIAN        NB.  A_ct = A

1

  NORMAL    =:  1 1 0 , 0 1 1 ,: 1 0 1
  ((mm~ -: mm) ct) NORMAL  NB. A_ct x A = A x A_ct

1

  UNITARY   =:  (-:%:2) * 1 1 0 , 0j_1 0j1 0 ,: 0 0 0j1 * %:2
  (ct -: %.)  UNITARY      NB.  A_ct = A^-1

1</lang>