Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/share/ruby
File: matrix.rb
# encoding: utf-8
[0] Fix | Delete
# frozen_string_literal: false
[1] Fix | Delete
#
[2] Fix | Delete
# = matrix.rb
[3] Fix | Delete
#
[4] Fix | Delete
# An implementation of Matrix and Vector classes.
[5] Fix | Delete
#
[6] Fix | Delete
# See classes Matrix and Vector for documentation.
[7] Fix | Delete
#
[8] Fix | Delete
# Current Maintainer:: Marc-André Lafortune
[9] Fix | Delete
# Original Author:: Keiju ISHITSUKA
[10] Fix | Delete
# Original Documentation:: Gavin Sinclair (sourced from <i>Ruby in a Nutshell</i> (Matsumoto, O'Reilly))
[11] Fix | Delete
##
[12] Fix | Delete
[13] Fix | Delete
require "e2mmap.rb"
[14] Fix | Delete
[15] Fix | Delete
module ExceptionForMatrix # :nodoc:
[16] Fix | Delete
extend Exception2MessageMapper
[17] Fix | Delete
def_e2message(TypeError, "wrong argument type %s (expected %s)")
[18] Fix | Delete
def_e2message(ArgumentError, "Wrong # of arguments(%d for %d)")
[19] Fix | Delete
[20] Fix | Delete
def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch")
[21] Fix | Delete
def_exception("ErrNotRegular", "Not Regular Matrix")
[22] Fix | Delete
def_exception("ErrOperationNotDefined", "Operation(%s) can\\'t be defined: %s op %s")
[23] Fix | Delete
def_exception("ErrOperationNotImplemented", "Sorry, Operation(%s) not implemented: %s op %s")
[24] Fix | Delete
end
[25] Fix | Delete
[26] Fix | Delete
#
[27] Fix | Delete
# The +Matrix+ class represents a mathematical matrix. It provides methods for creating
[28] Fix | Delete
# matrices, operating on them arithmetically and algebraically,
[29] Fix | Delete
# and determining their mathematical properties such as trace, rank, inverse, determinant,
[30] Fix | Delete
# or eigensystem.
[31] Fix | Delete
#
[32] Fix | Delete
class Matrix
[33] Fix | Delete
include Enumerable
[34] Fix | Delete
include ExceptionForMatrix
[35] Fix | Delete
autoload :EigenvalueDecomposition, "matrix/eigenvalue_decomposition"
[36] Fix | Delete
autoload :LUPDecomposition, "matrix/lup_decomposition"
[37] Fix | Delete
[38] Fix | Delete
# instance creations
[39] Fix | Delete
private_class_method :new
[40] Fix | Delete
attr_reader :rows
[41] Fix | Delete
protected :rows
[42] Fix | Delete
[43] Fix | Delete
#
[44] Fix | Delete
# Creates a matrix where each argument is a row.
[45] Fix | Delete
# Matrix[ [25, 93], [-1, 66] ]
[46] Fix | Delete
# => 25 93
[47] Fix | Delete
# -1 66
[48] Fix | Delete
#
[49] Fix | Delete
def Matrix.[](*rows)
[50] Fix | Delete
rows(rows, false)
[51] Fix | Delete
end
[52] Fix | Delete
[53] Fix | Delete
#
[54] Fix | Delete
# Creates a matrix where +rows+ is an array of arrays, each of which is a row
[55] Fix | Delete
# of the matrix. If the optional argument +copy+ is false, use the given
[56] Fix | Delete
# arrays as the internal structure of the matrix without copying.
[57] Fix | Delete
# Matrix.rows([[25, 93], [-1, 66]])
[58] Fix | Delete
# => 25 93
[59] Fix | Delete
# -1 66
[60] Fix | Delete
#
[61] Fix | Delete
def Matrix.rows(rows, copy = true)
[62] Fix | Delete
rows = convert_to_array(rows, copy)
[63] Fix | Delete
rows.map! do |row|
[64] Fix | Delete
convert_to_array(row, copy)
[65] Fix | Delete
end
[66] Fix | Delete
size = (rows[0] || []).size
[67] Fix | Delete
rows.each do |row|
[68] Fix | Delete
raise ErrDimensionMismatch, "row size differs (#{row.size} should be #{size})" unless row.size == size
[69] Fix | Delete
end
[70] Fix | Delete
new rows, size
[71] Fix | Delete
end
[72] Fix | Delete
[73] Fix | Delete
#
[74] Fix | Delete
# Creates a matrix using +columns+ as an array of column vectors.
[75] Fix | Delete
# Matrix.columns([[25, 93], [-1, 66]])
[76] Fix | Delete
# => 25 -1
[77] Fix | Delete
# 93 66
[78] Fix | Delete
#
[79] Fix | Delete
def Matrix.columns(columns)
[80] Fix | Delete
rows(columns, false).transpose
[81] Fix | Delete
end
[82] Fix | Delete
[83] Fix | Delete
#
[84] Fix | Delete
# Creates a matrix of size +row_count+ x +column_count+.
[85] Fix | Delete
# It fills the values by calling the given block,
[86] Fix | Delete
# passing the current row and column.
[87] Fix | Delete
# Returns an enumerator if no block is given.
[88] Fix | Delete
#
[89] Fix | Delete
# m = Matrix.build(2, 4) {|row, col| col - row }
[90] Fix | Delete
# => Matrix[[0, 1, 2, 3], [-1, 0, 1, 2]]
[91] Fix | Delete
# m = Matrix.build(3) { rand }
[92] Fix | Delete
# => a 3x3 matrix with random elements
[93] Fix | Delete
#
[94] Fix | Delete
def Matrix.build(row_count, column_count = row_count)
[95] Fix | Delete
row_count = CoercionHelper.coerce_to_int(row_count)
[96] Fix | Delete
column_count = CoercionHelper.coerce_to_int(column_count)
[97] Fix | Delete
raise ArgumentError if row_count < 0 || column_count < 0
[98] Fix | Delete
return to_enum :build, row_count, column_count unless block_given?
[99] Fix | Delete
rows = Array.new(row_count) do |i|
[100] Fix | Delete
Array.new(column_count) do |j|
[101] Fix | Delete
yield i, j
[102] Fix | Delete
end
[103] Fix | Delete
end
[104] Fix | Delete
new rows, column_count
[105] Fix | Delete
end
[106] Fix | Delete
[107] Fix | Delete
#
[108] Fix | Delete
# Creates a matrix where the diagonal elements are composed of +values+.
[109] Fix | Delete
# Matrix.diagonal(9, 5, -3)
[110] Fix | Delete
# => 9 0 0
[111] Fix | Delete
# 0 5 0
[112] Fix | Delete
# 0 0 -3
[113] Fix | Delete
#
[114] Fix | Delete
def Matrix.diagonal(*values)
[115] Fix | Delete
size = values.size
[116] Fix | Delete
return Matrix.empty if size == 0
[117] Fix | Delete
rows = Array.new(size) {|j|
[118] Fix | Delete
row = Array.new(size, 0)
[119] Fix | Delete
row[j] = values[j]
[120] Fix | Delete
row
[121] Fix | Delete
}
[122] Fix | Delete
new rows
[123] Fix | Delete
end
[124] Fix | Delete
[125] Fix | Delete
#
[126] Fix | Delete
# Creates an +n+ by +n+ diagonal matrix where each diagonal element is
[127] Fix | Delete
# +value+.
[128] Fix | Delete
# Matrix.scalar(2, 5)
[129] Fix | Delete
# => 5 0
[130] Fix | Delete
# 0 5
[131] Fix | Delete
#
[132] Fix | Delete
def Matrix.scalar(n, value)
[133] Fix | Delete
diagonal(*Array.new(n, value))
[134] Fix | Delete
end
[135] Fix | Delete
[136] Fix | Delete
#
[137] Fix | Delete
# Creates an +n+ by +n+ identity matrix.
[138] Fix | Delete
# Matrix.identity(2)
[139] Fix | Delete
# => 1 0
[140] Fix | Delete
# 0 1
[141] Fix | Delete
#
[142] Fix | Delete
def Matrix.identity(n)
[143] Fix | Delete
scalar(n, 1)
[144] Fix | Delete
end
[145] Fix | Delete
class << Matrix
[146] Fix | Delete
alias unit identity
[147] Fix | Delete
alias I identity
[148] Fix | Delete
end
[149] Fix | Delete
[150] Fix | Delete
#
[151] Fix | Delete
# Creates a zero matrix.
[152] Fix | Delete
# Matrix.zero(2)
[153] Fix | Delete
# => 0 0
[154] Fix | Delete
# 0 0
[155] Fix | Delete
#
[156] Fix | Delete
def Matrix.zero(row_count, column_count = row_count)
[157] Fix | Delete
rows = Array.new(row_count){Array.new(column_count, 0)}
[158] Fix | Delete
new rows, column_count
[159] Fix | Delete
end
[160] Fix | Delete
[161] Fix | Delete
#
[162] Fix | Delete
# Creates a single-row matrix where the values of that row are as given in
[163] Fix | Delete
# +row+.
[164] Fix | Delete
# Matrix.row_vector([4,5,6])
[165] Fix | Delete
# => 4 5 6
[166] Fix | Delete
#
[167] Fix | Delete
def Matrix.row_vector(row)
[168] Fix | Delete
row = convert_to_array(row)
[169] Fix | Delete
new [row]
[170] Fix | Delete
end
[171] Fix | Delete
[172] Fix | Delete
#
[173] Fix | Delete
# Creates a single-column matrix where the values of that column are as given
[174] Fix | Delete
# in +column+.
[175] Fix | Delete
# Matrix.column_vector([4,5,6])
[176] Fix | Delete
# => 4
[177] Fix | Delete
# 5
[178] Fix | Delete
# 6
[179] Fix | Delete
#
[180] Fix | Delete
def Matrix.column_vector(column)
[181] Fix | Delete
column = convert_to_array(column)
[182] Fix | Delete
new [column].transpose, 1
[183] Fix | Delete
end
[184] Fix | Delete
[185] Fix | Delete
#
[186] Fix | Delete
# Creates a empty matrix of +row_count+ x +column_count+.
[187] Fix | Delete
# At least one of +row_count+ or +column_count+ must be 0.
[188] Fix | Delete
#
[189] Fix | Delete
# m = Matrix.empty(2, 0)
[190] Fix | Delete
# m == Matrix[ [], [] ]
[191] Fix | Delete
# => true
[192] Fix | Delete
# n = Matrix.empty(0, 3)
[193] Fix | Delete
# n == Matrix.columns([ [], [], [] ])
[194] Fix | Delete
# => true
[195] Fix | Delete
# m * n
[196] Fix | Delete
# => Matrix[[0, 0, 0], [0, 0, 0]]
[197] Fix | Delete
#
[198] Fix | Delete
def Matrix.empty(row_count = 0, column_count = 0)
[199] Fix | Delete
raise ArgumentError, "One size must be 0" if column_count != 0 && row_count != 0
[200] Fix | Delete
raise ArgumentError, "Negative size" if column_count < 0 || row_count < 0
[201] Fix | Delete
[202] Fix | Delete
new([[]]*row_count, column_count)
[203] Fix | Delete
end
[204] Fix | Delete
[205] Fix | Delete
#
[206] Fix | Delete
# Create a matrix by stacking matrices vertically
[207] Fix | Delete
#
[208] Fix | Delete
# x = Matrix[[1, 2], [3, 4]]
[209] Fix | Delete
# y = Matrix[[5, 6], [7, 8]]
[210] Fix | Delete
# Matrix.vstack(x, y) # => Matrix[[1, 2], [3, 4], [5, 6], [7, 8]]
[211] Fix | Delete
#
[212] Fix | Delete
def Matrix.vstack(x, *matrices)
[213] Fix | Delete
x = CoercionHelper.coerce_to_matrix(x)
[214] Fix | Delete
result = x.send(:rows).map(&:dup)
[215] Fix | Delete
matrices.each do |m|
[216] Fix | Delete
m = CoercionHelper.coerce_to_matrix(m)
[217] Fix | Delete
if m.column_count != x.column_count
[218] Fix | Delete
raise ErrDimensionMismatch, "The given matrices must have #{x.column_count} columns, but one has #{m.column_count}"
[219] Fix | Delete
end
[220] Fix | Delete
result.concat(m.send(:rows))
[221] Fix | Delete
end
[222] Fix | Delete
new result, x.column_count
[223] Fix | Delete
end
[224] Fix | Delete
[225] Fix | Delete
[226] Fix | Delete
#
[227] Fix | Delete
# Create a matrix by stacking matrices horizontally
[228] Fix | Delete
#
[229] Fix | Delete
# x = Matrix[[1, 2], [3, 4]]
[230] Fix | Delete
# y = Matrix[[5, 6], [7, 8]]
[231] Fix | Delete
# Matrix.hstack(x, y) # => Matrix[[1, 2, 5, 6], [3, 4, 7, 8]]
[232] Fix | Delete
#
[233] Fix | Delete
def Matrix.hstack(x, *matrices)
[234] Fix | Delete
x = CoercionHelper.coerce_to_matrix(x)
[235] Fix | Delete
result = x.send(:rows).map(&:dup)
[236] Fix | Delete
total_column_count = x.column_count
[237] Fix | Delete
matrices.each do |m|
[238] Fix | Delete
m = CoercionHelper.coerce_to_matrix(m)
[239] Fix | Delete
if m.row_count != x.row_count
[240] Fix | Delete
raise ErrDimensionMismatch, "The given matrices must have #{x.row_count} rows, but one has #{m.row_count}"
[241] Fix | Delete
end
[242] Fix | Delete
result.each_with_index do |row, i|
[243] Fix | Delete
row.concat m.send(:rows)[i]
[244] Fix | Delete
end
[245] Fix | Delete
total_column_count += m.column_count
[246] Fix | Delete
end
[247] Fix | Delete
new result, total_column_count
[248] Fix | Delete
end
[249] Fix | Delete
[250] Fix | Delete
#
[251] Fix | Delete
# Create a matrix by combining matrices entrywise, using the given block
[252] Fix | Delete
#
[253] Fix | Delete
# x = Matrix[[6, 6], [4, 4]]
[254] Fix | Delete
# y = Matrix[[1, 2], [3, 4]]
[255] Fix | Delete
# Matrix.combine(x, y) {|a, b| a - b} # => Matrix[[5, 4], [1, 0]]
[256] Fix | Delete
#
[257] Fix | Delete
def Matrix.combine(*matrices)
[258] Fix | Delete
return to_enum(__method__, *matrices) unless block_given?
[259] Fix | Delete
[260] Fix | Delete
return Matrix.empty if matrices.empty?
[261] Fix | Delete
matrices.map!(&CoercionHelper.method(:coerce_to_matrix))
[262] Fix | Delete
x = matrices.first
[263] Fix | Delete
matrices.each do |m|
[264] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless x.row_count == m.row_count && x.column_count == m.column_count
[265] Fix | Delete
end
[266] Fix | Delete
[267] Fix | Delete
rows = Array.new(x.row_count) do |i|
[268] Fix | Delete
Array.new(x.column_count) do |j|
[269] Fix | Delete
yield matrices.map{|m| m[i,j]}
[270] Fix | Delete
end
[271] Fix | Delete
end
[272] Fix | Delete
new rows, x.column_count
[273] Fix | Delete
end
[274] Fix | Delete
[275] Fix | Delete
def combine(*matrices, &block)
[276] Fix | Delete
Matrix.combine(self, *matrices, &block)
[277] Fix | Delete
end
[278] Fix | Delete
[279] Fix | Delete
#
[280] Fix | Delete
# Matrix.new is private; use Matrix.rows, columns, [], etc... to create.
[281] Fix | Delete
#
[282] Fix | Delete
def initialize(rows, column_count = rows[0].size)
[283] Fix | Delete
# No checking is done at this point. rows must be an Array of Arrays.
[284] Fix | Delete
# column_count must be the size of the first row, if there is one,
[285] Fix | Delete
# otherwise it *must* be specified and can be any integer >= 0
[286] Fix | Delete
@rows = rows
[287] Fix | Delete
@column_count = column_count
[288] Fix | Delete
end
[289] Fix | Delete
[290] Fix | Delete
def new_matrix(rows, column_count = rows[0].size) # :nodoc:
[291] Fix | Delete
self.class.send(:new, rows, column_count) # bypass privacy of Matrix.new
[292] Fix | Delete
end
[293] Fix | Delete
private :new_matrix
[294] Fix | Delete
[295] Fix | Delete
#
[296] Fix | Delete
# Returns element (+i+,+j+) of the matrix. That is: row +i+, column +j+.
[297] Fix | Delete
#
[298] Fix | Delete
def [](i, j)
[299] Fix | Delete
@rows.fetch(i){return nil}[j]
[300] Fix | Delete
end
[301] Fix | Delete
alias element []
[302] Fix | Delete
alias component []
[303] Fix | Delete
[304] Fix | Delete
def []=(i, j, v)
[305] Fix | Delete
@rows[i][j] = v
[306] Fix | Delete
end
[307] Fix | Delete
alias set_element []=
[308] Fix | Delete
alias set_component []=
[309] Fix | Delete
private :[]=, :set_element, :set_component
[310] Fix | Delete
[311] Fix | Delete
#
[312] Fix | Delete
# Returns the number of rows.
[313] Fix | Delete
#
[314] Fix | Delete
def row_count
[315] Fix | Delete
@rows.size
[316] Fix | Delete
end
[317] Fix | Delete
[318] Fix | Delete
alias_method :row_size, :row_count
[319] Fix | Delete
#
[320] Fix | Delete
# Returns the number of columns.
[321] Fix | Delete
#
[322] Fix | Delete
attr_reader :column_count
[323] Fix | Delete
alias_method :column_size, :column_count
[324] Fix | Delete
[325] Fix | Delete
#
[326] Fix | Delete
# Returns row vector number +i+ of the matrix as a Vector (starting at 0 like
[327] Fix | Delete
# an array). When a block is given, the elements of that vector are iterated.
[328] Fix | Delete
#
[329] Fix | Delete
def row(i, &block) # :yield: e
[330] Fix | Delete
if block_given?
[331] Fix | Delete
@rows.fetch(i){return self}.each(&block)
[332] Fix | Delete
self
[333] Fix | Delete
else
[334] Fix | Delete
Vector.elements(@rows.fetch(i){return nil})
[335] Fix | Delete
end
[336] Fix | Delete
end
[337] Fix | Delete
[338] Fix | Delete
#
[339] Fix | Delete
# Returns column vector number +j+ of the matrix as a Vector (starting at 0
[340] Fix | Delete
# like an array). When a block is given, the elements of that vector are
[341] Fix | Delete
# iterated.
[342] Fix | Delete
#
[343] Fix | Delete
def column(j) # :yield: e
[344] Fix | Delete
if block_given?
[345] Fix | Delete
return self if j >= column_count || j < -column_count
[346] Fix | Delete
row_count.times do |i|
[347] Fix | Delete
yield @rows[i][j]
[348] Fix | Delete
end
[349] Fix | Delete
self
[350] Fix | Delete
else
[351] Fix | Delete
return nil if j >= column_count || j < -column_count
[352] Fix | Delete
col = Array.new(row_count) {|i|
[353] Fix | Delete
@rows[i][j]
[354] Fix | Delete
}
[355] Fix | Delete
Vector.elements(col, false)
[356] Fix | Delete
end
[357] Fix | Delete
end
[358] Fix | Delete
[359] Fix | Delete
#
[360] Fix | Delete
# Returns a matrix that is the result of iteration of the given block over all
[361] Fix | Delete
# elements of the matrix.
[362] Fix | Delete
# Matrix[ [1,2], [3,4] ].collect { |e| e**2 }
[363] Fix | Delete
# => 1 4
[364] Fix | Delete
# 9 16
[365] Fix | Delete
#
[366] Fix | Delete
def collect(&block) # :yield: e
[367] Fix | Delete
return to_enum(:collect) unless block_given?
[368] Fix | Delete
rows = @rows.collect{|row| row.collect(&block)}
[369] Fix | Delete
new_matrix rows, column_count
[370] Fix | Delete
end
[371] Fix | Delete
alias map collect
[372] Fix | Delete
[373] Fix | Delete
#
[374] Fix | Delete
# Yields all elements of the matrix, starting with those of the first row,
[375] Fix | Delete
# or returns an Enumerator if no block given.
[376] Fix | Delete
# Elements can be restricted by passing an argument:
[377] Fix | Delete
# * :all (default): yields all elements
[378] Fix | Delete
# * :diagonal: yields only elements on the diagonal
[379] Fix | Delete
# * :off_diagonal: yields all elements except on the diagonal
[380] Fix | Delete
# * :lower: yields only elements on or below the diagonal
[381] Fix | Delete
# * :strict_lower: yields only elements below the diagonal
[382] Fix | Delete
# * :strict_upper: yields only elements above the diagonal
[383] Fix | Delete
# * :upper: yields only elements on or above the diagonal
[384] Fix | Delete
#
[385] Fix | Delete
# Matrix[ [1,2], [3,4] ].each { |e| puts e }
[386] Fix | Delete
# # => prints the numbers 1 to 4
[387] Fix | Delete
# Matrix[ [1,2], [3,4] ].each(:strict_lower).to_a # => [3]
[388] Fix | Delete
#
[389] Fix | Delete
def each(which = :all) # :yield: e
[390] Fix | Delete
return to_enum :each, which unless block_given?
[391] Fix | Delete
last = column_count - 1
[392] Fix | Delete
case which
[393] Fix | Delete
when :all
[394] Fix | Delete
block = Proc.new
[395] Fix | Delete
@rows.each do |row|
[396] Fix | Delete
row.each(&block)
[397] Fix | Delete
end
[398] Fix | Delete
when :diagonal
[399] Fix | Delete
@rows.each_with_index do |row, row_index|
[400] Fix | Delete
yield row.fetch(row_index){return self}
[401] Fix | Delete
end
[402] Fix | Delete
when :off_diagonal
[403] Fix | Delete
@rows.each_with_index do |row, row_index|
[404] Fix | Delete
column_count.times do |col_index|
[405] Fix | Delete
yield row[col_index] unless row_index == col_index
[406] Fix | Delete
end
[407] Fix | Delete
end
[408] Fix | Delete
when :lower
[409] Fix | Delete
@rows.each_with_index do |row, row_index|
[410] Fix | Delete
0.upto([row_index, last].min) do |col_index|
[411] Fix | Delete
yield row[col_index]
[412] Fix | Delete
end
[413] Fix | Delete
end
[414] Fix | Delete
when :strict_lower
[415] Fix | Delete
@rows.each_with_index do |row, row_index|
[416] Fix | Delete
[row_index, column_count].min.times do |col_index|
[417] Fix | Delete
yield row[col_index]
[418] Fix | Delete
end
[419] Fix | Delete
end
[420] Fix | Delete
when :strict_upper
[421] Fix | Delete
@rows.each_with_index do |row, row_index|
[422] Fix | Delete
(row_index+1).upto(last) do |col_index|
[423] Fix | Delete
yield row[col_index]
[424] Fix | Delete
end
[425] Fix | Delete
end
[426] Fix | Delete
when :upper
[427] Fix | Delete
@rows.each_with_index do |row, row_index|
[428] Fix | Delete
row_index.upto(last) do |col_index|
[429] Fix | Delete
yield row[col_index]
[430] Fix | Delete
end
[431] Fix | Delete
end
[432] Fix | Delete
else
[433] Fix | Delete
raise ArgumentError, "expected #{which.inspect} to be one of :all, :diagonal, :off_diagonal, :lower, :strict_lower, :strict_upper or :upper"
[434] Fix | Delete
end
[435] Fix | Delete
self
[436] Fix | Delete
end
[437] Fix | Delete
[438] Fix | Delete
#
[439] Fix | Delete
# Same as #each, but the row index and column index in addition to the element
[440] Fix | Delete
#
[441] Fix | Delete
# Matrix[ [1,2], [3,4] ].each_with_index do |e, row, col|
[442] Fix | Delete
# puts "#{e} at #{row}, #{col}"
[443] Fix | Delete
# end
[444] Fix | Delete
# # => Prints:
[445] Fix | Delete
# # 1 at 0, 0
[446] Fix | Delete
# # 2 at 0, 1
[447] Fix | Delete
# # 3 at 1, 0
[448] Fix | Delete
# # 4 at 1, 1
[449] Fix | Delete
#
[450] Fix | Delete
def each_with_index(which = :all) # :yield: e, row, column
[451] Fix | Delete
return to_enum :each_with_index, which unless block_given?
[452] Fix | Delete
last = column_count - 1
[453] Fix | Delete
case which
[454] Fix | Delete
when :all
[455] Fix | Delete
@rows.each_with_index do |row, row_index|
[456] Fix | Delete
row.each_with_index do |e, col_index|
[457] Fix | Delete
yield e, row_index, col_index
[458] Fix | Delete
end
[459] Fix | Delete
end
[460] Fix | Delete
when :diagonal
[461] Fix | Delete
@rows.each_with_index do |row, row_index|
[462] Fix | Delete
yield row.fetch(row_index){return self}, row_index, row_index
[463] Fix | Delete
end
[464] Fix | Delete
when :off_diagonal
[465] Fix | Delete
@rows.each_with_index do |row, row_index|
[466] Fix | Delete
column_count.times do |col_index|
[467] Fix | Delete
yield row[col_index], row_index, col_index unless row_index == col_index
[468] Fix | Delete
end
[469] Fix | Delete
end
[470] Fix | Delete
when :lower
[471] Fix | Delete
@rows.each_with_index do |row, row_index|
[472] Fix | Delete
0.upto([row_index, last].min) do |col_index|
[473] Fix | Delete
yield row[col_index], row_index, col_index
[474] Fix | Delete
end
[475] Fix | Delete
end
[476] Fix | Delete
when :strict_lower
[477] Fix | Delete
@rows.each_with_index do |row, row_index|
[478] Fix | Delete
[row_index, column_count].min.times do |col_index|
[479] Fix | Delete
yield row[col_index], row_index, col_index
[480] Fix | Delete
end
[481] Fix | Delete
end
[482] Fix | Delete
when :strict_upper
[483] Fix | Delete
@rows.each_with_index do |row, row_index|
[484] Fix | Delete
(row_index+1).upto(last) do |col_index|
[485] Fix | Delete
yield row[col_index], row_index, col_index
[486] Fix | Delete
end
[487] Fix | Delete
end
[488] Fix | Delete
when :upper
[489] Fix | Delete
@rows.each_with_index do |row, row_index|
[490] Fix | Delete
row_index.upto(last) do |col_index|
[491] Fix | Delete
yield row[col_index], row_index, col_index
[492] Fix | Delete
end
[493] Fix | Delete
end
[494] Fix | Delete
else
[495] Fix | Delete
raise ArgumentError, "expected #{which.inspect} to be one of :all, :diagonal, :off_diagonal, :lower, :strict_lower, :strict_upper or :upper"
[496] Fix | Delete
end
[497] Fix | Delete
self
[498] Fix | Delete
end
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function