Created
July 9, 2010 04:18
-
-
Save bensonk/469039 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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