📜  红宝石 |向量归一化()函数

📅  最后修改于: 2022-05-13 01:55:28.607000             🧑  作者: Mango

红宝石 |向量归一化()函数

normalize()是 Ruby 中的一个内置方法,它返回一个方向相同但范数等于 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]