NumPy cosh() Function



The NumPy cosh() function is used to calculate the hyperbolic cosine of each element in the input array. It computes the value of cosh(x) = (ex + e(-x)) / 2 for each element in the array, where e is Euler's number (approximately 2.71828). This function is commonly used in hyperbolic functions.

  • Domain: The function accepts real-valued input and can handle both positive and negative values. The domain is all real numbers.
  • Range: The output values are always positive, as the hyperbolic cosine function spans values from 1 to positive infinity.

Syntax

Following is the syntax of the NumPy cosh() function −

numpy.cosh(x, /, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Parameters

This function accepts the following parameters −

  • x: The input array or scalar. The function computes the hyperbolic cosine for each element in the array or scalar.
  • out (optional): 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.
  • where (optional): This condition is broadcast over the input. At locations where the condition is True, the result will be computed. Otherwise, the result will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are processed. The dtype of x is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array where each element is the hyperbolic cosine of the corresponding element in the input array x.

Example: Basic Usage of cosh() Function

In the following example, we use the cosh() function to compute the hyperbolic cosine of each element in a 1-dimensional array −

import numpy as np

# Creating a 1-dimensional array
arr = np.array([0, 1, 2, 3])

# Applying cosh to each element
result = np.cosh(arr)
print(result)

The output obtained will be −

[ 1.          1.54308063  3.76219569 10.067662  ]

Example: Hyperbolic Cosine of Negative Values

In this example, we calculate the hyperbolic cosine of negative values. The hyperbolic cosine function is an even function, meaning that cosh(-x) = cosh(x) −

import numpy as np

# Negative values for the array
arr = np.array([-1, -2, -3])

# Applying cosh to each element
result = np.cosh(arr)
print(result)

This will produce the following result −

[ 1.54308063  3.76219569 10.067662  ]

Example: Hyperbolic Cosine for Scalar Value

In this example, we use the cosh() function to calculate the hyperbolic cosine of a scalar value −

import numpy as np

# Scalar value
scalar = 1

# Applying cosh to the scalar
result = np.cosh(scalar)
print(result)

The output obtained is −

1.5430806348152437

Example: Hyperbolic Cosine of an Array with Different Shapes

In this example, we calculate the hyperbolic cosine of two arrays with different shapes. NumPy will broadcast the arrays to be compatible for element-wise computation −

import numpy as np

# Creating two arrays with different shapes
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# Applying cosh to each pair of elements (broadcasting)
result = np.cosh(arr1 + arr2)
print(result)

This will produce the following result −

[  74.20994852  548.31703516 4051.54202549]
numpy_trigonometric_functions.htm
Advertisements