Bug #1532 » b_matrix_empty.diff
lib/matrix.rb | ||
---|---|---|
rows.each do |row|
|
||
Matrix.Raise ErrDimensionMismatch, "element size differs (#{row.size} should be #{size})" unless row.size == size
|
||
end
|
||
new rows
|
||
new rows, size
|
||
end
|
||
#
|
||
... | ... | |
#
|
||
def Matrix.column_vector(column)
|
||
column = Matrix.convert_to_array(column)
|
||
new [column].transpose
|
||
new [column].transpose, 1
|
||
end
|
||
#
|
||
# Matrix.new is private; use Matrix.rows, columns, [], etc... to create.
|
||
#
|
||
def initialize(rows)
|
||
def initialize(rows, column_size = rows[0].size)
|
||
# No checking is done at this point. rows must be an Array of Arrays.
|
||
# column_size must be the size of the first row, if there is one,
|
||
# otherwise it *must* be specified and can be any integer >= 0
|
||
@rows = rows
|
||
@column_size = column_size
|
||
end
|
||
def new(rows) # :nodoc:
|
||
Matrix.send(:new, rows) # bypass privacy of Matrix.new
|
||
def new(rows, column_size = rows[0].size) # :nodoc:
|
||
Matrix.send(:new, rows, column_size) # bypass privacy of Matrix.new
|
||
end
|
||
private :new
|
||
... | ... | |
#
|
||
# Returns the number of columns.
|
||
#
|
||
def column_size
|
||
@rows[0].size
|
||
end
|
||
attr_reader :column_size
|
||
#
|
||
# Returns row vector number +i+ of the matrix as a Vector (starting at 0 like
|
||
... | ... | |
#
|
||
def collect(&block) # :yield: e
|
||
rows = @rows.collect{|row| row.collect(&block)}
|
||
new(rows)
|
||
new rows, column_size
|
||
end
|
||
alias map collect
|
||
... | ... | |
rows = @rows[from_row, size_row].collect{|row|
|
||
row[from_col, size_col]
|
||
}
|
||
new(rows)
|
||
new rows, column_size - from_col
|
||
end
|
||
#--
|
||
... | ... | |
# There should be no good reason to do this since Matrices are immutable.
|
||
#
|
||
def clone
|
||
new(@rows.map{|row| row.dup})
|
||
new @rows.map{|row| row.dup}, column_size
|
||
end
|
||
#
|
||
... | ... | |
e * m
|
||
}
|
||
}
|
||
return new(rows)
|
||
return new rows, column_size
|
||
when Vector
|
||
m = Matrix.column_vector(m)
|
||
r = self * m
|
||
... | ... | |
end
|
||
}
|
||
}
|
||
return new(rows)
|
||
return new rows, m.column_size
|
||
else
|
||
x, y = m.coerce(self)
|
||
return x * y
|
||
... | ... | |
self[i, j] + m[i, j]
|
||
}
|
||
}
|
||
new(rows)
|
||
new rows, column_size
|
||
end
|
||
#
|
||
... | ... | |
self[i, j] - m[i, j]
|
||
}
|
||
}
|
||
new(rows)
|
||
new rows, column_size
|
||
end
|
||
#
|
||
... | ... | |
e / other
|
||
}
|
||
}
|
||
return new(rows)
|
||
return new rows, column_size
|
||
when Matrix
|
||
return self * other.inverse
|
||
else
|
||
... | ... | |
# 2 4 6
|
||
#
|
||
def transpose
|
||
new @rows.transpose
|
||
new @rows.transpose, row_size
|
||
end
|
||
alias t transpose
|
||
... | ... | |
# Overrides Object#to_s
|
||
#
|
||
def to_s
|
||
"Matrix[" + @rows.collect{|row|
|
||
"[" + row.collect{|e| e.to_s}.join(", ") + "]"
|
||
}.join(", ")+"]"
|
||
end
|
||
#
|
||
# Overrides Object#inspect
|
||
#
|
||
def inspect
|
||
"Matrix"+@rows.inspect
|
||
if row_size == 0 && column_size > 0
|
||
"Matrix.columns(#{Array.new(column_size, [])})"
|
||
else
|
||
"Matrix#{@rows}"
|
||
end
|
||
end
|
||
alias_method :inspect, :to_s
|
||
#
|
||
# Converts the obj to an Array. If copy is set to true
|