diff options
author | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-09-16 21:19:40 +0000 |
---|---|---|
committer | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-09-16 21:19:40 +0000 |
commit | b9c7e6b04990d2fdc48d096843a3aaef3af535e7 (patch) | |
tree | e1c903e2f4b57bb9e689e9d4819db69001e47cb6 /lib | |
parent | 684115fa9cc610f4ce5ae9a5c9a4c424161ccfd3 (diff) |
* lib/matrix.rb: Optimizations
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@24970 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/matrix.rb | 60 |
1 files changed, 21 insertions, 39 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb index 22fa170ba5..a86440dbba 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -316,8 +316,8 @@ class Matrix # => 1 4 # 9 16 # - def collect # :yield: e - rows = @rows.collect{|row| row.collect{|e| yield e}} + def collect(&block) # :yield: e + rows = @rows.collect{|row| row.collect(&block)} Matrix.rows(rows, false) end alias map collect @@ -454,11 +454,9 @@ class Matrix rows = (0 ... row_size).collect {|i| (0 ... m.column_size).collect {|j| - vij = 0 - column_size.times do |k| - vij += self[i, k] * m[k, j] + (0 ... column_size).inject(0) do |vij, k| + vij + self[i, k] * m[k, j] end - vij } } return Matrix.rows(rows, false) @@ -662,10 +660,10 @@ class Matrix det = 1 size.times do |k| if (akk = a[k][k]) == 0 - i = k - begin - return 0 if (i += 1) >= size - end while a[i][k] == 0 + i = (k+1 ... size).find {|i| + a[i][k] != 0 + } + return 0 if i.nil? a[i], a[k] = a[k], a[i] akk = a[k][k] det *= -1 @@ -703,35 +701,21 @@ class Matrix rank = 0 a_column_size.times do |k| if (akk = a[k][k]) == 0 - i = k - exists = true - loop do - if (i += 1) >= a_column_size - exists = false - break - end - break unless a[i][k] == 0 - end - if exists + i = (k+1 ... a_column_size).find {|i| + a[i][k] != 0 + } + if i a[i], a[k] = a[k], a[i] akk = a[k][k] else - i = k - exists = true - begin - if (i += 1) >= a_row_size - exists = false - break - end - end while a[k][i] == 0 - if exists - (k ... a_column_size).each do |j| - a[j][k], a[j][i] = a[j][i], a[j][k] - end - akk = a[k][k] - else - next + i = (k+1 ... a_row_size).find {|i| + a[k][i] != 0 + } + next if i.nil? + (k ... a_column_size).each do |j| + a[j][k], a[j][i] = a[j][i], a[j][k] end + akk = a[k][k] end end @@ -752,11 +736,9 @@ class Matrix # => 16 # def trace - tr = 0 - column_size.times do |i| - tr += @rows[i][i] + (0...column_size).inject(0) do |tr, i| + tr + @rows[i][i] end - tr end alias tr trace |