Project

General

Profile

Feature #10445 ยป implement_matrix.rb.patch

gogotanaka (Kazuki Tanaka), 10/29/2014 08:26 AM

View differences:

lib/matrix.rb
49 49
# * Matrix.vstack(*matrices)
50 50
#
51 51
# To access Matrix elements/columns/rows/submatrices/properties:
52
# * #[](i, j)
52
# * #[](row_arg, col_arg)
53 53
# * #row_count (row_size)
54 54
# * #column_count (column_size)
55 55
# * #row(i)
......
366 366
  private :new_matrix
367 367
  #
368
  # Returns element (+i+,+j+) of the matrix.  That is: row +i+, column +j+.
368
  # :call-seq:
369
  #   matrix[row, column]          -> obj        or nil
370
  #   matrix[row, col_range]       -> new_vector or nil
371
  #   matrix[row_range, column]    -> new_vector or nil
372
  #   matrix[row_range, col_range] -> new_matrix or nil
373
  #
374
  #   Matrix.diagonal(9, 5, -3)[1, 1]
375
  #     => 5
376
  #
377
  #   Matrix.diagonal(9, 5, -3)[1, 0..1]
378
  #     => Vector[0, 5]
379
  #
380
  #   Matrix.diagonal(9, 5, -3)[0..1, 0]
381
  #     => Vector[9, 0]
382
  #
383
  #   Matrix.diagonal(9, 5, -3)[0..1, 0..1]
384
  #     => Matrix[[9, 0], [0, 5]]
385
  #
386
  # Like Array#[], negative indices count backward from the end of the
387
  # row or column (-1 is the last element).
369 388
  #
370
  def [](i, j)
371
    @rows.fetch(i){return nil}[j]
389
  def [](row_arg, col_arg)
390
    if row_arg.is_a?(Range)
391
      if col_arg.is_a?(Range)
392
        minor(row_arg, col_arg)
393
      else
394
        return unless col_vector = column(col_arg)
395
        col_vector[row_arg]
396
      end
397
    else
398
      if col_arg.is_a?(Range)
399
        return unless row_vector = row(row_arg)
400
        row_vector[col_arg]
401
      else
402
        @rows.fetch(row_arg){return nil}[col_arg]
403
      end
404
    end
372 405
  end
373 406
  alias element []
374 407
  alias component []
375
-