LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Python Data Science NumPy ufunc NumPy's Trigonometric Functions
What are Trigonometric Functions?
NumPy provides the ufuncs sin(), cos() and tan() that take values in radians and produce the corresponding sin, cos and tan values.
Code
import numpy as np
x = np.sin(np.pi/4)
print(x)
the output will be
0.7071067811865476
Example 2: Find sine values for all of the values in arr.
import numpy as np
arr = np.array([np.pi/2, np.pi/3, np.pi/4])
x = np.sin(arr)
print(x)
the output will be
[1. 0.8660254 0.70710678]
Convert Degrees Into Radians
All of the trigonometric functions take radians as parameters by default but we can
convert radians to degrees and vice versa as well in NumPy.
Note: radians values are pi/180 * degree_values.
Example 3: Convert all of the values in following array arr to radians:
Code
import numpy as np
arr = np.array([45, 90, 120, 180, 245, 270, 360])
x = np.deg2rad(arr)
print(x)
the output will be
[0.78539816 1.57079633 2.0943951 3.14159265 4.27605667 4.71238898
6.28318531]
Radians to Degrees
Example 4: Convert all of the values in following array arr to degrees:
Code
import numpy as np
arr = np.array([np.pi/2, np.pi, 1.5*np.pi, 1.75*np.pi, 2*np.pi])
x = np.rad2deg(arr)
print(x)
the output will be
[ 90. 180. 270. 315. 360.]
Finding Angles
Finding angles from values of sine, cos, tan.
E.g. Inverse of sin, cos and tan are arcsin, arccos, and arctan respectively.
NumPy provides ufuncs arcsin(), arccos() and arctan() that produce radian values for corresponding sin, cos and tan values.
Example 5: Find the angle of 1.0.
Code
import numpy as np
x = np.arcsin(1.0)
print(x)
the output will be
1.5707963267948966
Angles of Each Value in Arrays
Example 6: Find the angle for all of the sine values in the array.
Code
import numpy as np
arr = np.array([1, -1])
x = np.arcsin(arr)
print(x)
the output will be
[ 1.57079633 -1.57079633]
Hypotenues
How to find hypotenues using pythagoras theorem in NumPy?
NumPy provides the hypot() function that takes the base and perpendicular values and
produces hypotenues based on pythagoras theorem.
Example 7: Find the hypotenues for 4 base and 3 perpendicular.
Code
import numpy as np
base = 2
perp = 4
x = np.hypot(base, perp)
print(x)
the output will be
4.47213595499958
Note: NumPy ufunc Universal function or ufuncs is a function which operates on ndarrays in an element by element fashion and supports array broadcasting, type casting, and many other standard features.