Open In App

Ruby | Vector normalize() function

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




# Ruby program for normalize() method in Vector
     
# Include matrix 
require "matrix"
     
# Initialize the vector
vec1 = Vector[1, 2, 3]
    
# Prints vector with the same direction
puts vec1.normalize()


Output:

Vector[0.2672612419124244, 0.5345224838248488, 0.8017837257372732]

Example 2:




# Ruby program for normalize() method in Vector
     
# Include matrix 
require "matrix"
     
# Initialize the vector
vec1 = Vector[1, 1, 1]
    
# Prints vector with the same direction
puts vec1.normalize()


Output:

Vector[0.5773502691896258, 0.5773502691896258, 0.5773502691896258]

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads