diff options
author | Marc-Andre Lafortune <[email protected]> | 2020-12-04 01:57:40 -0500 |
---|---|---|
committer | Marc-André Lafortune <[email protected]> | 2020-12-05 00:56:58 -0500 |
commit | a83a51932dbc31b549e11b9da8967f2f52a8b07c (patch) | |
tree | 272eea99e5f40150af2a52114d7f8c9d8f325264 /lib/matrix.rb | |
parent | 3b5b309b7b3724849c27dc1c836b5348a8a82e23 (diff) |
[ruby/matrix] Optimize **
Avoiding recursive call would imply iterating bits starting from
most significant, which is not easy to do efficiently.
Any saving would be dwarfed by the multiplications anyways.
[Feature #15233]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3844
Diffstat (limited to 'lib/matrix.rb')
-rw-r--r-- | lib/matrix.rb | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb index 336a92877b..c6193ebee1 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1233,26 +1233,49 @@ class Matrix # # => 67 96 # # 48 99 # - def **(other) - case other + def **(exp) + case exp when Integer - x = self - if other <= 0 - x = self.inverse - return self.class.identity(self.column_count) if other == 0 - other = -other - end - z = nil - loop do - z = z ? z * x : x if other[0] == 1 - return z if (other >>= 1).zero? - x *= x + case + when exp == 0 + _make_sure_it_is_invertible = inverse + self.class.identity(column_count) + when exp < 0 + inverse.power_int(-exp) + else + power_int(exp) end when Numeric v, d, v_inv = eigensystem - v * self.class.diagonal(*d.each(:diagonal).map{|e| e ** other}) * v_inv + v * self.class.diagonal(*d.each(:diagonal).map{|e| e ** exp}) * v_inv + else + raise ErrOperationNotDefined, ["**", self.class, exp.class] + end + end + + protected def power_int(exp) + # assumes `exp` is an Integer > 0 + # + # Previous algorithm: + # build M**2, M**4 = (M**2)**2, M**8, ... and multiplying those you need + # e.g. M**0b1011 = M**11 = M * M**2 * M**8 + # ^ ^ + # (highlighted the 2 out of 5 multiplications involving `M * x`) + # + # Current algorithm has same number of multiplications but with lower exponents: + # M**11 = M * (M * M**4)**2 + # ^ ^ ^ + # (highlighted the 3 out of 5 multiplications involving `M * x`) + # + # This should be faster for all (non nil-potent) matrices. + case + when exp == 1 + self + when exp.odd? + self * power_int(exp - 1) else - raise ErrOperationNotDefined, ["**", self.class, other.class] + sqrt = power_int(exp / 2) + sqrt * sqrt end end |