红宝石 |向量归一化()函数
normalize()是 Ruby 中的一个内置方法,它返回一个方向相同但范数等于 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.
示例 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()
输出:
Vector[0.2672612419124244, 0.5345224838248488, 0.8017837257372732]
示例 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()
输出:
Vector[0.5773502691896258, 0.5773502691896258, 0.5773502691896258]