class Matrix

Public Instance Methods

minor_to_elements(*param) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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