Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn ufunc Trigomentary

NumPy's trigonometric functions are part of its universal functions (ufuncs), which are designed to operate efficiently on NumPy arrays, performing element-wise computations. Trigonometric ufuncs allow you to apply various trigonometric operations to each element of a NumPy array.


Explanation of some commonly used trigonometric ufuncs in NumPy:


Sine (numpy.sin()): Computes the sine of each element in the input array. The sine of an angle in radians is the ratio of the length of the side opposite the angle to the length of the hypotenuse in a right-angled triangle.

import numpy as np
# Define an array of angles in radians
angles = np.array([0, np.pi/2, np.pi])
# Compute the sine of each angle
sine_values = np.sin(angles)
print("Sine of angles:", sine_values) # Output: [0. 1. 1.2246468e-16]


Cosine (numpy.cos()): Computes the cosine of each element in the input array. The cosine of an angle in radians is the ratio of the length of the adjacent side to the length of the hypotenuse in a right-angled triangle.

import numpy as np
# Define an array of angles in radians
angles = np.array([0, np.pi/2, np.pi])
# Compute the cosine of each angle
cosine_values = np.cos(angles)
print("Cosine of angles:", cosine_values) # Output: [ 1.000000e+00 6.123234e-17 -1.000000e+00]


Tangent (numpy.tan()): Computes the tangent of each element in the input array. The tangent of an angle in radians is the ratio of the length of the side opposite the angle to the length of the side adjacent to the angle in a right-angled triangle.

import numpy as np
# Define an array of angles in radians
angles = np.array([0, np.pi/4, np.pi/2])
# Compute the tangent of each angle
tangent_values = np.tan(angles)
print("Tangent of angles:", tangent_values) # Output: [0.00000000e+00 1.00000000e+00 1.63312394e+16]


Inverse Sine (numpy.arcsin()): Computes the inverse sine (arcsine) of each element in the input array. It returns the angle in radians whose sine is the specified number.

import numpy as np
# Define an array of sine values
sine_values = np.array([0, 0.5, 1])
# Compute the inverse sine of each value
inverse_sine_values = np.arcsin(sine_values)
print("Inverse sine values:", inverse_sine_values) # Output: [0. 0.52359878 1.57079633]


Inverse Cosine (numpy.arccos()): Computes the inverse cosine (arccosine) of each element in the input array. It returns the angle in radians whose cosine is the specified number.

import numpy as np
# Define an array of cosine values
cosine_values = np.array([1, 0.5, 0])
# Compute the inverse cosine of each value
inverse_cosine_values = np.arccos(cosine_values)
print("Inverse cosine values:", inverse_cosine_values) # Output: [0. 1.04719755 1.57079633]


Inverse Tangent (numpy.arctan()):Computes the inverse tangent (arctangent) of each element in the input array. It returns the angle in radians whose tangent is the specified number.

import numpy as np
# Define an array of tangent values
tangent_values = np.array([0, 1, np.inf])
# Compute the inverse tangent of each value
inverse_tangent_values = np.arctan(tangent_values)
print("Inverse tangent values:", inverse_tangent_values) # Output: [0. 0.78539816 1.57079633]


These examples demonstrate how to use trigonometric ufuncs in NumPy to perform various trigonometric operations on arrays of angles or trigonometric values.







Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java