The dot product of the unit vectors is 0 when calculated between different unit vectors, because they are orthogonal to each other.
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
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.
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.
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,
We use the following formula to calculate the dot product for the vectors geometrically when we know the angle between them.
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.
inner_product
functionWe 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.
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 std
namespace into the code so we can avoid prefixing them with std::
.
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_product
function. We send iterators (v1.begin()
and v2.begin()
) that point to the beginning of each vector, and v1.end()
that points just past the last element of v1
as parameters. The last argument 0
is 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!
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.
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 variable result
.
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
.
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.
Haven’t found what you were looking for? Contact Us
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