NumPy radians()

The numpy.radians() function converts angles from degrees to radians. This function is useful in trigonometric calculations where angles are required in radians.

Syntax

</>
Copy
numpy.radians(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array containing angles in degrees.
outndarray, None, or tuple of ndarray and None, optionalOptional output array to store the results. If None, a new array is created.
wherearray_like, optionalBoolean condition determining which elements to convert. Elements where where=False retain their original value.
castingstr, optionalDefines how input data types are cast when computing the conversion.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalSpecifies the data type of the output array.
subokbool, optionalDetermines whether subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the input angles converted from degrees to radians. If the input is a scalar, the function returns a scalar.


Examples

1. Converting a Single Angle to Radians

Here, we convert a single angle from degrees to radians.

</>
Copy
import numpy as np

# Define an angle in degrees
angle_degrees = 180  # 180 degrees

# Convert to radians
angle_radians = np.radians(angle_degrees)

# Print the result
print("180 degrees in radians:", angle_radians)

Output:

180 degrees in radians: 3.141592653589793

2. Converting an Array of Angles

We convert multiple angles from degrees to radians using an array.

</>
Copy
import numpy as np

# Define an array of angles in degrees
angles_degrees = np.array([0, 30, 45, 60, 90, 180, 360])

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

# Print the results
print("Angles in degrees:", angles_degrees)
print("Angles in radians:", angles_radians)

Output:

Angles in degrees: [  0  30  45  60  90 180 360]
Angles in radians: [0.         0.52359878 0.78539816 1.04719755 1.57079633 3.14159265
 6.28318531]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of angles in degrees
angles_degrees = np.array([0, 45, 90, 180])

# Create an output array with the same shape
output_array = np.empty_like(angles_degrees, dtype=float)

# Convert to radians and store the result in output_array
np.radians(angles_degrees, out=output_array)

# Print the results
print("Converted radians:", output_array)

Output:

Converted radians: [0.         0.78539816 1.57079633 3.14159265]

4. Using the where Parameter

Using a condition to convert only selected elements.

</>
Copy
import numpy as np

# Define an array of angles in degrees
angles_degrees = np.array([0, 30, 90, 180])

# Define a mask (convert only where mask is True)
mask = np.array([True, False, True, False])

# Convert to radians only for selected elements
result = np.radians(angles_degrees, where=mask)

# Print the results
print("Converted radians with mask:", result)

Output:

Converted radians with mask: [0.         0.         1.57079633 0.        ]

The conversion is performed only for elements where mask=True. The other values remain unchanged.