Golang program to find the cross product of two vectors


The cross product is an operation performed on two vectors in three-dimensional space that results in a third vector that is orthogonal (perpendicular) to the original vectors. In this article, we will see the Golang program to find the cross product of two vectors, here we will explore two different methods.

Explanation

Let us assume we have two vectors A and B. The cross product of the two vectors can be calculated by the formula:

C = A X B.

Components of a cross product vector can be calculated by formula:

Cx =Ay ⋅Bz −Az ⋅By
Cy =Az ⋅Bx −Ax ⋅Bz
Cz =Ax ⋅By −Ay ⋅Bx

The value of A = (1,2,3) and B = (4,5,6), then by using the above formula we can calculate the cross-product vector as shown below:

Cx = (2.6) - (3.5) = -3

Cy = (3.4) - (1.6) = 6

Cz = (1.5) - (2.4) = -3

The cross product, C = (-3,6,-3).

Algorithm

  • Define a function called "CrossProduct" that takes two vectors, 'a' and 'b', as input and returns the cross product as a vector.

  • Create a vector 'bVec' from the values of vector 'b' using the "mat.NewVecDense" function.

  • Create a 3x3 matrix 'crossProductMat' that represents the formula for calculating the cross product. The values in this matrix are derived from the components of vector 'a'.

  • Multiply the matrices 'crossProductMat' and 'bVec' using the "result.MulVec" method. Retrieve the raw data of the result vector using the "result.RawVector().Data" method and return it as the cross product.

  • In the main function:

    • Define two vectors, 'a' and 'b', with their respective components.

    • Call the 'CrossProduct' function with 'a' and 'b' as arguments to calculate the cross product.

    • Print the resulting cross product to the console.

Syntax

func CrossProduct(a, b Vector) Vector

The syntax defines a function named CrossProduct calculates the cross product of two vectors, a and b, in three-dimensional space. It takes two vectors of type Vector as input and returns the resulting cross product vector, also of type Vector.

func CrossProduct(a, b []float64) []float64 {

The syntax defines a function named CrossProduct calculates the cross product of two vectors, a and b, represented as slices of float64 values. It utilizes matrix multiplication to perform the calculation. The function takes two input slices, a and b, and returns a slice representing the resulting cross product. The function signature specifies that it takes two float64 slices, a and b, as input, and returns a float64 slice.

Example 1

In this example, we define a Vector struct with X, Y, and Z components. The CrossProduct function takes two vectors as input and returns the resulting cross product vector. We create two vectors, a and b, and calculate their cross product using the CrossProduct function. In the fuction we will directly use the formula (A × B = (A_y * B_z - A_z * B_y, A_z * B_x - A_x * B_z, A_x * B_y - A_y * B_x)). Finally, we print the result to the console.

package main
import "fmt"
type Vector struct {
   X, Y, Z float64
}
func CrossProduct(a, b Vector) Vector {
   return Vector{
      X: a.Y*b.Z - a.Z*b.Y,
      Y: a.Z*b.X - a.X*b.Z,
      Z: a.X*b.Y - a.Y*b.X,
   }
}
func main() {
   a := Vector{X: 1, Y: 2, Z: 3}
   b := Vector{X: 4, Y: 5, Z: 6}
   crossProduct := CrossProduct(a, b)
   fmt.Println("The CrossProduct vector is :", crossProduct)
}

Output

The CrossProduct vector is : {-3 6 -3}

Example 2

In this example, we will represent the vectors as 3x1 matrices and perform matrix multiplication to obtain the resulting cross product vector, in the Golang program to find the cross product of two vectors, we use the Gonum library (gonum.org/v1/gonum/mat) to work with matrices. We define the CrossProduct function, which takes two slices representing the vectors a and b. We create 3x1 matrices for vectors a and b. and multiply these matrices to obtain the resulting cross product matrix. Finally, we convert the resulting matrix back to a slice of float64 values and print the cross product to the console.

package main
import (
   "fmt"
   "gonum.org/v1/gonum/mat"
) 
func CrossProduct(a, b []float64) []float64 {
   bVec := mat.NewVecDense(3, b)
   crossProductMat := mat.NewDense(3, 3, []float64{
      0, -a[2], a[1],
      a[2], 0, -a[0],
      -a[1], a[0], 0,
})
   result := mat.NewVecDense(3, nil)
   result.MulVec(crossProductMat, bVec)
   return result.RawVector().Data
}
func main() {
   a := []float64{1, 2, 3}
   b := []float64{4, 5, 6}
   crossProduct := CrossProduct(a, b)
   fmt.Println("The Cross Product is :", crossProduct)
}

Output

Cross Product: [-3 6 -3]

Real life implementations

  • Robotics: The use of the cross product is prevalent in the domains of motion planning and control within the field of robotics. The use of the cross product is applied in the calculation of the necessary torque and control signals for the joints when a robotic arm or mechanism is needed to operate within a three-dimensional space. This guarantees that the locomotion of the robot is precise and consistent.

  • Astronomy: The calculation of orbital angular momentum for celestial entities in the field of astronomy involves the use of the cross product. The angular momentum vector of a planet or any other celestial object in orbit around a central body is obtained by taking the cross product of its position vector and its velocity vector. This facilitates comprehension of the mechanics of astronomical orbits and enables the prediction of their trajectory.

Conclusion

The cross product is also known as the vector product as it takes two vectors as the input and produces the output as a vector. In this article, we have looked at two different Golang program to find the cross product of two vectors. The first method involved direct calculation using the cross-product formula, while the second method utilised matrix multiplication. The cross product is often used in various applications, including computer graphics, physics, and engineering. Both methods yield the same result, and the choice of method depends on the specific requirements of your program.

Updated on: 18-Oct-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements