Bug #1532 » d_matrix_cleanup.diff
| lib/matrix.rb | ||
|---|---|---|
|
#
|
||
|
def ==(other)
|
||
|
return false unless Matrix === other
|
||
|
other.compare_by_row_vectors(@rows)
|
||
|
rows == other.rows
|
||
|
end
|
||
|
def eql?(other)
|
||
|
return false unless Matrix === other
|
||
|
other.compare_by_row_vectors(@rows, :eql?)
|
||
|
end
|
||
|
#
|
||
|
# Not really intended for general consumption.
|
||
|
#
|
||
|
def compare_by_row_vectors(rows, comparison = :==)
|
||
|
return false unless @rows.size == rows.size
|
||
|
@rows.size.times do |i|
|
||
|
return false unless @rows[i].send(comparison, rows[i])
|
||
|
end
|
||
|
true
|
||
|
rows.eql? other.rows
|
||
|
end
|
||
|
#
|
||
| ... | ... | |
|
#INSTANCE CREATION
|
||
|
private_class_method :new
|
||
|
attr_reader :elements
|
||
|
protected :elements
|
||
|
#
|
||
|
# Creates a Vector from a list of elements.
|
||
|
# Vector[7, 4, ...]
|
||
|
#
|
||
|
def Vector.[](*array)
|
||
|
new(:init_elements, array, copy = false)
|
||
|
new Matrix.convert_to_array(array, copy = false)
|
||
|
end
|
||
|
#
|
||
| ... | ... | |
|
# whether the array itself or a copy is used internally.
|
||
|
#
|
||
|
def Vector.elements(array, copy = true)
|
||
|
new(:init_elements, array, copy)
|
||
|
new Matrix.convert_to_array(array, copy)
|
||
|
end
|
||
|
#
|
||
|
# For internal use.
|
||
|
# Vector.new is private; use Vector[] or Vector.elements to create.
|
||
|
#
|
||
|
def initialize(method, array, copy)
|
||
|
self.send(method, array, copy)
|
||
|
end
|
||
|
#
|
||
|
# For internal use.
|
||
|
#
|
||
|
def init_elements(array, copy)
|
||
|
if copy
|
||
|
@elements = array.dup
|
||
|
else
|
||
|
@elements = array
|
||
|
end
|
||
|
def initialize(array)
|
||
|
# No checking is done at this point.
|
||
|
@elements = array
|
||
|
end
|
||
|
# ACCESSING
|
||
| ... | ... | |
|
#
|
||
|
def ==(other)
|
||
|
return false unless Vector === other
|
||
|
other.compare_by(@elements)
|
||
|
@elements == other.elements
|
||
|
end
|
||
|
def eql?(other)
|
||
|
return false unless Vector === other
|
||
|
other.compare_by(@elements, :eql?)
|
||
|
end
|
||
|
#
|
||
|
# For internal use.
|
||
|
#
|
||
|
def compare_by(elements, comparison = :==)
|
||
|
@elements.send(comparison, elements)
|
||
|
@elements.eql? other.elements
|
||
|
end
|
||
|
#
|
||