Hello coders!! In this example, we will be learning about NumPy cross product in Python. We will also see different examples to clarify the concept. Let us dive into the topic.
What is a cross product?
A cross product, also known as a vector product is a binary operation done between two vectors in 3D space. It is denoted by the symbol X. A cross product between two vectors ‘a X b’ is perpendicular to both a and b.
What is NumPy in python?
It is an inbuilt module in Python used primarily for array operations. It also includes functions for linear algebra, Fourier transform, and matrices. Let us see some examples to see how NumPy is used for cross product.
syntax:
numpy.cross
(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)
parameters:
- a: first vector
- b: second vector
- axisa: Axis of a that defines the vector
- axisb: Axis of b that defines the vector
- axisc: Axis of c containing the cross product vector
- axis: If defined, the axis of a, b and c that defines the vector(s) and cross product(s)
Return Value:
Cross product of two vectors
Example 1: Vector cross product:
import numpy as np x = [1, -1, 1] y = [4, 2, 3] print('Vector 1:',x) print('Vector 2:',y) z=np.cross(x, y) print('Cross Product:',z)
Output & Explanation:
Here, first, we imported the NumPy module to use its functions. We then declared two 3d vectors. Then we used the method to calculate the cross product of the two vectors. As you can see it’s very easy to find the cross product of two vectors using the NumPy module.
Example 2: One 2D vector:
import numpy as np x = [1, -1] y = [4, 2, 3] print('Vector 1:',x) print('Vector 2:',y) z=np.cross(x, y) print('Cross Product:',z)
Output & Explanation:
Here, we have used one 3d vector and one 2d vector and then calculated its cross product using the function available in the NumPy module of python.
Example 3: Cross Product of 3D Vectors
import numpy as np x = np.array([[1,2,3], [4,5,6]]) y = np.array([[4,5,6], [1,2,3]]) print('Vector 1:',x,sep='\n') print('Vector 2:',y,sep='\n') z=np.cross(x, y) print('Cross Product:',z,sep='\n')
Output & Explanation:
In this example, we used 3D vectors to see how the output turns out to be. As you can see, the method calculates its cross-product and gives the desired output.
Why NumPy cross product is slow?
Let "a"
b
e an array with vectors laid out across either the most quickly varying axis or the slowest varying axis, or something in between. axisa
tells cross()
how the vectors are laid out in "a"
. By default, the value of the most slowly varying axis is taken. axisb
works the same with input "b"
. If the output is an array, the output vectors can be laid out with different array axes. The layout of the vectors in its output array is determined by axisc
. The most slowly varying axis is indicated by default.
You Might Be Also Interested in Reading
- Numpy Dot Product in Python With Examples
- Python 2d List: From Basic to Advance
- Numpy Multiply
- Numpy Angle Explained With Examples
Conclusion| NumPy Cross Product
With this, we come to an end with this article. We learned about the NumPy cross method in detail and also saw various examples to clarify the concept.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
Happy Pythoning!
The post NumPy Cross Product in Python with Examples appeared first on Python Pool.
from Planet Python
via read more
No comments:
Post a Comment