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