Project

General

Profile

Bug #1532 ยป d_matrix_cleanup.diff

marcandre (Marc-Andre Lafortune), 09/17/2009 02:03 PM

View differences:

lib/matrix.rb
377 377
  #
378 378
  def ==(other)
379 379
    return false unless Matrix === other
380

  
381
    other.compare_by_row_vectors(@rows)
380
    rows == other.rows
382 381
  end
382

  
383 383
  def eql?(other)
384 384
    return false unless Matrix === other
385

  
386
    other.compare_by_row_vectors(@rows, :eql?)
387
  end
388

  
389
  #
390
  # Not really intended for general consumption.
391
  #
392
  def compare_by_row_vectors(rows, comparison = :==)
393
    return false unless @rows.size == rows.size
394

  
395
    @rows.size.times do |i|
396
      return false unless @rows[i].send(comparison, rows[i])
397
    end
398
    true
385
    rows.eql? other.rows
399 386
  end
400 387

  
401 388
  #
......
1044 1031
  #INSTANCE CREATION
1045 1032

  
1046 1033
  private_class_method :new
1047

  
1034
  attr_reader :elements
1035
  protected :elements
1048 1036
  #
1049 1037
  # Creates a Vector from a list of elements.
1050 1038
  #   Vector[7, 4, ...]
1051 1039
  #
1052 1040
  def Vector.[](*array)
1053
    new(:init_elements, array, copy = false)
1041
    new Matrix.convert_to_array(array, copy = false)
1054 1042
  end
1055 1043

  
1056 1044
  #
......
1058 1046
  # whether the array itself or a copy is used internally.
1059 1047
  #
1060 1048
  def Vector.elements(array, copy = true)
1061
    new(:init_elements, array, copy)
1049
    new Matrix.convert_to_array(array, copy)
1062 1050
  end
1063 1051

  
1064 1052
  #
1065
  # For internal use.
1053
  # Vector.new is private; use Vector[] or Vector.elements to create.
1066 1054
  #
1067
  def initialize(method, array, copy)
1068
    self.send(method, array, copy)
1069
  end
1070

  
1071
  #
1072
  # For internal use.
1073
  #
1074
  def init_elements(array, copy)
1075
    if copy
1076
      @elements = array.dup
1077
    else
1078
      @elements = array
1079
    end
1055
  def initialize(array)
1056
    # No checking is done at this point.
1057
    @elements = array
1080 1058
  end
1081 1059

  
1082 1060
  # ACCESSING
......
1138 1116
  #
1139 1117
  def ==(other)
1140 1118
    return false unless Vector === other
1141

  
1142
    other.compare_by(@elements)
1119
    @elements == other.elements
1143 1120
  end
1121

  
1144 1122
  def eql?(other)
1145 1123
    return false unless Vector === other
1146

  
1147
    other.compare_by(@elements, :eql?)
1148
  end
1149

  
1150
  #
1151
  # For internal use.
1152
  #
1153
  def compare_by(elements, comparison = :==)
1154
    @elements.send(comparison, elements)
1124
    @elements.eql? other.elements
1155 1125
  end
1156 1126

  
1157 1127
  #