Project

General

Profile

Feature #10073 ยป implement_Matrix#laplace_expansion.patch

gogotanaka (Kazuki Tanaka), 07/19/2014 07:40 AM

View differences:

NEWS
59 59
      by deleting the specified row and column.
60 60
    * Matrix#cofactor(row, column) returns the (row, column) cofactor
61 61
      which is obtained by multiplying the first minor by (-1)**(row + column).
62
    * Matrix#laplace_expansion(num, row_or_column) returns the laplace_expansion
63
      along +num+ -th +row_or_column+.
62 64
    * Matrix#@+ and Matrix#@- .
63 65

  
64 66
* Vector
lib/matrix.rb
61 61
# * #minor(*param)
62 62
# * #first_minor(row, column)
63 63
# * #cofactor(row, column)
64
# * #laplace_expansion(num, row_or_column)
65
# * #cofactor_expansion(num, row_or_column)
66
# * #cofact_exp(num, row_or_column)
64 67
#
65 68
# Properties of a matrix:
66 69
# * #diagonal?
......
639 642
    det_of_minor * (-1) ** (row + column)
640 643
  end
641 644

  
645
  #
646
  # Returns the laplace_expansion along +num+ -th +row_or_column+
647
  #
648
  #    Matrix[[Vector[1, 0], Vector[0, 1]], [2, 3]].laplace_expansion(0, :row)
649
  #     => Vector[3, -2]
650
  #
651
  #    Matrix[[7,6], [3,9]].laplace_expansion(1, :column)
652
  #     => 45
653
  #
654
  def laplace_expansion(num, row_or_column)
655
    Matrix.Raise ErrDimensionMismatch unless square?
656
    raise RuntimeError, "laplace_expansion of empty matrix is not defined" if empty?
657
    raise ArgumentError, "#{row_or_column} should be :row or :column" unless %i(row column).include?(row_or_column)
658
    unless 0 <= num && num < row_count
659
      raise ArgumentError, "invalid num (#{num.inspect} for 0..#{row_count - 1})"
660
    end
661

  
662
    send(row_or_column, num).map.with_index { |e, k|
663
      e * cofactor(*(row_or_column == :row ? [num, k] : [k,num]))
664
    }.inject(:+)
665
  end
666
  alias_method :cofactor_expansion, :laplace_expansion
667
  alias_method :cofact_exp, :laplace_expansion
668

  
669

  
642 670
  #--
643 671
  # TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
644 672
  #++
test/matrix/test_matrix.rb
283 283
    assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { Matrix[[2,0,1],[0,-2,2]].cofactor(0, 0) }
284 284
  end
285 285

  
286
  def test_laplace_expansion
287
    assert_equal(1, Matrix[[1]].laplace_expansion(0, :row))
288
    assert_equal(45, Matrix[[7,6], [3,9]].laplace_expansion(1, :row))
289
    assert_equal(0, Matrix[[0,0],[0,0]].laplace_expansion(0, :column))
290
    assert_equal(-7, Matrix[[0,0,1],[0,7,6],[1,3,9]].laplace_expansion(2, :column))
291

  
292
    assert_equal(Vector[3, -2], Matrix[[Vector[1, 0], Vector[0, 1]], [2, 3]].laplace_expansion(0, :row))
293

  
294
    assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { @m1.laplace_expansion(1, :row) }
295
    assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion(2, :row) }
296
    assert_raise(ArgumentError) { Matrix[[0,0,1],[0,7,6],[1,3,9]].laplace_expansion(-1, :column) }
297
    assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion(1, :foo) }
298
    assert_raise(RuntimeError) { Matrix.empty(0, 0).laplace_expansion(0, :row) }
299
  end
300

  
286 301
  def test_regular?
287 302
    assert(Matrix[[1, 0], [0, 1]].regular?)
288 303
    assert(Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]].regular?)