diff options
author | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-10-24 20:55:40 +0000 |
---|---|---|
committer | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-10-24 20:55:40 +0000 |
commit | c038e9003ce249d4f1346fef5b21883978a780ab (patch) | |
tree | 39c97af077c709d5ba2fac8cdd36ff3c8fd14d7a /lib | |
parent | 55bf7f9d40bae8671476b576a2c8e98bed27b1a6 (diff) | |
download | ruby-c038e9003ce249d4f1346fef5b21883978a780ab.tar.gz |
* lib/matrix.rb (**): Optimization (up to 45% faster)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25457 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/matrix.rb | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb index b577ef3118..3c75751e37 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -642,18 +642,12 @@ class Matrix return Matrix.identity(self.column_size) if other == 0 other = -other end - z = x - n = other - 1 - while n != 0 - while (div, mod = n.divmod(2) - mod == 0) - x = x * x - n = div - end - z *= x - n -= 1 + z = nil + loop do + z = z ? z * x : x if other[0] == 1 + return z if (other >>= 1).zero? + x *= x end - z elsif other.kind_of?(Float) || defined?(Rational) && other.kind_of?(Rational) Matrix.Raise ErrOperationNotDefined, "**" else |