Class: Matrix
- Defined in:
- lib/quality_extensions/matrix/linked_vectors.rb,
lib/quality_extensions/matrix/indexable.rb
Overview
Defined Under Namespace
Classes: MatrixDetails
Instance Method Summary collapse
-
#[]=(i, j, new) ⇒ Object
Changes element (
i
,j
) of the matrix. -
#column(j) ⇒ Object
Returns column vector number
j
of the matrix as a Vector (starting at 0 like an array). -
#row(i) ⇒ Object
Returns row vector number
i
of the matrix as a Vector (starting at 0 like an array).
Instance Method Details
#[]=(i, j, new) ⇒ Object
Changes element (i
,j
) of the matrix. (That is: row i
, column j
.)
16 17 18 |
# File 'lib/quality_extensions/matrix/indexable.rb', line 16 def []=(i, j, new) @rows[i][j] = new end |
#column(j) ⇒ Object
Returns column vector number j
of the matrix as a Vector (starting at 0 like an array). When a block is given, the elements of that vector are iterated.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/quality_extensions/matrix/linked_vectors.rb', line 44 def column(j) # :yield: e if block_given? 0.upto(row_size - 1) do |i| yield @rows[i][j] end else col = (0 .. row_size - 1).collect { |i| @rows[i][j] } # With column vectors, it's a bit trickier to link changes so they propagate to the matrix. The matrix doesn't natively store an array of column arrays. The column array constructed here is already a copy. So we pass the matrix by reference and the column number so that we can later use Matrix#[]= to propagate changes to the vector back to the matrix. Vector.elements(col, false, MatrixDetails.new(self, j)) end end |
#row(i) ⇒ Object
Returns row vector number i
of the matrix as a Vector (starting at 0 like an array). When a block is given, the elements of that vector are iterated.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/quality_extensions/matrix/linked_vectors.rb', line 28 def row(i) # :yield: e if block_given? for e in @rows[i] yield e end else # Because an array of rows happens to be the native internal format of a matrix, the only thing changed was passing copy = false instead of copy = true. Then the rows are passed by reference instead of dup'd and any changes made to them will automatically be made in the matrix as well. Vector.elements(@rows[i], false) end end |