* lib/matrix.rb: Allow non integer exponents for Matrix#**

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32352 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2011-07-01 06:21:24 +00:00
Родитель 004c34f9de
Коммит 59a3d59496
3 изменённых файлов: 11 добавлений и 3 удалений

Просмотреть файл

@ -1,3 +1,7 @@
Fri Jul 1 15:21:14 2011 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/matrix.rb: Allow non integer exponents for Matrix#**
Fri Jul 1 15:13:25 2011 Marc-Andre Lafortune <ruby-core@marc-andre.ca> Fri Jul 1 15:13:25 2011 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/matrix: Add Eigenvalue Decomposition * lib/matrix: Add Eigenvalue Decomposition

1
NEWS
Просмотреть файл

@ -178,6 +178,7 @@ with all sufficient information, see the ChangeLog file.
* Matrix#each and #each_with_index can iterate on a subset of the elements * Matrix#each and #each_with_index can iterate on a subset of the elements
* Matrix#find_index returns [row, column] and can iterate on a subset * Matrix#find_index returns [row, column] and can iterate on a subset
of the elements of the elements
* Matrix#** implements Numeric exponents (using the eigensystem)
* Matrix.zero can build rectangular matrices * Matrix.zero can build rectangular matrices
* net/http * net/http

Просмотреть файл

@ -956,8 +956,10 @@ class Matrix
private :inverse_from private :inverse_from
# #
# Matrix exponentiation. Currently implemented for integer powers only. # Matrix exponentiation.
# Equivalent to multiplying the matrix by itself N times. # Equivalent to multiplying the matrix by itself N times.
# Non integer exponents will be handled by diagonalizing the matrix.
#
# Matrix[[7,6], [3,9]] ** 2 # Matrix[[7,6], [3,9]] ** 2
# => 67 96 # => 67 96
# 48 99 # 48 99
@ -977,8 +979,9 @@ class Matrix
return z if (other >>= 1).zero? return z if (other >>= 1).zero?
x *= x x *= x
end end
when Float, Rational when Numeric
Matrix.Raise ErrOperationNotImplemented, "**", self.class, other.class v, d, v_inv = eigensystem
v * Matrix.diagonal(*d.each(:diagonal).map{|e| e ** other}) * v_inv
else else
Matrix.Raise ErrOperationNotDefined, "**", self.class, other.class Matrix.Raise ErrOperationNotDefined, "**", self.class, other.class
end end