Skip to content

Instantly share code, notes, and snippets.

@bensonk
Created July 9, 2010 04:18
Show Gist options
  • Save bensonk/469039 to your computer and use it in GitHub Desktop.
Save bensonk/469039 to your computer and use it in GitHub Desktop.
class Vector
def normalize
sum_of_squares = to_a.inject(0) { |s, x| s + x*x }
if sum_of_squares == 0
self
else
self * (1 / Math.sqrt(sum_of_squares))
end
end
end
class Matrix
def normalize
normalized = row_vectors.collect do |row|
row.normalize
end
return Matrix.rows(normalized)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment