Class: Matrix
- Inherits:
-
Object
- Object
- Matrix
- Defined in:
- lib/ruby_pager/extendmatrix2.rb
Instance Method Summary collapse
- #minor_to_elements(*param) ⇒ Object
- #normalize!(avg) ⇒ Object
- #row_vector_accumulation!(row_index, v) ⇒ Object
- #row_vector_decrement!(row_index, v) ⇒ Object
- #row_vector_distance(row_index, v2) ⇒ Object
- #row_vector_scalar_division!(row_index, val) ⇒ Object
- #to_elements ⇒ Object
- #to_histogram ⇒ Object
- #to_normalized_histogram ⇒ Object
Instance Method Details
#minor_to_elements(*param) ⇒ Object
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 |
# File 'lib/ruby_pager/extendmatrix2.rb', line 78 def minor_to_elements(*param) case param.size when 2 row_range, col_range = param from_row = row_range.first from_row += row_size if from_row < 0 to_row = row_range.end to_row += row_size if to_row < 0 to_row += 1 unless row_range.exclude_end? size_row = to_row - from_row from_col = col_range.first from_col += column_size if from_col < 0 to_col = col_range.end to_col += column_size if to_col < 0 to_col += 1 unless col_range.exclude_end? size_col = to_col - from_col when 4 from_row, size_row, from_col, size_col = param return nil if size_row < 0 || size_col < 0 from_row += row_size if from_row < 0 from_col += column_size if from_col < 0 else Matrix.Raise ArgumentError, param.inspect end return nil if from_row > row_size || from_col > column_size || from_row < 0 || from_col < 0 elements = Array.new rows = @rows[from_row, size_row].each{|row| elements.concat(row[from_col, size_col]) } return elements end |
#normalize!(avg) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby_pager/extendmatrix2.rb', line 5 def normalize!(avg) square=0.0 # require 'ruby-debug'; debugger @rows.each do |row| row.each {|val| square+=(val-avg)**2} end deviation = Math.sqrt(square/(@rows.size*column_size).to_f) deviation = 1.0 if deviation == 0.0 @rows.each_with_index do |row, row_index| row.each_with_index do |e, col_index| @rows[row_index][col_index] = (e - avg) / deviation end end return self end |
#row_vector_accumulation!(row_index, v) ⇒ Object
48 49 50 51 |
# File 'lib/ruby_pager/extendmatrix2.rb', line 48 def row_vector_accumulation!(row_index,v) @rows[row_index].size.times{|i| @rows[row_index][i] += v[i] } return self end |
#row_vector_decrement!(row_index, v) ⇒ Object
53 54 55 56 |
# File 'lib/ruby_pager/extendmatrix2.rb', line 53 def row_vector_decrement!(row_index,v) @rows[row_index].size.times{|i| @rows[row_index][i] -= v[i] } return self end |
#row_vector_distance(row_index, v2) ⇒ Object
62 63 64 65 66 |
# File 'lib/ruby_pager/extendmatrix2.rb', line 62 def row_vector_distance(row_index,v2) dist = 0.0 @rows[row_index].size.times{|i|dist += (@rows[row_index][i]-v2[i])**2} return Math.sqrt(dist) end |
#row_vector_scalar_division!(row_index, val) ⇒ Object
57 58 59 60 |
# File 'lib/ruby_pager/extendmatrix2.rb', line 57 def row_vector_scalar_division!(row_index,val) @rows[row_index].size.times{|i| @rows[row_index][i] /= val } return self end |
#to_elements ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/ruby_pager/extendmatrix2.rb', line 68 def to_elements elements = Array.new @rows.each_with_index do |row, row_index| elements.concat(@rows[row_index]) end return elements end |
#to_histogram ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ruby_pager/extendmatrix2.rb', line 21 def to_histogram histogram = Hash.new histogram.default = 0 @rows.each_with_index do |row, row_index| row.each_with_index do |e, col_index| histogram[e]+=1 end end return histogram end |
#to_normalized_histogram ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ruby_pager/extendmatrix2.rb', line 32 def to_normalized_histogram histogram = Hash.new histogram.default = 0.0 @rows.each_with_index do |row, row_index| row.each_with_index do |e, col_index| histogram[e]+=1.0 end end num_elements = (row_size * column_size).to_f histogram.each do |index,value| histogram[index]/=num_elements end return histogram end |