bm_hilbert_matrix.rb
| 1 |
# Submitted by M. Edward (Ed) Borasky
|
|---|---|
| 2 |
require 'mathn' # also brings in matrix, rational and complex |
| 3 |
|
| 4 |
# return a Hilbert matrix of given dimension
|
| 5 |
def hilbert(dimension) |
| 6 |
rows = Array.new
|
| 7 |
(1..dimension).each do |i| |
| 8 |
row = Array.new
|
| 9 |
(1..dimension).each do |j| |
| 10 |
row.push(1/(i + j - 1)) |
| 11 |
end
|
| 12 |
rows.push(row) |
| 13 |
end
|
| 14 |
return(Matrix.rows(rows)) |
| 15 |
end
|
| 16 |
|
| 17 |
|
| 18 |
def run_hilbert(dimension) |
| 19 |
m = hilbert(dimension) |
| 20 |
print "Hilbert matrix of dimension #{dimension} times its inverse = identity? "
|
| 21 |
k = m * m.inv |
| 22 |
print "#{k==Matrix.I(dimension)}\n"
|
| 23 |
m = nil # for the garbage collector |
| 24 |
k = nil
|
| 25 |
end
|
| 26 |
|
| 27 |
a = [] |
| 28 |
50_000_000.times { a << 1} |
| 29 |
[10, 20].each do |n| |
| 30 |
run_hilbert(n) |
| 31 |
end
|