Class: Matrix

Inherits:
Object show all
Defined in:
lib/classifier/extensions/vector.rb,
sig/vendor/matrix.rbs

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[]Matrix

Parameters:

Returns:



21
# File 'sig/vendor/matrix.rbs', line 21

def self.[]: (*Array[untyped]) -> Matrix

.columnsMatrix

Parameters:

Returns:



23
# File 'sig/vendor/matrix.rbs', line 23

def self.columns: (Array[Array[untyped]]) -> Matrix

.diag(diagonal_elements) ⇒ Matrix

Parameters:

Returns:



54
55
56
# File 'lib/classifier/extensions/vector.rb', line 54

def self.diag(diagonal_elements)
  Matrix.diagonal(*diagonal_elements)
end

.emptyMatrix

Parameters:

  • (Integer)
  • (Integer)

Returns:



24
# File 'sig/vendor/matrix.rbs', line 24

def self.empty: (Integer, Integer) -> Matrix

.rowsMatrix

Parameters:

Returns:



20
# File 'sig/vendor/matrix.rbs', line 20

def self.rows: (Array[Array[untyped]]) -> Matrix

.vstackMatrix

Parameters:

Returns:



26
# File 'sig/vendor/matrix.rbs', line 26

def self.vstack: (Matrix, Matrix) -> Matrix

.zeroMatrix

Parameters:

  • (Integer)
  • (Integer)

Returns:



25
# File 'sig/vendor/matrix.rbs', line 25

def self.zero: (Integer, Integer) -> Matrix

Instance Method Details

#*Object

Parameters:

Returns:



29
# File 'sig/vendor/matrix.rbs', line 29

def *: (untyped) -> untyped

#[]=(row_index, col_index, value) ⇒ Object



119
120
121
# File 'lib/classifier/extensions/vector.rb', line 119

def []=(row_index, col_index, value)
  @rows[row_index][col_index] = value
end

#columnVector

Parameters:

  • (Integer)

Returns:



33
# File 'sig/vendor/matrix.rbs', line 33

def column: (Integer) -> Vector

#column_sizeInteger

Returns:

  • (Integer)


31
# File 'sig/vendor/matrix.rbs', line 31

def column_size: () -> Integer

#is_a?Boolean

Parameters:

Returns:

  • (Boolean)


35
# File 'sig/vendor/matrix.rbs', line 35

def is_a?: (untyped) -> bool

#respond_to?Boolean

Parameters:

Returns:

  • (Boolean)


36
# File 'sig/vendor/matrix.rbs', line 36

def respond_to?: (Symbol) -> bool

#rowVector

Parameters:

  • (Integer)

Returns:



32
# File 'sig/vendor/matrix.rbs', line 32

def row: (Integer) -> Vector

#row_sizeInteger

Returns:

  • (Integer)


30
# File 'sig/vendor/matrix.rbs', line 30

def row_size: () -> Integer

#SV_decomp(max_sweeps = 20) ⇒ [Matrix, Matrix, untyped]

Returns:



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/classifier/extensions/vector.rb', line 60

def SV_decomp(max_sweeps = 20)
  q_matrix = if row_size >= column_size
               trans * self
             else
               self * trans
             end

  q_rotation_matrix = q_matrix.dup
  v_matrix = Matrix.identity(q_matrix.row_size)
  iteration_count = 0
  previous_s_matrix = nil

  loop do
    iteration_count += 1
    (0...(q_rotation_matrix.row_size - 1)).each do |row|
      (1..(q_rotation_matrix.row_size - 1)).each do |col|
        next if row == col

        numerator = 2.0 * q_rotation_matrix[row, col]
        denominator = q_rotation_matrix[row, row] - q_rotation_matrix[col, col]

        angle = if denominator.abs < Vector::EPSILON
                  numerator >= 0 ? Math::PI / 4.0 : -Math::PI / 4.0
                else
                  Math.atan(numerator / denominator) / 2.0
                end

        cosine = Math.cos(angle)
        sine = Math.sin(angle)
        rotation_matrix = Matrix.identity(q_rotation_matrix.row_size)
        rotation_matrix[row, row] = cosine
        rotation_matrix[row, col] = -sine
        rotation_matrix[col, row] = sine
        rotation_matrix[col, col] = cosine
        q_rotation_matrix = rotation_matrix.trans * q_rotation_matrix * rotation_matrix
        v_matrix *= rotation_matrix
      end
    end
    previous_s_matrix = q_rotation_matrix.dup if iteration_count == 1
    sum_of_differences = 0.to_r
    if iteration_count > 1
      q_rotation_matrix.row_size.times do |r|
        difference = (q_rotation_matrix[r, r] - previous_s_matrix[r, r]).abs
        sum_of_differences += difference.to_r if difference > 0.001
      end
      previous_s_matrix = q_rotation_matrix.dup
    end
    break if (sum_of_differences <= 0.001 && iteration_count > 1) || iteration_count >= max_sweeps
  end

  singular_values = q_rotation_matrix.row_size.times.map do |r|
    Math.sqrt([q_rotation_matrix[r, r].to_f, 0.0].max)
  end

  safe_singular_values = singular_values.map { |v| [v, Vector::EPSILON].max }
  u_matrix = (row_size >= column_size ? self : trans) * v_matrix * Matrix.diagonal(*safe_singular_values).inverse
  [u_matrix, v_matrix, singular_values]
end

#transposeMatrix Also known as: trans

Returns:



28
# File 'sig/vendor/matrix.rbs', line 28

def transpose: () -> Matrix