ruby-gsl Sample: matrix.rb


#!/usr/local/bin/ruby


require "GSL"
include GSL

STDERR.puts "Running tests for Matrix..."

m = Matrix.new 10, 10
0.upto(9) do |i|
  0.upto(9) do |j|
    m.set i, j, Math::sin(i) + Math::cos(j)
  end
end

puts "writing matrix"
f = File.open "matrix.dat", "w"
m.fwrite f
f.close

puts "\nreading matrix"
n = Matrix.new 10, 10
f = File.open "matrix.dat", "r"
n.fread f
f.close

k = 0
0.upto(9) do |i|
  0.upto(9) do |j|
    d1 = m.get i, j
    d2 = n.get i, j
    k += 1 if d1 != d2
  end
end

puts "\ndifferences = #{k} (should be zero)"

0.upto(9) do |j|
  col = m.column j
  d = 0.0
  col.each do |i|
    d += i*i
  end
  printf "matrix column %d, norm = %g\n", j, Math::sqrt(d)
end

puts "\neigenvalues and eigenvectors"
n = Matrix.new [1.0, 1/2.0, 1/3.0, 1/4.0], [1/2.0, 1/3.0, 1/4.0, 1/5.0],
               [1/3.0, 1/4.0, 1/5.0, 1/6.0], [1/4.0, 1/5.0, 1/6.0, 1/7.0]
vals, vecs = n.eigen_symmv
Eigen.symmv_sort vals, vecs, Eigen::SORT_VAL_ASC
vals.to_a.each_index do |i|
  printf "l = %.3f\n", vals.get(i)
  vecs.get_col(i).printf "%.3f"
  puts
end

STDERR.puts "\ndone."


Back