Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/share/ruby
File: matrix.rb
[500] Fix | Delete
SELECTORS = {all: true, diagonal: true, off_diagonal: true, lower: true, strict_lower: true, strict_upper: true, upper: true}.freeze
[501] Fix | Delete
#
[502] Fix | Delete
# :call-seq:
[503] Fix | Delete
# index(value, selector = :all) -> [row, column]
[504] Fix | Delete
# index(selector = :all){ block } -> [row, column]
[505] Fix | Delete
# index(selector = :all) -> an_enumerator
[506] Fix | Delete
#
[507] Fix | Delete
# The index method is specialized to return the index as [row, column]
[508] Fix | Delete
# It also accepts an optional +selector+ argument, see #each for details.
[509] Fix | Delete
#
[510] Fix | Delete
# Matrix[ [1,2], [3,4] ].index(&:even?) # => [0, 1]
[511] Fix | Delete
# Matrix[ [1,1], [1,1] ].index(1, :strict_lower) # => [1, 0]
[512] Fix | Delete
#
[513] Fix | Delete
def index(*args)
[514] Fix | Delete
raise ArgumentError, "wrong number of arguments(#{args.size} for 0-2)" if args.size > 2
[515] Fix | Delete
which = (args.size == 2 || SELECTORS.include?(args.last)) ? args.pop : :all
[516] Fix | Delete
return to_enum :find_index, which, *args unless block_given? || args.size == 1
[517] Fix | Delete
if args.size == 1
[518] Fix | Delete
value = args.first
[519] Fix | Delete
each_with_index(which) do |e, row_index, col_index|
[520] Fix | Delete
return row_index, col_index if e == value
[521] Fix | Delete
end
[522] Fix | Delete
else
[523] Fix | Delete
each_with_index(which) do |e, row_index, col_index|
[524] Fix | Delete
return row_index, col_index if yield e
[525] Fix | Delete
end
[526] Fix | Delete
end
[527] Fix | Delete
nil
[528] Fix | Delete
end
[529] Fix | Delete
alias_method :find_index, :index
[530] Fix | Delete
[531] Fix | Delete
#
[532] Fix | Delete
# Returns a section of the matrix. The parameters are either:
[533] Fix | Delete
# * start_row, nrows, start_col, ncols; OR
[534] Fix | Delete
# * row_range, col_range
[535] Fix | Delete
#
[536] Fix | Delete
# Matrix.diagonal(9, 5, -3).minor(0..1, 0..2)
[537] Fix | Delete
# => 9 0 0
[538] Fix | Delete
# 0 5 0
[539] Fix | Delete
#
[540] Fix | Delete
# Like Array#[], negative indices count backward from the end of the
[541] Fix | Delete
# row or column (-1 is the last element). Returns nil if the starting
[542] Fix | Delete
# row or column is greater than row_count or column_count respectively.
[543] Fix | Delete
#
[544] Fix | Delete
def minor(*param)
[545] Fix | Delete
case param.size
[546] Fix | Delete
when 2
[547] Fix | Delete
row_range, col_range = param
[548] Fix | Delete
from_row = row_range.first
[549] Fix | Delete
from_row += row_count if from_row < 0
[550] Fix | Delete
to_row = row_range.end
[551] Fix | Delete
to_row += row_count if to_row < 0
[552] Fix | Delete
to_row += 1 unless row_range.exclude_end?
[553] Fix | Delete
size_row = to_row - from_row
[554] Fix | Delete
[555] Fix | Delete
from_col = col_range.first
[556] Fix | Delete
from_col += column_count if from_col < 0
[557] Fix | Delete
to_col = col_range.end
[558] Fix | Delete
to_col += column_count if to_col < 0
[559] Fix | Delete
to_col += 1 unless col_range.exclude_end?
[560] Fix | Delete
size_col = to_col - from_col
[561] Fix | Delete
when 4
[562] Fix | Delete
from_row, size_row, from_col, size_col = param
[563] Fix | Delete
return nil if size_row < 0 || size_col < 0
[564] Fix | Delete
from_row += row_count if from_row < 0
[565] Fix | Delete
from_col += column_count if from_col < 0
[566] Fix | Delete
else
[567] Fix | Delete
raise ArgumentError, param.inspect
[568] Fix | Delete
end
[569] Fix | Delete
[570] Fix | Delete
return nil if from_row > row_count || from_col > column_count || from_row < 0 || from_col < 0
[571] Fix | Delete
rows = @rows[from_row, size_row].collect{|row|
[572] Fix | Delete
row[from_col, size_col]
[573] Fix | Delete
}
[574] Fix | Delete
new_matrix rows, [column_count - from_col, size_col].min
[575] Fix | Delete
end
[576] Fix | Delete
[577] Fix | Delete
#
[578] Fix | Delete
# Returns the submatrix obtained by deleting the specified row and column.
[579] Fix | Delete
#
[580] Fix | Delete
# Matrix.diagonal(9, 5, -3, 4).first_minor(1, 2)
[581] Fix | Delete
# => 9 0 0
[582] Fix | Delete
# 0 0 0
[583] Fix | Delete
# 0 0 4
[584] Fix | Delete
#
[585] Fix | Delete
def first_minor(row, column)
[586] Fix | Delete
raise RuntimeError, "first_minor of empty matrix is not defined" if empty?
[587] Fix | Delete
[588] Fix | Delete
unless 0 <= row && row < row_count
[589] Fix | Delete
raise ArgumentError, "invalid row (#{row.inspect} for 0..#{row_count - 1})"
[590] Fix | Delete
end
[591] Fix | Delete
[592] Fix | Delete
unless 0 <= column && column < column_count
[593] Fix | Delete
raise ArgumentError, "invalid column (#{column.inspect} for 0..#{column_count - 1})"
[594] Fix | Delete
end
[595] Fix | Delete
[596] Fix | Delete
arrays = to_a
[597] Fix | Delete
arrays.delete_at(row)
[598] Fix | Delete
arrays.each do |array|
[599] Fix | Delete
array.delete_at(column)
[600] Fix | Delete
end
[601] Fix | Delete
[602] Fix | Delete
new_matrix arrays, column_count - 1
[603] Fix | Delete
end
[604] Fix | Delete
[605] Fix | Delete
#
[606] Fix | Delete
# Returns the (row, column) cofactor which is obtained by multiplying
[607] Fix | Delete
# the first minor by (-1)**(row + column).
[608] Fix | Delete
#
[609] Fix | Delete
# Matrix.diagonal(9, 5, -3, 4).cofactor(1, 1)
[610] Fix | Delete
# => -108
[611] Fix | Delete
#
[612] Fix | Delete
def cofactor(row, column)
[613] Fix | Delete
raise RuntimeError, "cofactor of empty matrix is not defined" if empty?
[614] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[615] Fix | Delete
[616] Fix | Delete
det_of_minor = first_minor(row, column).determinant
[617] Fix | Delete
det_of_minor * (-1) ** (row + column)
[618] Fix | Delete
end
[619] Fix | Delete
[620] Fix | Delete
#
[621] Fix | Delete
# Returns the adjugate of the matrix.
[622] Fix | Delete
#
[623] Fix | Delete
# Matrix[ [7,6],[3,9] ].adjugate
[624] Fix | Delete
# => 9 -6
[625] Fix | Delete
# -3 7
[626] Fix | Delete
#
[627] Fix | Delete
def adjugate
[628] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[629] Fix | Delete
Matrix.build(row_count, column_count) do |row, column|
[630] Fix | Delete
cofactor(column, row)
[631] Fix | Delete
end
[632] Fix | Delete
end
[633] Fix | Delete
[634] Fix | Delete
#
[635] Fix | Delete
# Returns the Laplace expansion along given row or column.
[636] Fix | Delete
#
[637] Fix | Delete
# Matrix[[7,6], [3,9]].laplace_expansion(column: 1)
[638] Fix | Delete
# => 45
[639] Fix | Delete
#
[640] Fix | Delete
# Matrix[[Vector[1, 0], Vector[0, 1]], [2, 3]].laplace_expansion(row: 0)
[641] Fix | Delete
# => Vector[3, -2]
[642] Fix | Delete
#
[643] Fix | Delete
#
[644] Fix | Delete
def laplace_expansion(row: nil, column: nil)
[645] Fix | Delete
num = row || column
[646] Fix | Delete
[647] Fix | Delete
if !num || (row && column)
[648] Fix | Delete
raise ArgumentError, "exactly one the row or column arguments must be specified"
[649] Fix | Delete
end
[650] Fix | Delete
[651] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[652] Fix | Delete
raise RuntimeError, "laplace_expansion of empty matrix is not defined" if empty?
[653] Fix | Delete
[654] Fix | Delete
unless 0 <= num && num < row_count
[655] Fix | Delete
raise ArgumentError, "invalid num (#{num.inspect} for 0..#{row_count - 1})"
[656] Fix | Delete
end
[657] Fix | Delete
[658] Fix | Delete
send(row ? :row : :column, num).map.with_index { |e, k|
[659] Fix | Delete
e * cofactor(*(row ? [num, k] : [k,num]))
[660] Fix | Delete
}.inject(:+)
[661] Fix | Delete
end
[662] Fix | Delete
alias_method :cofactor_expansion, :laplace_expansion
[663] Fix | Delete
[664] Fix | Delete
[665] Fix | Delete
#--
[666] Fix | Delete
# TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
[667] Fix | Delete
#++
[668] Fix | Delete
[669] Fix | Delete
#
[670] Fix | Delete
# Returns +true+ if this is a diagonal matrix.
[671] Fix | Delete
# Raises an error if matrix is not square.
[672] Fix | Delete
#
[673] Fix | Delete
def diagonal?
[674] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[675] Fix | Delete
each(:off_diagonal).all?(&:zero?)
[676] Fix | Delete
end
[677] Fix | Delete
[678] Fix | Delete
#
[679] Fix | Delete
# Returns +true+ if this is an empty matrix, i.e. if the number of rows
[680] Fix | Delete
# or the number of columns is 0.
[681] Fix | Delete
#
[682] Fix | Delete
def empty?
[683] Fix | Delete
column_count == 0 || row_count == 0
[684] Fix | Delete
end
[685] Fix | Delete
[686] Fix | Delete
#
[687] Fix | Delete
# Returns +true+ if this is an hermitian matrix.
[688] Fix | Delete
# Raises an error if matrix is not square.
[689] Fix | Delete
#
[690] Fix | Delete
def hermitian?
[691] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[692] Fix | Delete
each_with_index(:upper).all? do |e, row, col|
[693] Fix | Delete
e == rows[col][row].conj
[694] Fix | Delete
end
[695] Fix | Delete
end
[696] Fix | Delete
[697] Fix | Delete
#
[698] Fix | Delete
# Returns +true+ if this is a lower triangular matrix.
[699] Fix | Delete
#
[700] Fix | Delete
def lower_triangular?
[701] Fix | Delete
each(:strict_upper).all?(&:zero?)
[702] Fix | Delete
end
[703] Fix | Delete
[704] Fix | Delete
#
[705] Fix | Delete
# Returns +true+ if this is a normal matrix.
[706] Fix | Delete
# Raises an error if matrix is not square.
[707] Fix | Delete
#
[708] Fix | Delete
def normal?
[709] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[710] Fix | Delete
rows.each_with_index do |row_i, i|
[711] Fix | Delete
rows.each_with_index do |row_j, j|
[712] Fix | Delete
s = 0
[713] Fix | Delete
rows.each_with_index do |row_k, k|
[714] Fix | Delete
s += row_i[k] * row_j[k].conj - row_k[i].conj * row_k[j]
[715] Fix | Delete
end
[716] Fix | Delete
return false unless s == 0
[717] Fix | Delete
end
[718] Fix | Delete
end
[719] Fix | Delete
true
[720] Fix | Delete
end
[721] Fix | Delete
[722] Fix | Delete
#
[723] Fix | Delete
# Returns +true+ if this is an orthogonal matrix
[724] Fix | Delete
# Raises an error if matrix is not square.
[725] Fix | Delete
#
[726] Fix | Delete
def orthogonal?
[727] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[728] Fix | Delete
rows.each_with_index do |row, i|
[729] Fix | Delete
column_count.times do |j|
[730] Fix | Delete
s = 0
[731] Fix | Delete
row_count.times do |k|
[732] Fix | Delete
s += row[k] * rows[k][j]
[733] Fix | Delete
end
[734] Fix | Delete
return false unless s == (i == j ? 1 : 0)
[735] Fix | Delete
end
[736] Fix | Delete
end
[737] Fix | Delete
true
[738] Fix | Delete
end
[739] Fix | Delete
[740] Fix | Delete
#
[741] Fix | Delete
# Returns +true+ if this is a permutation matrix
[742] Fix | Delete
# Raises an error if matrix is not square.
[743] Fix | Delete
#
[744] Fix | Delete
def permutation?
[745] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[746] Fix | Delete
cols = Array.new(column_count)
[747] Fix | Delete
rows.each_with_index do |row, i|
[748] Fix | Delete
found = false
[749] Fix | Delete
row.each_with_index do |e, j|
[750] Fix | Delete
if e == 1
[751] Fix | Delete
return false if found || cols[j]
[752] Fix | Delete
found = cols[j] = true
[753] Fix | Delete
elsif e != 0
[754] Fix | Delete
return false
[755] Fix | Delete
end
[756] Fix | Delete
end
[757] Fix | Delete
return false unless found
[758] Fix | Delete
end
[759] Fix | Delete
true
[760] Fix | Delete
end
[761] Fix | Delete
[762] Fix | Delete
#
[763] Fix | Delete
# Returns +true+ if all entries of the matrix are real.
[764] Fix | Delete
#
[765] Fix | Delete
def real?
[766] Fix | Delete
all?(&:real?)
[767] Fix | Delete
end
[768] Fix | Delete
[769] Fix | Delete
#
[770] Fix | Delete
# Returns +true+ if this is a regular (i.e. non-singular) matrix.
[771] Fix | Delete
#
[772] Fix | Delete
def regular?
[773] Fix | Delete
not singular?
[774] Fix | Delete
end
[775] Fix | Delete
[776] Fix | Delete
#
[777] Fix | Delete
# Returns +true+ if this is a singular matrix.
[778] Fix | Delete
#
[779] Fix | Delete
def singular?
[780] Fix | Delete
determinant == 0
[781] Fix | Delete
end
[782] Fix | Delete
[783] Fix | Delete
#
[784] Fix | Delete
# Returns +true+ if this is a square matrix.
[785] Fix | Delete
#
[786] Fix | Delete
def square?
[787] Fix | Delete
column_count == row_count
[788] Fix | Delete
end
[789] Fix | Delete
[790] Fix | Delete
#
[791] Fix | Delete
# Returns +true+ if this is a symmetric matrix.
[792] Fix | Delete
# Raises an error if matrix is not square.
[793] Fix | Delete
#
[794] Fix | Delete
def symmetric?
[795] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[796] Fix | Delete
each_with_index(:strict_upper) do |e, row, col|
[797] Fix | Delete
return false if e != rows[col][row]
[798] Fix | Delete
end
[799] Fix | Delete
true
[800] Fix | Delete
end
[801] Fix | Delete
[802] Fix | Delete
#
[803] Fix | Delete
# Returns +true+ if this is a unitary matrix
[804] Fix | Delete
# Raises an error if matrix is not square.
[805] Fix | Delete
#
[806] Fix | Delete
def unitary?
[807] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[808] Fix | Delete
rows.each_with_index do |row, i|
[809] Fix | Delete
column_count.times do |j|
[810] Fix | Delete
s = 0
[811] Fix | Delete
row_count.times do |k|
[812] Fix | Delete
s += row[k].conj * rows[k][j]
[813] Fix | Delete
end
[814] Fix | Delete
return false unless s == (i == j ? 1 : 0)
[815] Fix | Delete
end
[816] Fix | Delete
end
[817] Fix | Delete
true
[818] Fix | Delete
end
[819] Fix | Delete
[820] Fix | Delete
#
[821] Fix | Delete
# Returns +true+ if this is an upper triangular matrix.
[822] Fix | Delete
#
[823] Fix | Delete
def upper_triangular?
[824] Fix | Delete
each(:strict_lower).all?(&:zero?)
[825] Fix | Delete
end
[826] Fix | Delete
[827] Fix | Delete
#
[828] Fix | Delete
# Returns +true+ if this is a matrix with only zero elements
[829] Fix | Delete
#
[830] Fix | Delete
def zero?
[831] Fix | Delete
all?(&:zero?)
[832] Fix | Delete
end
[833] Fix | Delete
[834] Fix | Delete
#--
[835] Fix | Delete
# OBJECT METHODS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
[836] Fix | Delete
#++
[837] Fix | Delete
[838] Fix | Delete
#
[839] Fix | Delete
# Returns +true+ if and only if the two matrices contain equal elements.
[840] Fix | Delete
#
[841] Fix | Delete
def ==(other)
[842] Fix | Delete
return false unless Matrix === other &&
[843] Fix | Delete
column_count == other.column_count # necessary for empty matrices
[844] Fix | Delete
rows == other.rows
[845] Fix | Delete
end
[846] Fix | Delete
[847] Fix | Delete
def eql?(other)
[848] Fix | Delete
return false unless Matrix === other &&
[849] Fix | Delete
column_count == other.column_count # necessary for empty matrices
[850] Fix | Delete
rows.eql? other.rows
[851] Fix | Delete
end
[852] Fix | Delete
[853] Fix | Delete
#
[854] Fix | Delete
# Returns a clone of the matrix, so that the contents of each do not reference
[855] Fix | Delete
# identical objects.
[856] Fix | Delete
# There should be no good reason to do this since Matrices are immutable.
[857] Fix | Delete
#
[858] Fix | Delete
def clone
[859] Fix | Delete
new_matrix @rows.map(&:dup), column_count
[860] Fix | Delete
end
[861] Fix | Delete
[862] Fix | Delete
#
[863] Fix | Delete
# Returns a hash-code for the matrix.
[864] Fix | Delete
#
[865] Fix | Delete
def hash
[866] Fix | Delete
@rows.hash
[867] Fix | Delete
end
[868] Fix | Delete
[869] Fix | Delete
#--
[870] Fix | Delete
# ARITHMETIC -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
[871] Fix | Delete
#++
[872] Fix | Delete
[873] Fix | Delete
#
[874] Fix | Delete
# Matrix multiplication.
[875] Fix | Delete
# Matrix[[2,4], [6,8]] * Matrix.identity(2)
[876] Fix | Delete
# => 2 4
[877] Fix | Delete
# 6 8
[878] Fix | Delete
#
[879] Fix | Delete
def *(m) # m is matrix or vector or number
[880] Fix | Delete
case(m)
[881] Fix | Delete
when Numeric
[882] Fix | Delete
rows = @rows.collect {|row|
[883] Fix | Delete
row.collect {|e| e * m }
[884] Fix | Delete
}
[885] Fix | Delete
return new_matrix rows, column_count
[886] Fix | Delete
when Vector
[887] Fix | Delete
m = self.class.column_vector(m)
[888] Fix | Delete
r = self * m
[889] Fix | Delete
return r.column(0)
[890] Fix | Delete
when Matrix
[891] Fix | Delete
Matrix.Raise ErrDimensionMismatch if column_count != m.row_count
[892] Fix | Delete
[893] Fix | Delete
rows = Array.new(row_count) {|i|
[894] Fix | Delete
Array.new(m.column_count) {|j|
[895] Fix | Delete
(0 ... column_count).inject(0) do |vij, k|
[896] Fix | Delete
vij + self[i, k] * m[k, j]
[897] Fix | Delete
end
[898] Fix | Delete
}
[899] Fix | Delete
}
[900] Fix | Delete
return new_matrix rows, m.column_count
[901] Fix | Delete
else
[902] Fix | Delete
return apply_through_coercion(m, __method__)
[903] Fix | Delete
end
[904] Fix | Delete
end
[905] Fix | Delete
[906] Fix | Delete
#
[907] Fix | Delete
# Matrix addition.
[908] Fix | Delete
# Matrix.scalar(2,5) + Matrix[[1,0], [-4,7]]
[909] Fix | Delete
# => 6 0
[910] Fix | Delete
# -4 12
[911] Fix | Delete
#
[912] Fix | Delete
def +(m)
[913] Fix | Delete
case m
[914] Fix | Delete
when Numeric
[915] Fix | Delete
Matrix.Raise ErrOperationNotDefined, "+", self.class, m.class
[916] Fix | Delete
when Vector
[917] Fix | Delete
m = self.class.column_vector(m)
[918] Fix | Delete
when Matrix
[919] Fix | Delete
else
[920] Fix | Delete
return apply_through_coercion(m, __method__)
[921] Fix | Delete
end
[922] Fix | Delete
[923] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count
[924] Fix | Delete
[925] Fix | Delete
rows = Array.new(row_count) {|i|
[926] Fix | Delete
Array.new(column_count) {|j|
[927] Fix | Delete
self[i, j] + m[i, j]
[928] Fix | Delete
}
[929] Fix | Delete
}
[930] Fix | Delete
new_matrix rows, column_count
[931] Fix | Delete
end
[932] Fix | Delete
[933] Fix | Delete
#
[934] Fix | Delete
# Matrix subtraction.
[935] Fix | Delete
# Matrix[[1,5], [4,2]] - Matrix[[9,3], [-4,1]]
[936] Fix | Delete
# => -8 2
[937] Fix | Delete
# 8 1
[938] Fix | Delete
#
[939] Fix | Delete
def -(m)
[940] Fix | Delete
case m
[941] Fix | Delete
when Numeric
[942] Fix | Delete
Matrix.Raise ErrOperationNotDefined, "-", self.class, m.class
[943] Fix | Delete
when Vector
[944] Fix | Delete
m = self.class.column_vector(m)
[945] Fix | Delete
when Matrix
[946] Fix | Delete
else
[947] Fix | Delete
return apply_through_coercion(m, __method__)
[948] Fix | Delete
end
[949] Fix | Delete
[950] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count
[951] Fix | Delete
[952] Fix | Delete
rows = Array.new(row_count) {|i|
[953] Fix | Delete
Array.new(column_count) {|j|
[954] Fix | Delete
self[i, j] - m[i, j]
[955] Fix | Delete
}
[956] Fix | Delete
}
[957] Fix | Delete
new_matrix rows, column_count
[958] Fix | Delete
end
[959] Fix | Delete
[960] Fix | Delete
#
[961] Fix | Delete
# Matrix division (multiplication by the inverse).
[962] Fix | Delete
# Matrix[[7,6], [3,9]] / Matrix[[2,9], [3,1]]
[963] Fix | Delete
# => -7 1
[964] Fix | Delete
# -3 -6
[965] Fix | Delete
#
[966] Fix | Delete
def /(other)
[967] Fix | Delete
case other
[968] Fix | Delete
when Numeric
[969] Fix | Delete
rows = @rows.collect {|row|
[970] Fix | Delete
row.collect {|e| e / other }
[971] Fix | Delete
}
[972] Fix | Delete
return new_matrix rows, column_count
[973] Fix | Delete
when Matrix
[974] Fix | Delete
return self * other.inverse
[975] Fix | Delete
else
[976] Fix | Delete
return apply_through_coercion(other, __method__)
[977] Fix | Delete
end
[978] Fix | Delete
end
[979] Fix | Delete
[980] Fix | Delete
#
[981] Fix | Delete
# Hadamard product
[982] Fix | Delete
# Matrix[[1,2], [3,4]].hadamard_product(Matrix[[1,2], [3,2]])
[983] Fix | Delete
# => 1 4
[984] Fix | Delete
# 9 8
[985] Fix | Delete
#
[986] Fix | Delete
def hadamard_product(m)
[987] Fix | Delete
combine(m){|a, b| a * b}
[988] Fix | Delete
end
[989] Fix | Delete
alias_method :entrywise_product, :hadamard_product
[990] Fix | Delete
[991] Fix | Delete
#
[992] Fix | Delete
# Returns the inverse of the matrix.
[993] Fix | Delete
# Matrix[[-1, -1], [0, -1]].inverse
[994] Fix | Delete
# => -1 1
[995] Fix | Delete
# 0 -1
[996] Fix | Delete
#
[997] Fix | Delete
def inverse
[998] Fix | Delete
Matrix.Raise ErrDimensionMismatch unless square?
[999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function