Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy sin() Function



The NumPy sin() function is used to calculate the sine of each element in an input array. It computes the sine of the input value (in radians), where the sine of an angle is defined as the ratio of the length of the opposite side to the hypotenuse in a right-angled triangle.

  • Domain: The function accepts input values in radians, and it can handle any real number. The domain is all real numbers.
  • Range: The output values lie between -1 and 1, as the sine of an angle is always within this range.

Syntax

Following is the syntax of the NumPy sin() function −

numpy.sin(x, /, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Parameters

This function accepts the following parameters −

  • x: The input array or scalar, in radians. The function computes the sine of each element of the array or scalar.
  • out (optional): A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where (optional): This condition is broadcast over the input. At locations where the condition is True, the result will be computed. Otherwise, the result will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are processed. The dtype of x is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array where each element is the sine of the corresponding element in the input array x.

Example: Basic Usage of sin() Function

In the following example, we use the sin() function to compute the sine of each element in a 1-dimensional array −

import numpy as np

# Creating a 1-dimensional array
arr = np.array([0, np.pi/2, np.pi, 3*np.pi/2])

# Applying sin to each element
result = np.sin(arr)
print(result)

The output obtained will be −

[ 0.0000000e+00  1.0000000e+00  1.2246468e-16 -1.0000000e+00]

Example: Sine of Angles in Degrees

In this example, we convert angles from degrees to radians before calculating their sine using numpy.sin() Function. The sine function expects input in radians, so we use numpy.radians() function to perform the conversion −

import numpy as np

# Angles in degrees
angles_degrees = np.array([0, 30, 45, 60, 90])

# Convert degrees to radians
angles_radians = np.radians(angles_degrees)

# Calculate sine of each angle in radians
result = np.sin(angles_radians)
print(result) 

This will produce the following result −

[0.         0.5               0.70710678 0.8660254  1.        ]

Example: Sine of a Single Scalar Value

In this example, we are using the sin() function to calculate a single scalar value −

import numpy as np

# Scalar value
scalar = np.pi/4

# Applying sin to the scalar
result = np.sin(scalar)
print(result)

The output obtained is −

0.7071067811865475

Example: sin() Function with Negative Angles

In this example, we calculate the sine of negative angles. The sine function is an odd function, meaning that sin(x)= sin(x) −

import numpy as np

negative_angles = np.array([-np.pi / 2, -np.pi, -3 * np.pi / 2])
result = np.sin(negative_angles)
print(result)  

This will produce the following result −

[-1.0000000e+00 -1.2246468e-16  1.0000000e+00]
numpy_trigonometric_functions.htm
Advertisements