---
title: cos()
url: https://docs.vultr.com/python/third-party/numpy/cos
description: Calculate Cosine
publish_date: 2024-11-08T18:40:00.094512Z
last_updated: 2024-11-08T18:40:00.094512Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/725/9dfa8745-8a66-4337-b0b6-32a72ce9cf33.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Python Numpy cos() - Calculate Cosine

## Introduction

The `numpy.cos()` function in Python is part of the NumPy library, specifically designed to calculate the cosine of an array of angles given in radians. This mathematical function is widely used in fields involving trigonometry, Fourier transforms, and engineering simulations where cosine values play a critical role.

In this article, you will learn how to leverage the `numpy.cos()` function to compute cosine values efficiently. Explore how it works with arrays of various sizes, and how to handle inputs in degrees as opposed to radians.

## Using numpy.cos() with Scalars

### Calculate the Cosine of a Single Value

1. Import the NumPy library.
1. Convert an angle in degrees to radians.
1. Use the `numpy.cos()` function to compute the cosine of the angle.

    ```python
    import numpy as np

    angle_deg = 60
    angle_rad = np.radians(angle_deg)
    cosine_value = np.cos(angle_rad)
    print(cosine_value)
    ```

    In this example, the angle `60` degrees is converted to radians and then passed to `numpy.cos()` to calculate the cosine. The result is printed on the console.

### Handle a NumPy Array of Angles

1. Create a NumPy array of angles in degrees.
1. Convert the array of angles from degrees to radians.
1. Calculate the cosine for each angle in the array.

    ```python
    import numpy as np

    angles_deg = np.array([0, 30, 45, 60, 90])
    angles_rad = np.radians(angles_deg)
    cosine_values = np.cos(angles_rad)
    print(cosine_values)
    ```

    This snippet creates an array of angles, converts them to radians, and then computes the cosine for each angle. The cosines of all angles in the array are output at once.

## Handling Different Data Types

### Cosine Calculations on Floats and Integers

1. Understand that `numpy.cos()` operates seamlessly across different data types like integers and floats.
1. Define a mixed data type array for demonstration.

    ```python
    import numpy as np

    mixed_angles = np.array([30.0, 45, 60, 90])  # mixed integer and float
    angles_rad = np.radians(mixed_angles)
    cosine_values = np.cos(angles_rad)
    print(cosine_values)
    ```

    Here, despite the mix of integers and floats in the `mixed_angles` array, `numpy.cos()` processes each element without error, outputting their cosine values.

### Handling Negative and Positive Angles

1. Compute the cosine for both negative and positive radian values.

    ```python
    import numpy as np

    angles = np.array([-np.pi, np.pi/2, np.pi])
    cosine_values = np.cos(angles)
    print(cosine_values)
    ```

    This example demonstrates that cosine values are correctly computed for both negative and positive angles, showcasing the function’s versatility.

## Conclusion

The `numpy.cos()` function is a versatile tool for computing the cosine of angles in Python. It accepts input in radians and efficiently processes scalar values, arrays, and even mixed data types. By understanding how to use this function with real-world data, ensure your projects involving trigonometric calculations are both accurate and efficient. Whether you're dealing with angles in degrees or radians, `numpy.cos()` helps you perform these operations with ease, enhancing both the performance and readability of your code.