Wednesday, January 6, 2021

Python Pool: Numpy Angle Explained With Examples

Hello geeks and welcome in this article, we will cover the NumPy angle(). Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples. But at first, let us try to get a brief understanding of the function through its definition.

Suppose you want to find out the angle of a complex argument. Then, in that case, the function NumPy angle() comes handy and helps us achieve our output. We will develop more clarity about it as we move ahead in the article. In the next section, we will be looking at the syntax associated with it.

Syntax

numpy.angle(z, deg="")

This is the general syntax of our function. In the next section we will discuss the parameters and return value associated with it.

PARAMETER

1.Z: Array_like

This parameter represents the complex argument or the complex number corresponding to which the angle needs to be calculated.

2.Deg:bool

This is an optional parameter. By default, it is set to “false” in that case, it returns the angle in radians. In case you set it to “true,” we get the angle in degree.

Return

angles: ndarray or scalar

It returns the counterclockwise angle from the positive real axis on completion of the program.

Examples Covering Numpy Angle

We have covered all the necessary theory associated with our function. Also, we have looked at its definition and developed a brief understanding of our function. In this section, we will be looking at various examples that will help us better understand our function. We will start with an elementary level example and gradually move our way to more complicated examples.

#input
import numpy as ppool
print(ppool.angle((1+1j),deg="true"))

Output:

45.0

In the above example, at first, we have imported the NumPy module. Then used our syntax to obtain the desired output. Here we have specified the degree to be true, due to which we get the output in degree. Here the output justifies our input. We can understand it as follow. We have considered (1+1j) as our complex number. Here a=1(real part) and b=1(complex part). Then we also know that tan(θ)=b/a in that case. Concerning our condition, we have a tan(θ)=1/1=45 degrees. Hence justified.

We can also use this function for an array of numbers. We will see that in the next example.

#input
import numpy as ppool
a=[1+2j,1,1j,3+4j]
print(ppool.angle(a,deg="true"))

Output:

[63.43494882  0.         90.         53.13010235]

In this example, we have followed all the steps similar to that of the first one. But here, instead of a single value, we have used an array. The output in all the cases justifies our input, and hence we can say it is verified. If you want, you can cross-check, as I did in the first example.

Why don’t you try for the complex numbers of your choice and tell me what result you got.

Limitations of Numpy Angle

The function NumPy angle is a really nice function. But it too has its own limitation. As per the definition, it only helps us in calculating the angle between the complex arguments. This means we cannot use this function to calculate the angle value between 2 points or vectors. It works great in its domain, but outside that, it is of no great use.

What is vector and how to calculate the angle between two vectors using the numpy angle Function

Vectors are generally defined as the physical quantities that, along with a magnitude, have a direction associated with them. Elementary examples of a vector are displacement, velocity, etc. The vectors are generally represented using the symbolic representation like î(along the x-axis), ĵ (along the y-axis), k̂(along the z-axis).

Now let us try to find out how to calculate the angle between 2 vectors. The general formula to calculate the angle between them is

                                                                                        [COS (θ)= a.b/|a|.|b|]   (consider an arrow symbol over a and b)
import numpy as np
vec1 = [2, 1]
vec2 = [1, 3]

unit1 = vec1 / np.linalg.norm(vec1)
unit2 = vec2 / np.linalg.norm(vec2)
dot_product = np.dot(unit1, unit2)
print(np.arccos(dot_product))

Output:

0.7853981633974484

In the example, we have first imported the NumPy module at first. Then we have defined 2 of our arrays between which we want to calculate the angle. Then we have used another function of the NumPy library which is linalg norm(). This function returns one of an infinite number of vector norms. Then we have used the function arccos that helps us in calculating the value of cos inverse. Then our value is calculated.

Must Read

Conclusion

In this article, we covered the NumPy angle(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. In the end, we can conclude that NumPy angle() is used to find the angle for a complex argument. I hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read Kronecker delta up next.

The post Numpy Angle Explained With Examples appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...