Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby27/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
#
[278] Fix | Delete
# Create a matrix by combining matrices entrywise, using the given block
[279] Fix | Delete
#
[280] Fix | Delete
# x = Matrix[[6, 6], [4, 4]]
[281] Fix | Delete
# y = Matrix[[1, 2], [3, 4]]
[282] Fix | Delete
# Matrix.combine(x, y) {|a, b| a - b} # => Matrix[[5, 4], [1, 0]]
[283] Fix | Delete
#
[284] Fix | Delete
def Matrix.combine(*matrices)
[285] Fix | Delete
return to_enum(__method__, *matrices) unless block_given?
[286] Fix | Delete
[287] Fix | Delete
return Matrix.empty if matrices.empty?
[288] Fix | Delete
matrices.map!(&CoercionHelper.method(:coerce_to_matrix))
[289] Fix | Delete
x = matrices.first
[290] Fix | Delete
matrices.each do |m|
[291] Fix | Delete
raise ErrDimensionMismatch unless x.row_count == m.row_count && x.column_count == m.column_count
[292] Fix | Delete
end
[293] Fix | Delete
[294] Fix | Delete
rows = Array.new(x.row_count) do |i|
[295] Fix | Delete
Array.new(x.column_count) do |j|
[296] Fix | Delete
yield matrices.map{|m| m[i,j]}
[297] Fix | Delete
end
[298] Fix | Delete
end
[299] Fix | Delete
new rows, x.column_count
[300] Fix | Delete
end
[301] Fix | Delete
[302] Fix | Delete
def combine(*matrices, &block)
[303] Fix | Delete
Matrix.combine(self, *matrices, &block)
[304] Fix | Delete
end
[305] Fix | Delete
[306] Fix | Delete
#
[307] Fix | Delete
# Matrix.new is private; use Matrix.rows, columns, [], etc... to create.
[308] Fix | Delete
#
[309] Fix | Delete
def initialize(rows, column_count = rows[0].size)
[310] Fix | Delete
# No checking is done at this point. rows must be an Array of Arrays.
[311] Fix | Delete
# column_count must be the size of the first row, if there is one,
[312] Fix | Delete
# otherwise it *must* be specified and can be any integer >= 0
[313] Fix | Delete
@rows = rows
[314] Fix | Delete
@column_count = column_count
[315] Fix | Delete
end
[316] Fix | Delete
[317] Fix | Delete
private def new_matrix(rows, column_count = rows[0].size) # :nodoc:
[318] Fix | Delete
self.class.send(:new, rows, column_count) # bypass privacy of Matrix.new
[319] Fix | Delete
end
[320] Fix | Delete
[321] Fix | Delete
#
[322] Fix | Delete
# Returns element (+i+,+j+) of the matrix. That is: row +i+, column +j+.
[323] Fix | Delete
#
[324] Fix | Delete
def [](i, j)
[325] Fix | Delete
@rows.fetch(i){return nil}[j]
[326] Fix | Delete
end
[327] Fix | Delete
alias element []
[328] Fix | Delete
alias component []
[329] Fix | Delete
[330] Fix | Delete
#
[331] Fix | Delete
# :call-seq:
[332] Fix | Delete
# matrix[range, range] = matrix/element
[333] Fix | Delete
# matrix[range, integer] = vector/column_matrix/element
[334] Fix | Delete
# matrix[integer, range] = vector/row_matrix/element
[335] Fix | Delete
# matrix[integer, integer] = element
[336] Fix | Delete
#
[337] Fix | Delete
# Set element or elements of matrix.
[338] Fix | Delete
def []=(i, j, v)
[339] Fix | Delete
raise FrozenError, "can't modify frozen Matrix" if frozen?
[340] Fix | Delete
rows = check_range(i, :row) or row = check_int(i, :row)
[341] Fix | Delete
columns = check_range(j, :column) or column = check_int(j, :column)
[342] Fix | Delete
if rows && columns
[343] Fix | Delete
set_row_and_col_range(rows, columns, v)
[344] Fix | Delete
elsif rows
[345] Fix | Delete
set_row_range(rows, column, v)
[346] Fix | Delete
elsif columns
[347] Fix | Delete
set_col_range(row, columns, v)
[348] Fix | Delete
else
[349] Fix | Delete
set_value(row, column, v)
[350] Fix | Delete
end
[351] Fix | Delete
end
[352] Fix | Delete
alias set_element []=
[353] Fix | Delete
alias set_component []=
[354] Fix | Delete
private :set_element, :set_component
[355] Fix | Delete
[356] Fix | Delete
# Returns range or nil
[357] Fix | Delete
private def check_range(val, direction)
[358] Fix | Delete
return unless val.is_a?(Range)
[359] Fix | Delete
count = direction == :row ? row_count : column_count
[360] Fix | Delete
CoercionHelper.check_range(val, count, direction)
[361] Fix | Delete
end
[362] Fix | Delete
[363] Fix | Delete
private def check_int(val, direction)
[364] Fix | Delete
count = direction == :row ? row_count : column_count
[365] Fix | Delete
CoercionHelper.check_int(val, count, direction)
[366] Fix | Delete
end
[367] Fix | Delete
[368] Fix | Delete
private def set_value(row, col, value)
[369] Fix | Delete
raise ErrDimensionMismatch, "Expected a a value, got a #{value.class}" if value.respond_to?(:to_matrix)
[370] Fix | Delete
[371] Fix | Delete
@rows[row][col] = value
[372] Fix | Delete
end
[373] Fix | Delete
[374] Fix | Delete
private def set_row_and_col_range(row_range, col_range, value)
[375] Fix | Delete
if value.is_a?(Matrix)
[376] Fix | Delete
if row_range.size != value.row_count || col_range.size != value.column_count
[377] Fix | Delete
raise ErrDimensionMismatch, [
[378] Fix | Delete
'Expected a Matrix of dimensions',
[379] Fix | Delete
"#{row_range.size}x#{col_range.size}",
[380] Fix | Delete
'got',
[381] Fix | Delete
"#{value.row_count}x#{value.column_count}",
[382] Fix | Delete
].join(' ')
[383] Fix | Delete
end
[384] Fix | Delete
source = value.instance_variable_get :@rows
[385] Fix | Delete
row_range.each_with_index do |row, i|
[386] Fix | Delete
@rows[row][col_range] = source[i]
[387] Fix | Delete
end
[388] Fix | Delete
elsif value.is_a?(Vector)
[389] Fix | Delete
raise ErrDimensionMismatch, 'Expected a Matrix or a value, got a Vector'
[390] Fix | Delete
else
[391] Fix | Delete
value_to_set = Array.new(col_range.size, value)
[392] Fix | Delete
row_range.each do |i|
[393] Fix | Delete
@rows[i][col_range] = value_to_set
[394] Fix | Delete
end
[395] Fix | Delete
end
[396] Fix | Delete
end
[397] Fix | Delete
[398] Fix | Delete
private def set_row_range(row_range, col, value)
[399] Fix | Delete
if value.is_a?(Vector)
[400] Fix | Delete
raise ErrDimensionMismatch unless row_range.size == value.size
[401] Fix | Delete
set_column_vector(row_range, col, value)
[402] Fix | Delete
elsif value.is_a?(Matrix)
[403] Fix | Delete
raise ErrDimensionMismatch unless value.column_count == 1
[404] Fix | Delete
value = value.column(0)
[405] Fix | Delete
raise ErrDimensionMismatch unless row_range.size == value.size
[406] Fix | Delete
set_column_vector(row_range, col, value)
[407] Fix | Delete
else
[408] Fix | Delete
@rows[row_range].each{|e| e[col] = value }
[409] Fix | Delete
end
[410] Fix | Delete
end
[411] Fix | Delete
[412] Fix | Delete
private def set_column_vector(row_range, col, value)
[413] Fix | Delete
value.each_with_index do |e, index|
[414] Fix | Delete
r = row_range.begin + index
[415] Fix | Delete
@rows[r][col] = e
[416] Fix | Delete
end
[417] Fix | Delete
end
[418] Fix | Delete
[419] Fix | Delete
private def set_col_range(row, col_range, value)
[420] Fix | Delete
value = if value.is_a?(Vector)
[421] Fix | Delete
value.to_a
[422] Fix | Delete
elsif value.is_a?(Matrix)
[423] Fix | Delete
raise ErrDimensionMismatch unless value.row_count == 1
[424] Fix | Delete
value.row(0).to_a
[425] Fix | Delete
else
[426] Fix | Delete
Array.new(col_range.size, value)
[427] Fix | Delete
end
[428] Fix | Delete
raise ErrDimensionMismatch unless col_range.size == value.size
[429] Fix | Delete
@rows[row][col_range] = value
[430] Fix | Delete
end
[431] Fix | Delete
[432] Fix | Delete
#
[433] Fix | Delete
# Returns the number of rows.
[434] Fix | Delete
#
[435] Fix | Delete
def row_count
[436] Fix | Delete
@rows.size
[437] Fix | Delete
end
[438] Fix | Delete
[439] Fix | Delete
alias_method :row_size, :row_count
[440] Fix | Delete
#
[441] Fix | Delete
# Returns the number of columns.
[442] Fix | Delete
#
[443] Fix | Delete
attr_reader :column_count
[444] Fix | Delete
alias_method :column_size, :column_count
[445] Fix | Delete
[446] Fix | Delete
#
[447] Fix | Delete
# Returns row vector number +i+ of the matrix as a Vector (starting at 0 like
[448] Fix | Delete
# an array). When a block is given, the elements of that vector are iterated.
[449] Fix | Delete
#
[450] Fix | Delete
def row(i, &block) # :yield: e
[451] Fix | Delete
if block_given?
[452] Fix | Delete
@rows.fetch(i){return self}.each(&block)
[453] Fix | Delete
self
[454] Fix | Delete
else
[455] Fix | Delete
Vector.elements(@rows.fetch(i){return nil})
[456] Fix | Delete
end
[457] Fix | Delete
end
[458] Fix | Delete
[459] Fix | Delete
#
[460] Fix | Delete
# Returns column vector number +j+ of the matrix as a Vector (starting at 0
[461] Fix | Delete
# like an array). When a block is given, the elements of that vector are
[462] Fix | Delete
# iterated.
[463] Fix | Delete
#
[464] Fix | Delete
def column(j) # :yield: e
[465] Fix | Delete
if block_given?
[466] Fix | Delete
return self if j >= column_count || j < -column_count
[467] Fix | Delete
row_count.times do |i|
[468] Fix | Delete
yield @rows[i][j]
[469] Fix | Delete
end
[470] Fix | Delete
self
[471] Fix | Delete
else
[472] Fix | Delete
return nil if j >= column_count || j < -column_count
[473] Fix | Delete
col = Array.new(row_count) {|i|
[474] Fix | Delete
@rows[i][j]
[475] Fix | Delete
}
[476] Fix | Delete
Vector.elements(col, false)
[477] Fix | Delete
end
[478] Fix | Delete
end
[479] Fix | Delete
[480] Fix | Delete
#
[481] Fix | Delete
# Returns a matrix that is the result of iteration of the given block over all
[482] Fix | Delete
# elements of the matrix.
[483] Fix | Delete
# Elements can be restricted by passing an argument:
[484] Fix | Delete
# * :all (default): yields all elements
[485] Fix | Delete
# * :diagonal: yields only elements on the diagonal
[486] Fix | Delete
# * :off_diagonal: yields all elements except on the diagonal
[487] Fix | Delete
# * :lower: yields only elements on or below the diagonal
[488] Fix | Delete
# * :strict_lower: yields only elements below the diagonal
[489] Fix | Delete
# * :strict_upper: yields only elements above the diagonal
[490] Fix | Delete
# * :upper: yields only elements on or above the diagonal
[491] Fix | Delete
# Matrix[ [1,2], [3,4] ].collect { |e| e**2 }
[492] Fix | Delete
# => 1 4
[493] Fix | Delete
# 9 16
[494] Fix | Delete
#
[495] Fix | Delete
def collect(which = :all, &block) # :yield: e
[496] Fix | Delete
return to_enum(:collect, which) unless block_given?
[497] Fix | Delete
dup.collect!(which, &block)
[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