Module: Matrix::Jacobi

Defined in:
lib/extendmatrix.rb

Class Method Summary collapse

Class Method Details

.J(p, q, c, s, n) ⇒ Object

Returns the Jacobi rotation matrix



1084
1085
1086
1087
1088
1089
# File 'lib/extendmatrix.rb', line 1084

def self.J(p, q, c, s, n)
  j = Matrix.I(n)
  j[p,p] = c; j[p, q] = s
  j[q,p] = -s; j[q, q] = c
  j
end

.max(a) ⇒ Object

Returns the index pair (p, q) with 1<= p < q <= n and A[p, q] is the maximum in absolute value



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'lib/extendmatrix.rb', line 1043

def self.max(a)
  n = a.row_size
  max = 0
  p = 0
  q = 0
  n.times{|i|
    ((i+1)...n).each{|j|
      val = a[i, j].abs
      if val > max
        max = val
        p = i
        q = j
      end	
    }
  }
  return p, q
end

.off(a) ⇒ Object

Returns the nurm of the off-diagonal element



1033
1034
1035
1036
1037
1038
# File 'lib/extendmatrix.rb', line 1033

def self.off(a)
  n = a.row_size
  sum = 0
  n.times{|i| n.times{|j| sum += a[i, j]**2 if j != i}}
  Math.sqrt(sum)
end

.sym_schur2(a, p, q) ⇒ Object

Compute the cosine-sine pair (c, s) for the element A[p, q]



1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
# File 'lib/extendmatrix.rb', line 1064

def self.sym_schur2(a, p, q)
  if a[p, q] != 0
    tau = Float(a[q, q] - a[p, p])/(2 * a[p, q])
    if tau >= 0
      t = 1./(tau + Math.sqrt(1 + tau ** 2))
    else
      t = -1./(-tau + Math.sqrt(1 + tau ** 2))
    end
    c = 1./Math.sqrt(1 + t ** 2)
    s = t * c
  else
    c = 1
    s = 0
  end
  return c, s
end