From de3770d158d8c955e450f1c2359cbc85b7515ebe Mon Sep 17 00:00:00 2001 From: matz Date: Mon, 19 Mar 2007 05:39:07 +0000 Subject: [PATCH] * lib/matrix.rb (Matrix::inverse_from): adding partial pivoting to the Gauss-Jordan algorithm, making it stable. a patch from Peter Vanbroekhoven. [ruby-core:10641] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12096 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ lib/matrix.rb | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1e89cc3a45..3ca7b5ae4e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Mon Mar 19 14:12:25 2007 Yukihiro Matsumoto + + * lib/matrix.rb (Matrix::inverse_from): adding partial pivoting to + the Gauss-Jordan algorithm, making it stable. a patch from + Peter Vanbroekhoven. [ruby-core:10641] + Mon Mar 19 12:13:36 2007 Yukihiro Matsumoto * regparse.c, etc.: K&R to ANSI code cleanup patch from Stefan diff --git a/lib/matrix.rb b/lib/matrix.rb index 8142a52315..d49e16b8fa 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -599,15 +599,21 @@ class Matrix a = src.to_a for k in 0..size - if (akk = a[k][k]) == 0 - i = k - begin - Matrix.Raise ErrNotRegular if (i += 1) > size - end while a[i][k] == 0 + i = k + akk = a[k][k].abs + for j in (k+1)..size + v = a[j][k].abs + if v > akk + i = j + akk = v + end + end + Matrix.Raise ErrNotRegular if akk == 0 + if i != k a[i], a[k] = a[k], a[i] @rows[i], @rows[k] = @rows[k], @rows[i] - akk = a[k][k] end + akk = a[k][k] for i in 0 .. size next if i == k