Class: Matrix::EigenvalueDecomposition

Inherits:
Object
  • Object
show all
Defined in:
lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb

Overview

Eigenvalues and eigenvectors of a real matrix.

Computes the eigenvalues and eigenvectors of a matrix A.

If A is diagonalizable, this provides matrices V and D such that A = V*D*V.inv, where D is the diagonal matrix with entries equal to the eigenvalues and V is formed by the eigenvectors.

If A is symmetric, then V is orthogonal and thus A = V*D*V.t

Instance Method Summary collapse

Constructor Details

#initialize(a) ⇒ EigenvalueDecomposition

Constructs the eigenvalue decomposition for a square matrix A

Raises:

  • (TypeError)

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb', line 18

def initialize(a)
  # @d, @e: Arrays for internal storage of eigenvalues.
  # @v: Array for internal storage of eigenvectors.
  # @h: Array for internal storage of nonsymmetric Hessenberg form.
  raise TypeError, "Expected Matrix but got #{a.class}" unless a.is_a?(Matrix)
  @size = a.row_size
  @d = Array.new(@size, 0)
  @e = Array.new(@size, 0)

  if (@symmetric = a.symmetric?)
    @v = a.to_a
    tridiagonalize
    diagonalize
  else
    @v = Array.new(@size) { Array.new(@size, 0) }
    @h = a.to_a
    @ort = Array.new(@size, 0)
    reduce_to_hessenberg
    hessenberg_to_real_schur
  end
end

Instance Method Details

#eigenvalue_matrixObject Also known as: d

Returns the block diagonal eigenvalue matrix D


72
73
74
# File 'lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb', line 72

def eigenvalue_matrix
  Matrix.diagonal(*eigenvalues)
end

#eigenvaluesObject

Returns the eigenvalues in an array


58
59
60
61
62
# File 'lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb', line 58

def eigenvalues
  values = @d.dup
  @e.each_with_index{|imag, i| values[i] = Complex(values[i], imag) unless imag == 0}
  values
end

#eigenvector_matrixObject Also known as: v

Returns the eigenvector matrix V


42
43
44
# File 'lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb', line 42

def eigenvector_matrix
  Matrix.send :new, build_eigenvectors.transpose
end

#eigenvector_matrix_invObject Also known as: v_inv

Returns the inverse of the eigenvector matrix V


49
50
51
52
53
# File 'lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb', line 49

def eigenvector_matrix_inv
  r = Matrix.send :new, build_eigenvectors
  r = r.transpose.inverse unless @symmetric
  r
end

#eigenvectorsObject

Returns an array of the eigenvectors


66
67
68
# File 'lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb', line 66

def eigenvectors
  build_eigenvectors.map{|ev| Vector.send :new, ev}
end

#to_aryObject Also known as: to_a

Returns [eigenvector_matrix, eigenvalue_matrix, eigenvector_matrix_inv]


79
80
81
# File 'lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb', line 79

def to_ary
  [v, d, v_inv]
end