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

NumPy cos() Function



The NumPy cos() function is used to calculate the cosine of each element in an input array. It computes the cosine of the input value (in radians), where the cosine of an angle is defined as the ratio of the length of the adjacent 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 cosine of an angle is always within this range.

Syntax

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

numpy.cos(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 cosine 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 cosine of the corresponding element in the input array x.

Example: Basic Usage of cos() Function

In the following example, we use the cos() function to compute the cosine 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 cos to each element
result = np.cos(arr)
print(result)

The output obtained will be −

[ 1.0000000e+00  6.1232340e-17 -1.0000000e+00 -1.8369702e-16]

Example: Cosine of Angles in Degrees

In this example, we convert angles from degrees to radians before calculating their cosine using numpy.cos() function. The cosine 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 cosine of each angle in radians
result = np.cos(angles_radians)
print(result)

This will produce the following result −

[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
 6.12323400e-17]

Example: Cosine of a Single Scalar Value

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

import numpy as np

# Scalar value
scalar = np.pi/4

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

The output obtained is −

0.7071067811865476

Example: cos() Function with Negative Angles

In this example, we calculate the cosine of negative angles. The cosine function is an even function, meaning that cos(x) = cos(x) −

import numpy as np

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

This will produce the following result −

[ 6.1232340e-17 -1.0000000e+00 -1.8369702e-16]
numpy_trigonometric_functions.htm
Advertisements