Module: Stick::Matrix::Jacobi

Defined in:
lib/stick/matrix/jacobi.rb

Class Method Summary collapse

Class Method Details

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

Returns the Jacobi rotation matrix



55
56
57
58
59
60
# File 'lib/stick/matrix/jacobi.rb', line 55

def Jacobi.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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stick/matrix/jacobi.rb', line 18

def Jacobi.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



9
10
11
12
13
14
# File 'lib/stick/matrix/jacobi.rb', line 9

def Jacobi.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]



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/stick/matrix/jacobi.rb', line 36

def Jacobi.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