.sin()
Published Jun 25, 2024
Contribute to Docs
In NumPy, the .sin()
method calculates the trigonometric sine of each element in an array. This method operates element-wise on an array and returns a new array containing the sine values of the input array’s elements. It is beneficial in mathematical computations involving trigonometric operations.
Syntax
numpy.sin(x, out=None, where=True)
array
: An array-like structure containing the elements for which the sine is computed. The elements should be in radians, not degrees.out
(Optional): Specifies the array where the result of the operation should be stored. If not provided, a new array is created to store the results.where
(Optional): The condition (array of boolean values) that determines the elements where the sine function is applied.- If the condition is
True
for a particular element, the sine is computed for that element. - If the condition is
False
for a particular element, the sine of the element will not be computed and the original element is retained. - If not provided, the sine is computed for all elements.
- If the condition is
Example
The following example illustrates the usage of the .sin()
method:
# Importing the 'numpy' library as 'np'import numpy as np# When the elements in the array are already in radiansarray1 = np.array([0, np.pi/2, np.pi, 3*np.pi/2])result1 = np.sin(array1)# The angles are originally in degrees, so they need to be converted to radiansarray2 = np.array((0, 30, 45, 90, 180))result2 = np.sin(array2 * np.pi/180) # convert to radians# using the where parameterarray3 = np.array([np.pi/6, np.pi/4, np.pi/2, np.pi])result3 = np.sin(array3, where=array3 >= np.pi/2)print(result1)print(result2)print(result3)
The output of the above code is as shown below:
[ 0.0000000e+00 1.0000000e+00 1.2246468e-16 -1.0000000e+00][0.00000000e+00 5.00000000e-01 7.07106781e-01 1.00000000e+001.22464680e-16][0.0000000e+00 0.0000000e+00 1.0000000e+00 1.2246468e-16]
Codebyte Example
In this codebyte example, the .sin()
method computes the sine of the elements of the array:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours