Module: Matrix::Jacobi
- Defined in:
- lib/extendmatrix.rb
Class Method Summary collapse
-
.J(p, q, c, s, n) ⇒ Object
Returns the Jacobi rotation matrix.
-
.max(a) ⇒ Object
Returns the index pair (p, q) with 1<= p < q <= n and A[p, q] is the maximum in absolute value.
-
.off(a) ⇒ Object
Returns the nurm of the off-diagonal element.
-
.sym_schur2(a, p, q) ⇒ Object
Compute the cosine-sine pair (c, s) for the element A[p, q].
Class Method Details
.J(p, q, c, s, n) ⇒ Object
Returns the Jacobi rotation matrix
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
# File 'lib/extendmatrix.rb', line 1059 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
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 |
# File 'lib/extendmatrix.rb', line 1018 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
1008 1009 1010 1011 1012 1013 |
# File 'lib/extendmatrix.rb', line 1008 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]
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 |
# File 'lib/extendmatrix.rb', line 1039 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 |