Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View vector_normalize.patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
diff --git a/lib/matrix.rb b/lib/matrix.rb
index 3fda08b..d33b3df 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -1479,6 +1479,13 @@ class Vector
end
#
+ # Creates a normalized vector.
+ #
+ def normalize
+ self / self.r
+ end
+
+ #
# Returns the elements of the vector in an array.
#
def to_a
diff --git a/test/matrix/test_vector.rb b/test/matrix/test_vector.rb
index 582509f..821d692 100644
--- a/test/matrix/test_vector.rb
+++ b/test/matrix/test_vector.rb
@@ -131,4 +131,8 @@ class TestVector < Test::Unit::TestCase
assert_equal("Vector[1, 2, 3]", @v1.inspect)
end
+ def test_normalize
+ assert_equal(Vector[0.2672612419124244, 0.5345224838248488, 0.8017837257372732],
+ Vector[1, 2, 3].normalize)
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.