numpy.acos() function is same as numpy.arccos() function.
NumPy acos()
The numpy.acos()
function computes the inverse cosine (arccos) of each element in an input array.
It returns the angle in radians within the range [0, π]
.
Syntax
</>
Copy
numpy.acos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | x-coordinates on the unit circle. The valid input domain is [-1, 1] . |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the inverse cosine. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with the inverse cosine values of the input array elements, expressed in radians within the range [0, π]
. If the input is a scalar, a scalar is returned.
Examples
1. Computing Inverse Cosine of a Single Value
Here, we compute the inverse cosine of a single value.
</>
Copy
import numpy as np
# Define a value within the valid domain [-1, 1]
value = 0.5
# Compute the inverse cosine (arccos) of the value
result = np.acos(value)
# Print the result
print("Arccos of 0.5 (in radians):", result)
Output:
Arccos of 0.5 (in radians): 1.0471975511965979

2. Computing Inverse Cosine for an Array of Values
We compute the inverse cosine values for multiple inputs provided in an array.
</>
Copy
import numpy as np
# Define an array of x-coordinates in the range [-1, 1]
values = np.array([-1, -0.5, 0, 0.5, 1])
# Compute the inverse cosine of each value
arccos_values = np.acos(values)
# Print the results
print("Input values:", values)
print("Arccos values (in radians):", arccos_values)
Output:
Input values: [-1. -0.5 0. 0.5 1. ]
Arccos values (in radians): [3.14159265 2.0943951 1.57079633 1.04719755 0. ]

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 x-coordinates
values = np.array([-1, 0, 1])
# Create an output array with the same shape
output_array = np.ndarray(shape=[1, 3])
# Compute inverse cosine and store the result in output_array
np.acos(values, out=output_array)
# Print the results
print("Computed arccos values:", output_array)
Output:
Computed arccos values: [[3.14159265 1.57079633 0. ]]

4. Using the where
Parameter
Using a condition to compute inverse cosine only for selected elements.
</>
Copy
import numpy as np
# Define an array of x-coordinates
values = np.array([-1, -0.5, 0, 0.5, 1])
# Define a mask (compute inverse cosine only where mask is True)
mask = np.array([True, False, True, False, True])
# Compute arccos values where mask is True
result = np.acos(values, where=mask)
# Print the results
print("Computed arccos values with mask:", result)
Output:
Computed arccos values with mask: [3.14159265 0.5 1.57079633 1.5 0. ]

The inverse cosine values are computed only for elements where mask=True
.