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