Golang program to find the dot product of two vectors


The dot product is a measure of how closely two vectors are aligning in the direction that they are pointing towards. The dot product of two vectors is a fundamental operation in linear algebra that calculates the sum of the products of corresponding elements in two vectors. In this article, we will write a Golang program to find the dot product of two vectors using a loop as well as using the go languages range keyword.

Explanation

The dot product of two vector is calculated by the formula shown below:

DOT PRODUCT = A⋅B =Ax ⋅Bx +Ay ⋅By +Az ⋅Bz

Let us assume we have two vector A and B, where A = (1,2,3) and B = (4,5,6), then the dot product of two vectors will be calculated by A.B = (1.4 + 2.5 + 3.6) = 32

Algorithm

  • Start by defining a function named dotProduct that takes two vector parameters: vector1 and vector2.

  • Check if the lengths of vector1 and vector2 are equal. If not, raise a panic or handle the error according to your desired approach. The dot product is only defined for vectors of the same length.

  • Initialize a variable named dotProduct to 0. This variable will store the cumulative sum of the products of corresponding elements in the two vectors.

  • Iterate through each index i from 0 to the length of vector1. Inside the loop, calculate the product of vector1[i] and vector2[i], the corresponding elements of the vectors.

  • Add the calculated product to the dotProduct variable. After the loop completes, return the value stored in the dotProduct variable as the result.

Syntax

func dotProduct(vector1 []int, vector2 []int) int

Here the function named "dotProduct," takes two parameters: "vector1" and "vector2," both of type [int]. It gives back a number that is the dot product of the two vectors.

Example 1

In this example, we will do this by using a loop to go through all of the vectors and write a golang program to find the dot product of two vectors. We will make a function called "dotProduct" that takes two vectors as input and gives back the "dot product" of those vectors as a number. First, we check to see if both vectors are the same length. If they aren't, we send a fear message that says the lengths of the vectors must be the same. Then we set up a variable called dotProduct to hold the answer. We go through each part of the vectors using a for loop. At each step, we multiply the parts of the vectors that correspond to each other and add the product to the dotProduct variable.

package main
import (
   "fmt"
)
func dotProduct(vec1 []int, vec2 []int) int {
   if len(vec1) != len(vec2) {
      panic("Vector should have same lenghth")
   }
   dotProduct := 0
   for i := 0; i < len(vec1); i++ {
      dotProduct += vec1[i] * vec2[i]
   }
   return dotProduct
}
 
func main() {
   vec1 := []int{1, 2, 3}
   vec2 := []int{4, 5, 6}
   dotProduct := dotProduct(vec1, vec2)
   fmt.Println("The Dot Product is :", dotProduct)
}

Output

The Dot Product is: 32

Example 2

In this example, we will use the ease of range to go through the elements of one vector while getting to the equivalent elements of the other vector at the same time and write a Golang program to find the dot product of two vectors, we use the range term to go through the elements of vector1 and store the number and value of each element in the i and v variables, respectively. Then, we can uses i to find the matching element in vector2 and multiply them together. The value is put to a property called "dotProduct.".

package main
import (
   "fmt"
)
func dotProduct(vector1 []int, vector2 []int) int {
   if len(vector1) != len(vector2) {  
      panic("Vectors should have same length")
   }
   dotProduct := 0
   for i, v := range vector1 {
      dotProduct += v * vector2[i]
   } 
   return dotProduct
}
func main() {
   vector1 := []int{1, 2, 3}
   vector2 := []int{4, 5, 6}
   dotProduct := dotProduct(vector1, vector2)
   fmt.Println("The Dot Product of two vector is :", dotProduct)
}

Output

The Dot Product of two vector is: 32

Real life implementation

  • Computer Graphics: One of the implementations of a dot product is in determining the shading of a surface. It is used to calculate the intensity of light hitting on a point; thus, it helps in creating realistic shadow effects in 3-dimensional graphics.

  • Physics: We all have heard of work and energy, so to calculate the work done by the force, is calculated by the dot product of force and distance. W = F. d, where F is force and d is distance.

Conclusion

A dot product is also called the scalar product of two vectors, the dot product of two vectors provides a scalar value. In this article, we have looked at how to write a Golang program to find the dot product of two vectors using two distinct methods. The first method uses a loop for precise control, while the second leverages the range keyword for concise and readable code. These techniques are simple to understand.

Updated on: 18-Oct-2023

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements