Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: matrix.rb
#!/usr/local/bin/ruby
[0] Fix | Delete
#--
[1] Fix | Delete
# matrix.rb -
[2] Fix | Delete
# $Release Version: 1.0$
[3] Fix | Delete
# $Revision: 1.11 $
[4] Fix | Delete
# $Date: 1999/10/06 11:01:53 $
[5] Fix | Delete
# Original Version from Smalltalk-80 version
[6] Fix | Delete
# on July 23, 1985 at 8:37:17 am
[7] Fix | Delete
# by Keiju ISHITSUKA
[8] Fix | Delete
#++
[9] Fix | Delete
#
[10] Fix | Delete
# = matrix.rb
[11] Fix | Delete
#
[12] Fix | Delete
# An implementation of Matrix and Vector classes.
[13] Fix | Delete
#
[14] Fix | Delete
# Author:: Keiju ISHITSUKA
[15] Fix | Delete
# Documentation:: Gavin Sinclair (sourced from <i>Ruby in a Nutshell</i> (Matsumoto, O'Reilly))
[16] Fix | Delete
#
[17] Fix | Delete
# See classes Matrix and Vector for documentation.
[18] Fix | Delete
#
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
require "e2mmap.rb"
[22] Fix | Delete
[23] Fix | Delete
module ExceptionForMatrix # :nodoc:
[24] Fix | Delete
extend Exception2MessageMapper
[25] Fix | Delete
def_e2message(TypeError, "wrong argument type %s (expected %s)")
[26] Fix | Delete
def_e2message(ArgumentError, "Wrong # of arguments(%d for %d)")
[27] Fix | Delete
[28] Fix | Delete
def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch")
[29] Fix | Delete
def_exception("ErrNotRegular", "Not Regular Matrix")
[30] Fix | Delete
def_exception("ErrOperationNotDefined", "This operation(%s) can\\'t defined")
[31] Fix | Delete
end
[32] Fix | Delete
[33] Fix | Delete
#
[34] Fix | Delete
# The +Matrix+ class represents a mathematical matrix, and provides methods for creating
[35] Fix | Delete
# special-case matrices (zero, identity, diagonal, singular, vector), operating on them
[36] Fix | Delete
# arithmetically and algebraically, and determining their mathematical properties (trace, rank,
[37] Fix | Delete
# inverse, determinant).
[38] Fix | Delete
#
[39] Fix | Delete
# Note that although matrices should theoretically be rectangular, this is not
[40] Fix | Delete
# enforced by the class.
[41] Fix | Delete
#
[42] Fix | Delete
# Also note that the determinant of integer matrices may be incorrectly calculated unless you
[43] Fix | Delete
# also <tt>require 'mathn'</tt>. This may be fixed in the future.
[44] Fix | Delete
#
[45] Fix | Delete
# == Method Catalogue
[46] Fix | Delete
#
[47] Fix | Delete
# To create a matrix:
[48] Fix | Delete
# * <tt> Matrix[*rows] </tt>
[49] Fix | Delete
# * <tt> Matrix.[](*rows) </tt>
[50] Fix | Delete
# * <tt> Matrix.rows(rows, copy = true) </tt>
[51] Fix | Delete
# * <tt> Matrix.columns(columns) </tt>
[52] Fix | Delete
# * <tt> Matrix.diagonal(*values) </tt>
[53] Fix | Delete
# * <tt> Matrix.scalar(n, value) </tt>
[54] Fix | Delete
# * <tt> Matrix.scalar(n, value) </tt>
[55] Fix | Delete
# * <tt> Matrix.identity(n) </tt>
[56] Fix | Delete
# * <tt> Matrix.unit(n) </tt>
[57] Fix | Delete
# * <tt> Matrix.I(n) </tt>
[58] Fix | Delete
# * <tt> Matrix.zero(n) </tt>
[59] Fix | Delete
# * <tt> Matrix.row_vector(row) </tt>
[60] Fix | Delete
# * <tt> Matrix.column_vector(column) </tt>
[61] Fix | Delete
#
[62] Fix | Delete
# To access Matrix elements/columns/rows/submatrices/properties:
[63] Fix | Delete
# * <tt> [](i, j) </tt>
[64] Fix | Delete
# * <tt> #row_size </tt>
[65] Fix | Delete
# * <tt> #column_size </tt>
[66] Fix | Delete
# * <tt> #row(i) </tt>
[67] Fix | Delete
# * <tt> #column(j) </tt>
[68] Fix | Delete
# * <tt> #collect </tt>
[69] Fix | Delete
# * <tt> #map </tt>
[70] Fix | Delete
# * <tt> #minor(*param) </tt>
[71] Fix | Delete
#
[72] Fix | Delete
# Properties of a matrix:
[73] Fix | Delete
# * <tt> #regular? </tt>
[74] Fix | Delete
# * <tt> #singular? </tt>
[75] Fix | Delete
# * <tt> #square? </tt>
[76] Fix | Delete
#
[77] Fix | Delete
# Matrix arithmetic:
[78] Fix | Delete
# * <tt> *(m) </tt>
[79] Fix | Delete
# * <tt> +(m) </tt>
[80] Fix | Delete
# * <tt> -(m) </tt>
[81] Fix | Delete
# * <tt> #/(m) </tt>
[82] Fix | Delete
# * <tt> #inverse </tt>
[83] Fix | Delete
# * <tt> #inv </tt>
[84] Fix | Delete
# * <tt> ** </tt>
[85] Fix | Delete
#
[86] Fix | Delete
# Matrix functions:
[87] Fix | Delete
# * <tt> #determinant </tt>
[88] Fix | Delete
# * <tt> #det </tt>
[89] Fix | Delete
# * <tt> #rank </tt>
[90] Fix | Delete
# * <tt> #trace </tt>
[91] Fix | Delete
# * <tt> #tr </tt>
[92] Fix | Delete
# * <tt> #transpose </tt>
[93] Fix | Delete
# * <tt> #t </tt>
[94] Fix | Delete
#
[95] Fix | Delete
# Conversion to other data types:
[96] Fix | Delete
# * <tt> #coerce(other) </tt>
[97] Fix | Delete
# * <tt> #row_vectors </tt>
[98] Fix | Delete
# * <tt> #column_vectors </tt>
[99] Fix | Delete
# * <tt> #to_a </tt>
[100] Fix | Delete
#
[101] Fix | Delete
# String representations:
[102] Fix | Delete
# * <tt> #to_s </tt>
[103] Fix | Delete
# * <tt> #inspect </tt>
[104] Fix | Delete
#
[105] Fix | Delete
class Matrix
[106] Fix | Delete
@RCS_ID='-$Id: matrix.rb,v 1.11 1999/10/06 11:01:53 keiju Exp keiju $-'
[107] Fix | Delete
[108] Fix | Delete
# extend Exception2MessageMapper
[109] Fix | Delete
include ExceptionForMatrix
[110] Fix | Delete
[111] Fix | Delete
# instance creations
[112] Fix | Delete
private_class_method :new
[113] Fix | Delete
[114] Fix | Delete
#
[115] Fix | Delete
# Creates a matrix where each argument is a row.
[116] Fix | Delete
# Matrix[ [25, 93], [-1, 66] ]
[117] Fix | Delete
# => 25 93
[118] Fix | Delete
# -1 66
[119] Fix | Delete
#
[120] Fix | Delete
def Matrix.[](*rows)
[121] Fix | Delete
new(:init_rows, rows, false)
[122] Fix | Delete
end
[123] Fix | Delete
[124] Fix | Delete
#
[125] Fix | Delete
# Creates a matrix where +rows+ is an array of arrays, each of which is a row
[126] Fix | Delete
# to the matrix. If the optional argument +copy+ is false, use the given
[127] Fix | Delete
# arrays as the internal structure of the matrix without copying.
[128] Fix | Delete
# Matrix.rows([[25, 93], [-1, 66]])
[129] Fix | Delete
# => 25 93
[130] Fix | Delete
# -1 66
[131] Fix | Delete
def Matrix.rows(rows, copy = true)
[132] Fix | Delete
new(:init_rows, rows, copy)
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
#
[136] Fix | Delete
# Creates a matrix using +columns+ as an array of column vectors.
[137] Fix | Delete
# Matrix.columns([[25, 93], [-1, 66]])
[138] Fix | Delete
# => 25 -1
[139] Fix | Delete
# 93 66
[140] Fix | Delete
#
[141] Fix | Delete
#
[142] Fix | Delete
def Matrix.columns(columns)
[143] Fix | Delete
rows = (0 .. columns[0].size - 1).collect {
[144] Fix | Delete
|i|
[145] Fix | Delete
(0 .. columns.size - 1).collect {
[146] Fix | Delete
|j|
[147] Fix | Delete
columns[j][i]
[148] Fix | Delete
}
[149] Fix | Delete
}
[150] Fix | Delete
Matrix.rows(rows, false)
[151] Fix | Delete
end
[152] Fix | Delete
[153] Fix | Delete
#
[154] Fix | Delete
# Creates a matrix where the diagonal elements are composed of +values+.
[155] Fix | Delete
# Matrix.diagonal(9, 5, -3)
[156] Fix | Delete
# => 9 0 0
[157] Fix | Delete
# 0 5 0
[158] Fix | Delete
# 0 0 -3
[159] Fix | Delete
#
[160] Fix | Delete
def Matrix.diagonal(*values)
[161] Fix | Delete
size = values.size
[162] Fix | Delete
rows = (0 .. size - 1).collect {
[163] Fix | Delete
|j|
[164] Fix | Delete
row = Array.new(size).fill(0, 0, size)
[165] Fix | Delete
row[j] = values[j]
[166] Fix | Delete
row
[167] Fix | Delete
}
[168] Fix | Delete
rows(rows, false)
[169] Fix | Delete
end
[170] Fix | Delete
[171] Fix | Delete
#
[172] Fix | Delete
# Creates an +n+ by +n+ diagonal matrix where each diagonal element is
[173] Fix | Delete
# +value+.
[174] Fix | Delete
# Matrix.scalar(2, 5)
[175] Fix | Delete
# => 5 0
[176] Fix | Delete
# 0 5
[177] Fix | Delete
#
[178] Fix | Delete
def Matrix.scalar(n, value)
[179] Fix | Delete
Matrix.diagonal(*Array.new(n).fill(value, 0, n))
[180] Fix | Delete
end
[181] Fix | Delete
[182] Fix | Delete
#
[183] Fix | Delete
# Creates an +n+ by +n+ identity matrix.
[184] Fix | Delete
# Matrix.identity(2)
[185] Fix | Delete
# => 1 0
[186] Fix | Delete
# 0 1
[187] Fix | Delete
#
[188] Fix | Delete
def Matrix.identity(n)
[189] Fix | Delete
Matrix.scalar(n, 1)
[190] Fix | Delete
end
[191] Fix | Delete
class << Matrix
[192] Fix | Delete
alias unit identity
[193] Fix | Delete
alias I identity
[194] Fix | Delete
end
[195] Fix | Delete
[196] Fix | Delete
#
[197] Fix | Delete
# Creates an +n+ by +n+ zero matrix.
[198] Fix | Delete
# Matrix.zero(2)
[199] Fix | Delete
# => 0 0
[200] Fix | Delete
# 0 0
[201] Fix | Delete
#
[202] Fix | Delete
def Matrix.zero(n)
[203] Fix | Delete
Matrix.scalar(n, 0)
[204] Fix | Delete
end
[205] Fix | Delete
[206] Fix | Delete
#
[207] Fix | Delete
# Creates a single-row matrix where the values of that row are as given in
[208] Fix | Delete
# +row+.
[209] Fix | Delete
# Matrix.row_vector([4,5,6])
[210] Fix | Delete
# => 4 5 6
[211] Fix | Delete
#
[212] Fix | Delete
def Matrix.row_vector(row)
[213] Fix | Delete
case row
[214] Fix | Delete
when Vector
[215] Fix | Delete
Matrix.rows([row.to_a], false)
[216] Fix | Delete
when Array
[217] Fix | Delete
Matrix.rows([row.dup], false)
[218] Fix | Delete
else
[219] Fix | Delete
Matrix.rows([[row]], false)
[220] Fix | Delete
end
[221] Fix | Delete
end
[222] Fix | Delete
[223] Fix | Delete
#
[224] Fix | Delete
# Creates a single-column matrix where the values of that column are as given
[225] Fix | Delete
# in +column+.
[226] Fix | Delete
# Matrix.column_vector([4,5,6])
[227] Fix | Delete
# => 4
[228] Fix | Delete
# 5
[229] Fix | Delete
# 6
[230] Fix | Delete
#
[231] Fix | Delete
def Matrix.column_vector(column)
[232] Fix | Delete
case column
[233] Fix | Delete
when Vector
[234] Fix | Delete
Matrix.columns([column.to_a])
[235] Fix | Delete
when Array
[236] Fix | Delete
Matrix.columns([column])
[237] Fix | Delete
else
[238] Fix | Delete
Matrix.columns([[column]])
[239] Fix | Delete
end
[240] Fix | Delete
end
[241] Fix | Delete
[242] Fix | Delete
#
[243] Fix | Delete
# This method is used by the other methods that create matrices, and is of no
[244] Fix | Delete
# use to general users.
[245] Fix | Delete
#
[246] Fix | Delete
def initialize(init_method, *argv)
[247] Fix | Delete
self.send(init_method, *argv)
[248] Fix | Delete
end
[249] Fix | Delete
[250] Fix | Delete
def init_rows(rows, copy)
[251] Fix | Delete
if copy
[252] Fix | Delete
@rows = rows.collect{|row| row.dup}
[253] Fix | Delete
else
[254] Fix | Delete
@rows = rows
[255] Fix | Delete
end
[256] Fix | Delete
self
[257] Fix | Delete
end
[258] Fix | Delete
private :init_rows
[259] Fix | Delete
[260] Fix | Delete
#
[261] Fix | Delete
# Returns element (+i+,+j+) of the matrix. That is: row +i+, column +j+.
[262] Fix | Delete
#
[263] Fix | Delete
def [](i, j)
[264] Fix | Delete
@rows[i][j]
[265] Fix | Delete
end
[266] Fix | Delete
[267] Fix | Delete
#
[268] Fix | Delete
# Returns the number of rows.
[269] Fix | Delete
#
[270] Fix | Delete
def row_size
[271] Fix | Delete
@rows.size
[272] Fix | Delete
end
[273] Fix | Delete
[274] Fix | Delete
#
[275] Fix | Delete
# Returns the number of columns. Note that it is possible to construct a
[276] Fix | Delete
# matrix with uneven columns (e.g. Matrix[ [1,2,3], [4,5] ]), but this is
[277] Fix | Delete
# mathematically unsound. This method uses the first row to determine the
[278] Fix | Delete
# result.
[279] Fix | Delete
#
[280] Fix | Delete
def column_size
[281] Fix | Delete
@rows[0].size
[282] Fix | Delete
end
[283] Fix | Delete
[284] Fix | Delete
#
[285] Fix | Delete
# Returns row vector number +i+ of the matrix as a Vector (starting at 0 like
[286] Fix | Delete
# an array). When a block is given, the elements of that vector are iterated.
[287] Fix | Delete
#
[288] Fix | Delete
def row(i) # :yield: e
[289] Fix | Delete
if block_given?
[290] Fix | Delete
for e in @rows[i]
[291] Fix | Delete
yield e
[292] Fix | Delete
end
[293] Fix | Delete
else
[294] Fix | Delete
Vector.elements(@rows[i])
[295] Fix | Delete
end
[296] Fix | Delete
end
[297] Fix | Delete
[298] Fix | Delete
#
[299] Fix | Delete
# Returns column vector number +j+ of the matrix as a Vector (starting at 0
[300] Fix | Delete
# like an array). When a block is given, the elements of that vector are
[301] Fix | Delete
# iterated.
[302] Fix | Delete
#
[303] Fix | Delete
def column(j) # :yield: e
[304] Fix | Delete
if block_given?
[305] Fix | Delete
0.upto(row_size - 1) do
[306] Fix | Delete
|i|
[307] Fix | Delete
yield @rows[i][j]
[308] Fix | Delete
end
[309] Fix | Delete
else
[310] Fix | Delete
col = (0 .. row_size - 1).collect {
[311] Fix | Delete
|i|
[312] Fix | Delete
@rows[i][j]
[313] Fix | Delete
}
[314] Fix | Delete
Vector.elements(col, false)
[315] Fix | Delete
end
[316] Fix | Delete
end
[317] Fix | Delete
[318] Fix | Delete
#
[319] Fix | Delete
# Returns a matrix that is the result of iteration of the given block over all
[320] Fix | Delete
# elements of the matrix.
[321] Fix | Delete
# Matrix[ [1,2], [3,4] ].collect { |i| i**2 }
[322] Fix | Delete
# => 1 4
[323] Fix | Delete
# 9 16
[324] Fix | Delete
#
[325] Fix | Delete
def collect # :yield: e
[326] Fix | Delete
rows = @rows.collect{|row| row.collect{|e| yield e}}
[327] Fix | Delete
Matrix.rows(rows, false)
[328] Fix | Delete
end
[329] Fix | Delete
alias map collect
[330] Fix | Delete
[331] Fix | Delete
#
[332] Fix | Delete
# Returns a section of the matrix. The parameters are either:
[333] Fix | Delete
# * start_row, nrows, start_col, ncols; OR
[334] Fix | Delete
# * col_range, row_range
[335] Fix | Delete
#
[336] Fix | Delete
# Matrix.diagonal(9, 5, -3).minor(0..1, 0..2)
[337] Fix | Delete
# => 9 0 0
[338] Fix | Delete
# 0 5 0
[339] Fix | Delete
#
[340] Fix | Delete
def minor(*param)
[341] Fix | Delete
case param.size
[342] Fix | Delete
when 2
[343] Fix | Delete
from_row = param[0].first
[344] Fix | Delete
size_row = param[0].end - from_row
[345] Fix | Delete
size_row += 1 unless param[0].exclude_end?
[346] Fix | Delete
from_col = param[1].first
[347] Fix | Delete
size_col = param[1].end - from_col
[348] Fix | Delete
size_col += 1 unless param[1].exclude_end?
[349] Fix | Delete
when 4
[350] Fix | Delete
from_row = param[0]
[351] Fix | Delete
size_row = param[1]
[352] Fix | Delete
from_col = param[2]
[353] Fix | Delete
size_col = param[3]
[354] Fix | Delete
else
[355] Fix | Delete
Matrix.Raise ArgumentError, param.inspect
[356] Fix | Delete
end
[357] Fix | Delete
[358] Fix | Delete
rows = @rows[from_row, size_row].collect{
[359] Fix | Delete
|row|
[360] Fix | Delete
row[from_col, size_col]
[361] Fix | Delete
}
[362] Fix | Delete
Matrix.rows(rows, false)
[363] Fix | Delete
end
[364] Fix | Delete
[365] Fix | Delete
#--
[366] Fix | Delete
# TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
[367] Fix | Delete
#++
[368] Fix | Delete
[369] Fix | Delete
#
[370] Fix | Delete
# Returns +true+ if this is a regular matrix.
[371] Fix | Delete
#
[372] Fix | Delete
def regular?
[373] Fix | Delete
square? and rank == column_size
[374] Fix | Delete
end
[375] Fix | Delete
[376] Fix | Delete
#
[377] Fix | Delete
# Returns +true+ is this is a singular (i.e. non-regular) matrix.
[378] Fix | Delete
#
[379] Fix | Delete
def singular?
[380] Fix | Delete
not regular?
[381] Fix | Delete
end
[382] Fix | Delete
[383] Fix | Delete
#
[384] Fix | Delete
# Returns +true+ is this is a square matrix. See note in column_size about this
[385] Fix | Delete
# being unreliable, though.
[386] Fix | Delete
#
[387] Fix | Delete
def square?
[388] Fix | Delete
column_size == row_size
[389] Fix | Delete
end
[390] Fix | Delete
[391] Fix | Delete
#--
[392] Fix | Delete
# OBJECT METHODS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
[393] Fix | Delete
#++
[394] Fix | Delete
[395] Fix | Delete
#
[396] Fix | Delete
# Returns +true+ if and only if the two matrices contain equal elements.
[397] Fix | Delete
#
[398] Fix | Delete
def ==(other)
[399] Fix | Delete
return false unless Matrix === other
[400] Fix | Delete
[401] Fix | Delete
other.compare_by_row_vectors(@rows)
[402] Fix | Delete
end
[403] Fix | Delete
alias eql? ==
[404] Fix | Delete
[405] Fix | Delete
#
[406] Fix | Delete
# Not really intended for general consumption.
[407] Fix | Delete
#
[408] Fix | Delete
def compare_by_row_vectors(rows)
[409] Fix | Delete
return false unless @rows.size == rows.size
[410] Fix | Delete
[411] Fix | Delete
0.upto(@rows.size - 1) do
[412] Fix | Delete
|i|
[413] Fix | Delete
return false unless @rows[i] == rows[i]
[414] Fix | Delete
end
[415] Fix | Delete
true
[416] Fix | Delete
end
[417] Fix | Delete
[418] Fix | Delete
#
[419] Fix | Delete
# Returns a clone of the matrix, so that the contents of each do not reference
[420] Fix | Delete
# identical objects.
[421] Fix | Delete
#
[422] Fix | Delete
def clone
[423] Fix | Delete
Matrix.rows(@rows)
[424] Fix | Delete
end
[425] Fix | Delete
[426] Fix | Delete
#
[427] Fix | Delete
# Returns a hash-code for the matrix.
[428] Fix | Delete
#
[429] Fix | Delete
def hash
[430] Fix | Delete
value = 0
[431] Fix | Delete
for row in @rows
[432] Fix | Delete
for e in row
[433] Fix | Delete
value ^= e.hash
[434] Fix | Delete
end
[435] Fix | Delete
end
[436] Fix | Delete
return value
[437] Fix | Delete
end
[438] Fix | Delete
[439] Fix | Delete
#--
[440] Fix | Delete
# ARITHMETIC -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
[441] Fix | Delete
#++
[442] Fix | Delete
[443] Fix | Delete
#
[444] Fix | Delete
# Matrix multiplication.
[445] Fix | Delete
# Matrix[[2,4], [6,8]] * Matrix.identity(2)
[446] Fix | Delete
# => 2 4
[447] Fix | Delete
# 6 8
[448] Fix | Delete
#
[449] Fix | Delete
def *(m) # m is matrix or vector or number
[450] Fix | Delete
case(m)
[451] Fix | Delete
when Numeric
[452] Fix | Delete
rows = @rows.collect {
[453] Fix | Delete
|row|
[454] Fix | Delete
row.collect {
[455] Fix | Delete
|e|
[456] Fix | Delete
e * m
[457] Fix | Delete
}
[458] Fix | Delete
}
[459] Fix | Delete
return Matrix.rows(rows, false)
[460] Fix | Delete
when Vector
[461] Fix | Delete
m = Matrix.column_vector(m)
[462] Fix | Delete
r = self * m
[463] Fix | Delete
return r.column(0)
[464] Fix | Delete
when Matrix
[465] Fix | Delete
Matrix.Raise ErrDimensionMismatch if column_size != m.row_size
[466] Fix | Delete
[467] Fix | Delete
rows = (0 .. row_size - 1).collect {
[468] Fix | Delete
|i|
[469] Fix | Delete
(0 .. m.column_size - 1).collect {
[470] Fix | Delete
|j|
[471] Fix | Delete
vij = 0
[472] Fix | Delete
0.upto(column_size - 1) do
[473] Fix | Delete
|k|
[474] Fix | Delete
vij += self[i, k] * m[k, j]
[475] Fix | Delete
end
[476] Fix | Delete
vij
[477] Fix | Delete
}
[478] Fix | Delete
}
[479] Fix | Delete
return Matrix.rows(rows, false)
[480] Fix | Delete
else
[481] Fix | Delete
x, y = m.coerce(self)
[482] Fix | Delete
return x * y
[483] Fix | Delete
end
[484] Fix | Delete
end
[485] Fix | Delete
[486] Fix | Delete
#
[487] Fix | Delete
# Matrix addition.
[488] Fix | Delete
# Matrix.scalar(2,5) + Matrix[[1,0], [-4,7]]
[489] Fix | Delete
# => 6 0
[490] Fix | Delete
# -4 12
[491] Fix | Delete
#
[492] Fix | Delete
def +(m)
[493] Fix | Delete
case m
[494] Fix | Delete
when Numeric
[495] Fix | Delete
Matrix.Raise ErrOperationNotDefined, "+"
[496] Fix | Delete
when Vector
[497] Fix | Delete
m = Matrix.column_vector(m)
[498] Fix | Delete
when Matrix
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function