Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.72 KB | None | 0 0
  1. class Vector
  2.   def norm
  3.     Math.sqrt(inject(0) { |sum, value| sum += value * value; sum })
  4.   end
  5. end
  6.  
  7. a = Matrix[[8.04, 2, 0, 0],
  8.            [2, 8.04, 2, 0],
  9.            [0, 2, 8.04, 2],
  10.            [0, 0, 2, 8.04]]
  11.  
  12. e = Matrix.diagonal(1, 1, 1, 1)
  13.  
  14. x = Vector[1,1,1,1]
  15. lam = 6
  16.  
  17. i = @options[:limit].times do |i|
  18.   temp = a - lam * e
  19.  
  20.   y = temp.inverse * x
  21.  
  22.   x_old = x
  23.  
  24.   x = y / y.norm
  25.  
  26.   lam_old = lam
  27.  
  28.   lam = (temp * x).zip(x).inject(0) { |sum, (a,b)| sum += a*b; sum }
  29.  
  30.   break i if (x - x_old).norm <= @options[:epsilon] or (lam - lam_old).abs <= @options[:epsilon]
  31.  
  32.   x = -1 * x if (x - x_old).norm > 1.99 and (x - x_old).norm < 2.01
  33. end
  34.  
  35. p "Finished in #{i} iterations", x.to_a.flatten, lam
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement