method
each_with_index
ruby latest stable - Class:
Matrix
each_with_index(which = :all)public
Same as #each, but the row index and column index in addition to the element
Matrix[ [1,2], [3,4] ].each_with_index do |e, row, col| puts "#{e} at #{row}, #{col}" end # => Prints: # 1 at 0, 0 # 2 at 0, 1 # 3 at 1, 0 # 4 at 1, 1
# File lib/matrix.rb, line 451
def each_with_index(which = :all) # :yield: e, row, column
return to_enum :each_with_index, which unless block_given?
last = column_count - 1
case which
when :all
@rows.each_with_index do |row, row_index|
row.each_with_index do |e, col_index|
yield e, row_index, col_index
end
end
when :diagonal
@rows.each_with_index do |row, row_index|
yield row.fetch(row_index){return self}, row_index, row_index
end
when :off_diagonal
@rows.each_with_index do |row, row_index|
column_count.times do |col_index|
yield row[col_index], row_index, col_index unless row_index == col_index
end
end
when :lower
@rows.each_with_index do |row, row_index|
0.upto([row_index, last].min) do |col_index|
yield row[col_index], row_index, col_index
end
end
when :strict_lower
@rows.each_with_index do |row, row_index|
[row_index, column_count].min.times do |col_index|
yield row[col_index], row_index, col_index
end
end
when :strict_upper
@rows.each_with_index do |row, row_index|
(row_index+1).upto(last) do |col_index|
yield row[col_index], row_index, col_index
end
end
when :upper
@rows.each_with_index do |row, row_index|
row_index.upto(last) do |col_index|
yield row[col_index], row_index, col_index
end
end
else
raise ArgumentError, "expected #{which.inspect} to be one of :all, :diagonal, :off_diagonal, :lower, :strict_lower, :strict_upper or :upper"
end
self
end Related methods
- Instance methods
- *
- **
- +
- +@
- -
- -@
- /
- ==
- []
- adjugate
- clone
- coerce
- cofactor
- cofactor_expansion
- collect
- column
- column_vectors
- combine
- component
- conj
- conjugate
- det
- det_e
- determinant
- determinant_e
- diagonal?
- each
- each_with_index
- eigen
- eigensystem
- element
- elements_to_f
- elements_to_i
- elements_to_r
- empty?
- entrywise_product
- eql?
- find_index
- first_minor
- hadamard_product
- hash
- hermitian?
- hstack
- imag
- imaginary
- index
- inspect
- inv
- inverse
- laplace_expansion
- lower_triangular?
- lup
- lup_decomposition
- map
- minor
- normal?
- orthogonal?
- permutation?
- rank
- rank_e
- real
- real?
- rect
- rectangular
- regular?
- round
- row
- row_count
- row_size
- row_vectors
- singular?
- square?
- symmetric?
- t
- to_a
- to_matrix
- to_s
- tr
- trace
- transpose
- unitary?
- upper_triangular?
- vstack
- zero?
- Class methods
- I
- []
- build
- column_vector
- columns
- combine
- diagonal
- empty
- hstack
- identity
- new
- row_vector
- rows
- scalar
- unit
- vstack
- zero
- Private methods
-
new -
[]= -
determinant_bareiss -
inverse_from -
new_matrix -
set_component -
set_element