Class: RSemantic::Transform::LSA

Inherits:
Object
  • Object
show all
Defined in:
lib/rsemantic/transform/lsa_transform.rb

Class Method Summary collapse

Class Method Details

.transform!(matrix, rank = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rsemantic/transform/lsa_transform.rb', line 7

def transform!(matrix, rank = nil)
  # TODO configurable rank
  columns = matrix.size2

  # if M < N perform SVD on transponsed matrix
  matrix.size1 < matrix.size2 ? (u, v, sigma = matrix.transpose.SV_decomp_mod) : (u, v, sigma = matrix.SV_decomp_mod)

  reduce_dimensions!(sigma, rank)
  sigma = GSL::Matrix.diagonal(sigma)

  # if M < N return transposed result
  matrix.size1 < matrix.size2 ? GSL::Matrix.swap(matrix, (u * sigma * v.transpose).transpose) : GSL::Matrix.swap(matrix, u * sigma * v.transpose)
end