The dot product of the unit vectors is 0 when calculated between different unit vectors, because they are orthogonal to each other.
Dot product of two vectors in C++
Key takeaways:
The dot product of two vectors results in a scalar and is used to understand the relationship between vectors, such as their angle or magnitude.
To calculate the dot product, multiply the corresponding components of the vectors and add the products together. For example, use the following formula to calculate the dot product of two vectors:
. The dot product can also be expressed as
, where is the angle between the vectors. In C++, the dot product can be computed using the
inner_product()function or an iterative approach that multiplies and sums the corresponding components of the vectors.The dot product is used in fields like physics, computer graphics, and machine learning for tasks such as vector projections, lighting calculations, and similarity measurements.
What is the dot product?
The dot product, also known as the inner product or scalar product, is a mathematical operation in which two vectors result in a scalar (a single numeric value). The dot product is defined for vectors in both 2D and 3D to understand and deal with vector behaviors. It is used in various fields, including physics, mathematics, and computer graphics.
Let's discuss the formulas to calculate the dot product of two vectors.
Algebraic formula to calculate dot product of two vectors
To calculate the dot product between two vectors, we multiply their corresponding values along the x-, y-, and z- axes before adding them. Suppose we have two vectors:
So the dot product is calculated as:
Here,
Geometric formula
We use the following formula to calculate the dot product for the vectors geometrically when we know the angle between them.
: The resultant scalar of the dot product. : The magnitude of the vector . : The magnitude of the vector . : The angle between vectors and .
Calculation of the dot product in C++
Let's calculate the dot product of two vectors in C++ now. The following examples refer to the algebraic formula while calculating the dot product.
1. Using the inner_product function
We can calculate the dot product of two vectors using the inner_product function from the <numeric> header file, which is part of the Standard Template Library (STL). To include the <numeric> header file, we write the following code:
#include <numeric>
Now let's discuss the syntax for using inner_product function:
template <class InputIt1, class InputIt2, class T>T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init);
class InputIt1,class InputIt2,class T: The template parameters that represent the placeholders for the types of arguments that the function template can accept.first1: The iterator that points to the beginning of the first vector.last1: The iterator that points to the end of the first vector.first2: The iterator that points to the beginning of the second vector.init: The initial value for the accumulation of the dot product. We start adding the dot product from this value.
Implementing the dot product in C++
The following code demonstrates how to calculate the dot product of two vectors using the inner_product function.
#include <iostream>#include <vector>#include <numeric>using namespace std;int main() {vector<int> v1 = {2, 3, 1};vector<int> v2 = {4, 2, 5};int dotProduct = inner_product(v1.begin(), v1.end(), v2.begin(), 0);cout << "Dot product: " << dotProduct << endl;return 0;}
Lines 1–3: We include the necessary libraries.
Line 4: We bring all the names from the
stdnamespace into the code so we can avoid prefixing them withstd::.Lines 7–8: We define the two vectors whose dot product we want to calculate of.
Line 10: We calculate the dot product of the two vectors using
inner_productfunction. We send iterators (v1.begin()andv2.begin()) that point to the beginning of each vector, andv1.end()that points just past the last element ofv1as parameters. The last argument0is the initial value for the accumulation of the dot product.Line 12: We print the dot product.
Ready to master C++?
Our Learn C++ course will help you build practical skills, from basic loops to complex program structures. Join now and start coding your way to success!
2. Using the iterative approach
We use an iterative approach to calculate the dot product of the vectors. We use a loop to iterate through the axes values of both vectors, multiply the corresponding values, and add the results together.
Implementing the dot product using the iterative approach
The following code demonstrates how to calculate the dot product of two vectors using an iteration.
#include <iostream>#include <vector>using namespace std;int dotProduct( vector<int> v1, vector<int> v2) {int result = 0;for (int i = 0; i < v1.size(); ++i) {result += v1[i] * v2[i];}return result;}int main() {// In case of any missing axes write 0vector<int> v1 = {2, 3, 1};vector<int> v2 = {4, 2, 5};int dotProductResult = dotProduct(v1, v2);cout << "Dot product: " << dotProductResult << endl;return 0;}
Lines 5–11: In the function
dotProduct, we multiply the corresponding values of each axis of the vectors and add them in a variableresult.Lines 15–16: We define the two vectors we want to calculate the dot product of.
Line 18: We make a call to the function
dotProduct.
Conclusion
By understanding the concept and implementation of the dot product in C++, we can perform various mathematical computations efficiently and use the dot product in a wide range of applications, including vector manipulations, physics simulations, and computer graphics rendering.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
What is the dot product of IJK?
Is work scalar or vector?
Is the dot product a scalar?
What is the dot product of 2i 4j 5k and 3i 2j k?
Is electric current a vector?
How to check if a vector is empty in C++
What is the difference between the dot product and the cross product?
Unlock your potential: Vector operations series, all in one place!
Continue your exploration of vector operations by checking out our series of Answers below:
What is the dot product?
Understand the definition and significance of the dot product in vector operations.What is the intuition behind the dot product?
Explore the intuitive concept behind the dot product and its applications.Dot product of two vectors in C++
Learn how to calculate the dot product of two vectors using C++.
What is the inner product?
Discover the inner product and its relationship to the dot product.What is the cross product?
Understand the cross product, its geometric interpretation, and its applications in various fields.Dot product vs. cross product of two vectors
Explore the differences between the dot product and the cross product.
Free Resources