Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vector
- def norm
- Math.sqrt(inject(0) { |sum, value| sum += value * value; sum })
- end
- end
- a = Matrix[[8.04, 2, 0, 0],
- [2, 8.04, 2, 0],
- [0, 2, 8.04, 2],
- [0, 0, 2, 8.04]]
- e = Matrix.diagonal(1, 1, 1, 1)
- x = Vector[1,1,1,1]
- lam = 6
- i = @options[:limit].times do |i|
- temp = a - lam * e
- y = temp.inverse * x
- x_old = x
- x = y / y.norm
- lam_old = lam
- lam = (temp * x).zip(x).inject(0) { |sum, (a,b)| sum += a*b; sum }
- break i if (x - x_old).norm <= @options[:epsilon] or (lam - lam_old).abs <= @options[:epsilon]
- x = -1 * x if (x - x_old).norm > 1.99 and (x - x_old).norm < 2.01
- end
- p "Finished in #{i} iterations", x.to_a.flatten, lam
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement