NumPy sinh()

The numpy.sinh() function computes the hyperbolic sine of each element in an input array.

Syntax

</>
Copy
numpy.sinh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array. Each element will have its hyperbolic sine computed.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the hyperbolic sine function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the hyperbolic sine values of the input array elements. If the input is a scalar, a scalar is returned.


Examples

1. Computing Hyperbolic Sine of a Single Value

Here, we compute the hyperbolic sine of a single value.

</>
Copy
import numpy as np

# Define a single input value
x = 1.0  

# Compute the hyperbolic sine of the value
result = np.sinh(x)

# Print the result
print("sinh(1.0):", result)

Output:

sinh(1.0): 1.1752011936438014

2. Computing Hyperbolic Sine for an Array of Values

We compute the hyperbolic sine values for multiple inputs provided in an array.

</>
Copy
import numpy as np

# Define an array of input values
values = np.array([-2, -1, 0, 1, 2])

# Compute the hyperbolic sine of each value
sinh_values = np.sinh(values)

# Print the results
print("Input values:", values)
print("Hyperbolic sine values:", sinh_values)

Output:

Input values: [-2 -1  0  1  2]
Hyperbolic sine values: [-3.62686041 -1.17520119  0.          1.17520119  3.62686041]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

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

# Create an output array with the same shape
output_array = np.ndarray(values.shape)

# Compute hyperbolic sine and store the result in output_array
np.sinh(values, out=output_array)

# Print the results
print("Computed hyperbolic sine values:", output_array)

Output:

Computed hyperbolic sine values: [-1.17520119  0.          1.17520119]

4. Using the where Parameter

Using a condition to compute hyperbolic sine only for selected elements.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([-2, -1, 0, 1, 2])

# Define a mask (compute sinh only where mask is True)
mask = np.array([True, False, True, False, True])

# Compute hyperbolic sine values where mask is True
result = np.sinh(values, where=mask)

# Print the results
print("Computed hyperbolic sine values with mask:", result)

Output:

Computed hyperbolic sine values with mask: [-3.62686041  0.5         0.          1.5         3.62686041]

The hyperbolic sine values are computed only for elements where mask=True. The other values remain unchanged.