2009-06-09 32 views

回答

1

嘗試使用「矩陣」庫:

http://www.ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html 
3

數值上更穩定的可能性比直接反轉是使用與包Cholesky decomposition你找到here

require 'Cholesky.rb' 
require 'pp' 
# m is the covariance matrix you want to invert (it is positive semidefinite) 
l = m.cholesky 
li = l.inverse 
lit = li.transpose 
# lit*li is approximately the inverse and the next line shows this 
pp lit*li*m 
比反相升更好

是使用上面鏈接的維基百科文章中描述的方法。

如果你的問題在數值上太不穩定,那麼考慮Singular Value Decomposition,但我沒有它的代碼。

3

如果可以編譯代碼,使用紅寶石-GSL

gem install gsl

逆可以使用LU模塊

inverse=GSL::Linalg::LU.invert(matrix)

0

NMatrix來獲得。支持各種操作,包括來自BLAS和LAPACK的一些操作(通過使用ATLAS)。