Ruby | Vector normalize() function
The normalize() is an inbuilt method in Ruby returns a new vector with the same direction but with norm equals to 1.
Syntax: vec1.normalize()
Parameters: The function accepts no parameter
Return Value: It returns a new vector with the same direction but with norm equals to 1.
Example 1:
require "matrix"
vec1 = Vector[ 1 , 2 , 3 ]
puts vec1.normalize()
|
Output:
Vector[0.2672612419124244, 0.5345224838248488, 0.8017837257372732]
Example 2:
require "matrix"
vec1 = Vector[ 1 , 1 , 1 ]
puts vec1.normalize()
|
Output:
Vector[0.5773502691896258, 0.5773502691896258, 0.5773502691896258]
Last Updated :
07 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...