Open In App

Ruby | Vector cross_product() function

The cross_product() is an inbuilt method in Ruby returns cross product of given vectors
 

Syntax: vec1.cross_product(vectors)



Parameters: The function accept vectors as parameters

Return Value: It returns cross product of given vectors 
 



Example 1:  




#Ruby program for cross_product() method in Vector
 
#Include matrix
require "matrix"
 
#Initialize the vector
    vec1
    = Vector[1, 2, 3] vec2 = Vector[2, 1, 4]
 
#Prints the cross product of vectors
                             puts vec1.cross_product(vec2)

Output

Vector[5, 2, -3]

Example 2




#Ruby program for cross_product() method in Vector
 
#Include matrix
require "matrix"
 
#Initialize the vector
    vec1
    = Vector[1, 2, 3, 4] vec2 = Vector[2, 1, 4, 1] vec3 = Vector[2, 4, 5, 7]
 
#Prints the cross product of vectors
                                                          puts vec1.cross_product(vec2, vec3)

Output

Vector[7, -5, -3, 3]

 


Article Tags :