Project

General

Profile

Bug #1532 ยป f_matrix_access.diff

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

View differences:

lib/matrix.rb
246 246
  # Returns element (+i+,+j+) of the matrix.  That is: row +i+, column +j+.
247 247
  #
248 248
  def [](i, j)
249
    @rows[i][j]
249
    @rows.fetch(i){return nil}[j]
250 250
  end
251 251
  alias element []
252 252
  alias component []
......
276 276
  #
277 277
  def row(i, &block) # :yield: e
278 278
    if block_given?
279
      @rows[i].each(&block)
279
      @rows.fetch(i){return self}.each(&block)
280 280
      self
281 281
    else
282
      Vector.elements(@rows[i])
282
      Vector.elements(@rows.fetch(i){return nil})
283 283
    end
284 284
  end
285 285

  
......
292 292
    if block_given?
293 293
      row_size.times do |i|
294 294
        yield @rows[i][j]
295
      end
295
      end unless j >= column_size
296 296
      self
297 297
    else
298
      return nil if j >= column_size
298 299
      col = (0 ... row_size).collect {|i|
299 300
        @rows[i][j]
300 301
      }
......
340 341
      Matrix.Raise ArgumentError, param.inspect
341 342
    end
342 343

  
344
    return nil if from_row > row_size || from_col > column_size
343 345
    rows = @rows[from_row, size_row].collect{|row|
344 346
      row[from_col, size_col]
345 347
    }