Get the Trigonometric cosine of an array of angles given in degrees with Python

PythonNumpyServer Side ProgrammingProgramming

To find the Trigonometric cosine of an array of angles given in degrees, use the numpy.cos() method in Python Numpy. The method returns the cosine of each element of the 1st parameter x. The 1st parameter, x, is an Angle, in radians (2pi means 360 degrees). Here, it is an array of angles. The 2nd and 3rd parameters are optional.

The 2nd parameter is an ndarray, 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. A tuple must have length equal to the number of outputs.

The 3rd parameter is the condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

Steps

At first, import the required library −

import numpy as np

Following are the Array of angles, finding cos 0, cos 30, cos 45, cos 60, cos 90, cos 180 −

arr = np.array((0., 30., 45., 60., 90., 180.))

Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the number of elements of the Array −

print("\nNumber of elements in the Array...\n",arr.size)

To find the cosines of an array of angles given in degrees, use the cos() method in Python Numpy −

print("\nResult...",np.cos(arr * np.pi / 180. ))

Example

import numpy as np

# To find the Trigonometric cosine of an array of angles given in degrees, use the numpy.cos() method in Python Numpy
# The method returns the cosine of each element of the 1st parameter x.

print("The Trigonometric cosine of an array of angles...")

# Array of angles
# finding cos 0, cos 30, cos 45, cos 60, cos 90, cos 180
arr = np.array((0., 30., 45., 60., 90., 180.))

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimensions...\n",arr.ndim)

# Get the number of elements in the Array
print("\nNumber of elements...\n", arr.size)

# To find the cosines of an array of angles given in degrees, use the cos() method in Python Numpy
print("\nResult...",np.cos(arr * np.pi / 180. ))

Output

The Trigonometric cosine of an array of angles...
Array...
[ 0. 30. 45. 60. 90. 180.]

Our Array type...
float64

Our Array Dimensions...
1

Number of elements...
6

Result... [ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17 -1.00000000e+00]
raja
Updated on 25-Feb-2022 05:23:18

Advertisements