Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
python
cos
trigonometry

# What is numpy.cos() in Python? Educative Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The numpy.cos() method in Python calculates the ratio of the length of the side nearest to the angle to the length of the hypotenuse. This function returns the cosine of a number. To be more precise, it returns the cosine of a radian.

A visual representation of the cosine ($cos$) is as follows.

Visual representaion of cos

### Syntax

numpy.cos(x, out=None)


### Input parameters

• x: This method takes an array of radian numbers as an input.
• out=None: This is an optional parameter in the numpy.cos() function. This could be ndarray, None, or tuple of the ndarray. This parameter identifies a location where the solution or output is stored.

### Returns

This method returns an ndarray containing the cosine values of radian numbers from the input. The output may be scalar if the input is scalar.

### Example

import numpy as np# Creating an arrayarray = np.array([0, np.pi/2, np.pi])#Calculating the cosine of the valuesresult = np.cos(array)#Using the out parameternew_result = np.array([0, np.pi/2, np.pi])np.cos(array, out = new_result)print("Cosine of values : ", result)print("New Result array : ", new_result)
An example of the cos() function in Python

### Explanation

• Line 3: We create an array using the np.array() method.
• Line 6: We calculate the cosine of the values in the array.
• Line 9: We create a new array in this statement.
• Line 10: We calculate the cosine values and store them in the new_result array using the out parameter.

Note: Providing the out parameter in the numpy.cos() function will store the result in the out parameter, and return a reference to it.

RELATED TAGS

numpy
python
cos
trigonometry 