NumPy arctan()

The numpy.arctan() method computes the arctangent (inverse tangent) of an array.

Example

import numpy as np

# create an array 
array1 = np.array([0, 1, -1])

# calculates the element-wise arctangent (inverse tangent) of array1 result = np.arctan(array1)
print(result) # Output: [ 0. 0.78539816 -0.78539816]

arctan() Syntax

The syntax of the numpy.arctan() method is:

numpy.arctan(x, out = None, where = True, dtype = None)

arctan() Arguments

The numpy.arctan() method takes the following arguments:

  • x - an input array
  • out (optional) - the output array where the result will be stored
  • where (optional) - a boolean array or condition indicating where to compute the arctangent
  • dtype (optional) - data type of the output array

arctan() Return Value

The numpy.arctan() method returns an array with the corresponding inverse tangent values.


Example 1: Use of out and where in arctan()

import numpy as np

array1 = np.array([0, -1, 1, 10, 100, -2])

# create an array of zeros with the same shape as array1
result = np.zeros_like(array1, dtype=float)  

# compute inverse tangent of elements in array1 # only where the element is greater than or equal to 0 np.arctan(array1, out = result, where = (array1 >= 0))
print(result)

Output

[0.         0.         0.78539816 1.47112767 1.56079666 0.        ]

Here,

  • out = result specifies that the output of the numpy.arctan() method should be stored in the result array,
  • where = (array1 >= 0) specifies that the inverse tangent operation should only be applied to elements in array1 that are greater than or equal to 0.

Example 2: Use of dtype Argument in arctan()

import numpy as np

# create an array
values = np.array([0, 1, -1])

# calculate the inverse tangent of # each value with float data type arctans_float = np.arctan(values, dtype = float)
print("Inverse Tangent with 'float' dtype:") print(arctans_float)
# calculate the inverse tangent of # each value with complex data type arctans_complex = np.arctan(values, dtype = complex)
print("\nInverse Tangent with 'complex' dtype:") print(arctans_complex)

Output

Inverse Tangent with 'float' dtype:
[ 0.          0.78539816 -0.78539816]

Inverse Tangent with 'complex' dtype:
[ 0.        +0.j  0.78539816+0.j -0.78539816+0.j]

Here, by specifying the desired dtype, we can control the data type of the output array according to our specific requirements.

Note: To learn more about the dtype argument, please visit NumPy Data Types.

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community