Class: Clusterer::DMatrix
Class Method Summary collapse
Instance Method Summary collapse
- #[]=(i, j, val) ⇒ Object
-
#svd ⇒ Object
algorithm description from “Simple Algoritms for the partial singular value decomposition” by J.
- #transpose ⇒ Object
Class Method Details
.join_columns(columns) ⇒ Object
105 106 107 |
# File 'lib/clusterer/lsi/dmatrix.rb', line 105 def self.join_columns(columns) DMatrix[*columns.collect {|c| [*c] }].transpose end |
.join_rows(rows) ⇒ Object
94 95 96 |
# File 'lib/clusterer/lsi/dmatrix.rb', line 94 def self.join_rows(rows) DMatrix[*rows.collect {|r| [*r] }] end |
Instance Method Details
#[]=(i, j, val) ⇒ Object
90 91 92 |
# File 'lib/clusterer/lsi/dmatrix.rb', line 90 def []=(i,j,val) @rows[i][j] = val end |
#svd ⇒ Object
algorithm description from “Simple Algoritms for the partial singular value decomposition” by J. C. Nash and S. Shlien Plane rotation method there were some typos in the original algorithm in the paper also see the Pascal code in NashSVD, file alg01.pas; for an idea the partial algorithm is an adaptation of that algo
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/clusterer/lsi/dmatrix.rb', line 32 def svd m, n = self.row_size, self.column_size tol = 0.001 slimit = [n/4.to_i, 6].max u, z, v = DMatrix[*(1..m).to_a.collect {|i| Array.new(n,0) }], Array.new(n), DMatrix.diagonal(*Array.new(n,1)) nt = n slimit.times do rcount = nt *(nt-1)/2 (nt-1).times do |j| (j+1).upto(nt - 1) do |k| p=q=r=0 m.times do |i| p += self[i,j]*self[i,k] q += self[i,j]*self[i,j] r += self[i,k]*self[i,k] end z[j], z[k] = q, r if q < r p, q = p/r, q/r - 1 vt = Math.sqrt(4*p*p + q*q) s = Math.sqrt(0.5*(1 - q/vt)) s = -s if p < 0 c = p / (vt*s) elsif (q * r <= tol * tol) || (p/q)*(p/r) <= tol rcount -= 1 next else p, r = p/q, 1 - r/q vt = Math.sqrt(4*p*p + r*r) c = Math.sqrt(0.5*(1 + r/vt)) s = p/(vt * c) end m.times do |i| r = self[i,j] self[i,j] = c * r + s * self[i,k] self[i,k] = -s*r + c * self[i,k] end n.times do |i| r = v[i,j] v[i,j] = c * r + s * v[i,k] #typo in paper replace r by s v[i,k] = -s*r + c * v[i,k] #typo in paper replace A(i,k) by v(i,k) end end end until nt < 3 || z[nt-1]/(z[0] + tol) > tol nt -= 1 end break unless rcount > 0 end nt.times do |j| z[j] = Math.sqrt(z[j]) m.times {|i| u[i,j] = self[i,j]/z[j] } end z = DMatrix.diagonal(*z) return u, z, v.transpose end |
#transpose ⇒ Object
98 99 100 101 102 103 |
# File 'lib/clusterer/lsi/dmatrix.rb', line 98 def transpose x = super y = DMatrix[] y.instance_variable_set("@rows",x.instance_variable_get("@rows")) y end |