Class: GFA::Matrix
- Inherits:
-
Object
- Object
- GFA::Matrix
- Defined in:
- lib/gfa/matrix.rb
Overview
A class to represent sparse matrices internally used for graph operations
Instance Attribute Summary collapse
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #[](row = nil, col = nil) ⇒ Object
- #[]=(row, col, value) ⇒ Object
-
#append(row, col, value) ⇒ Object
Determines the index of
rowandcol(both must be defined Integer), sets its value to an empty Array if not yet defined, and appendsvalue. -
#col_offset(col) ⇒ Object
Index of the first cell of the
col. -
#index(row = nil, col = nil) ⇒ Object
Returns the list of indexes determined by
rowandcolas an Array: - Ifrowandcolare Integer, it returns the value at the given cell - If bothrowandcolarenil, it returns the indexes for all values - Ifrowxorcolarenil, it returns the indexes of the entire column or row, respectively. -
#initialize(rows, columns, value = nil) ⇒ Matrix
constructor
Initialize a Matrix with
rowsandcolumns(both should be Integer), and a defaultvalue(nilif missing). -
#row_in_column(row) ⇒ Object
Index of the
rowas if it was in the first column.
Constructor Details
#initialize(rows, columns, value = nil) ⇒ Matrix
Initialize a Matrix with rows and columns (both should be Integer), and a default value (nil if missing)
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/gfa/matrix.rb', line 11 def initialize(rows, columns, value = nil) raise 'Matrix rows must be an integer' unless rows.is_a? Integer raise 'Matrix columns must be an integer' unless columns.is_a? Integer raise 'Matrix rows must be positive' if rows < 0 raise 'Matrix columns must be positive' if columns < 0 @rows = rows @columns = columns @values = Hash.new(value) end |
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns.
6 7 8 |
# File 'lib/gfa/matrix.rb', line 6 def columns @columns end |
#rows ⇒ Object
Returns the value of attribute rows.
6 7 8 |
# File 'lib/gfa/matrix.rb', line 6 def rows @rows end |
#values ⇒ Object
Returns the value of attribute values.
6 7 8 |
# File 'lib/gfa/matrix.rb', line 6 def values @values end |
Instance Method Details
#[](row = nil, col = nil) ⇒ Object
22 23 24 |
# File 'lib/gfa/matrix.rb', line 22 def [](row = nil, col = nil) index(row, col).map { |i| values[i] } end |
#[]=(row, col, value) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gfa/matrix.rb', line 26 def []=(row, col, value) values = (row.nil? || col.nil?) ? value : [value] unless values.is_a? Array raise 'Value must be an array if setting a range of cells' end idx = index(row, col) if idx.size != values.size raise "Expected #{idx.size} values, but only got #{values.size}" end idx.each_with_index.map { |i, k| @values[i] = values[k] } end |
#append(row, col, value) ⇒ Object
Determines the index of row and col (both must be defined Integer), sets its value to an empty Array if not yet defined, and appends value. Returns an error if the value already exists but it’s not an array
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/gfa/matrix.rb', line 43 def append(row, col, value) raise 'wow must be a defined integer' unless row.is_a?(Integer) raise 'col must be a defined integer' unless col.is_a?(Integer) idx = index(row, col).first @values[idx] ||= [] unless @values[idx].is_a? Array raise 'The values exists and it is not an array' end @values[idx] << value end |
#col_offset(col) ⇒ Object
Index of the first cell of the col. The column is a 0-based index, with negative integers representing columns counted from the bottom (-1 being the last column)
83 84 85 86 |
# File 'lib/gfa/matrix.rb', line 83 def col_offset(col) col = cols + col if col < 0 col * rows end |
#index(row = nil, col = nil) ⇒ Object
Returns the list of indexes determined by row and col as an Array:
-
If
rowandcolare Integer, it returns the value at the given cell -
If both
rowandcolarenil, it returns the indexes for all values -
If
rowxorcolarenil, it returns the indexes of the entire column or row, respectively.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gfa/matrix.rb', line 62 def index(row = nil, col = nil) if row.nil? && col.nil? # All values (0 .. values.size - 1).to_a elsif row.nil? # Entire column (col_offset(col) .. col_offset(col) + rows - 1).to_a elsif col.nil? # Entire row ric = row_in_column(row) (0 .. columns - 1).map { |i| col_offset(i) + ric } else # Single value [col_offset(col) + row_in_column(row)] end end |
#row_in_column(row) ⇒ Object
Index of the row as if it was in the first column. The row is a 0-based index, with negative integers representing rows counted from the end (-1 being the last row)
92 93 94 95 |
# File 'lib/gfa/matrix.rb', line 92 def row_in_column(row) row = rows + row if row < 0 row end |