classifier 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,12 +21,20 @@ end
21
21
  class Vector
22
22
  EPSILON = 1e-10
23
23
 
24
+ # Cache magnitude since Vector is immutable after creation
25
+ # Note: We undefine the matrix gem's normalize method first, then redefine it
26
+ # to provide a more robust implementation that handles zero vectors
27
+ undef_method :normalize if method_defined?(:normalize)
28
+
24
29
  def magnitude
25
- sum_of_squares = 0.to_r
26
- size.times do |i|
27
- sum_of_squares += self[i]**2.to_r
30
+ # Cache magnitude since Vector is immutable after creation
31
+ @magnitude ||= begin
32
+ sum_of_squares = 0.to_r
33
+ size.times do |i|
34
+ sum_of_squares += self[i]**2.to_r
35
+ end
36
+ Math.sqrt(sum_of_squares.to_f)
28
37
  end
29
- Math.sqrt(sum_of_squares.to_f)
30
38
  end
31
39
 
32
40
  def normalize
@@ -50,8 +50,8 @@ module Classifier
50
50
  #
51
51
  # @rbs (WordList) -> untyped
52
52
  def raw_vector_with(word_list)
53
- vec = if Classifier::LSI.gsl_available
54
- GSL::Vector.alloc(word_list.size)
53
+ vec = if Classifier::LSI.native_available?
54
+ Classifier::LSI.vector_class.alloc(word_list.size)
55
55
  else
56
56
  Array.new(word_list.size, 0)
57
57
  end
@@ -61,8 +61,8 @@ module Classifier
61
61
  end
62
62
 
63
63
  # Perform the scaling transform
64
- total_words = Classifier::LSI.gsl_available ? vec.sum : vec.sum_with_identity
65
- vec_array = Classifier::LSI.gsl_available ? vec.to_a : vec
64
+ total_words = Classifier::LSI.native_available? ? vec.sum : vec.sum_with_identity
65
+ vec_array = Classifier::LSI.native_available? ? vec.to_a : vec
66
66
  total_unique_words = vec_array.count { |word| word != 0 }
67
67
 
68
68
  # Perform first-order association transform if this vector has more
@@ -84,7 +84,7 @@ module Classifier
84
84
  vec = vec.collect { |val| Math.log(val + 1) / divisor }
85
85
  end
86
86
 
87
- if Classifier::LSI.gsl_available
87
+ if Classifier::LSI.native_available?
88
88
  @raw_norm = vec.normalize
89
89
  @raw_vector = vec
90
90
  else