NumPy heaviside()
The numpy.heaviside()
function computes the Heaviside step function element-wise.
It is commonly used in signal processing and control systems to represent step changes in values.
Syntax
numpy.heaviside(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1 | array_like | Input values. The function evaluates whether values are negative, zero, or positive. |
x2 | array_like | The function value for elements where x1 == 0 . Typically 0.5 , but can be 0 or 1 . |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None , a new array is created. |
where | array_like, optional | Boolean mask specifying which elements should be computed. Other elements retain their original value. |
casting | str, optional | Defines the casting behavior of data types. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array or scalar containing the computed Heaviside step function values. If both x1
and x2
are scalars, a scalar is returned.
Examples
1. Basic Heaviside Function Computation
In this example, we compute the Heaviside step function for a scalar input value.
import numpy as np
# Define input values
x1 = -2 # A negative value
x2 = 0.5 # The value when x1 is zero
# Compute the Heaviside function
result = np.heaviside(x1, x2)
# Print the result
print("Heaviside(-2, 0.5):", result)
Output:
Heaviside(-2, 0.5): 0.0

Since x1
is negative, the Heaviside function returns 0
.
2. Computing Heaviside for an Array of Values
Here, we compute the Heaviside function for an array of input values.
import numpy as np
# Define an array of input values
x1 = np.array([-3, -1, 0, 1, 3])
# Define the value to be used when x1 is zero
x2 = 0.5
# Compute the Heaviside function for the array
result = np.heaviside(x1, x2)
# Print the results
print("Input values:", x1)
print("Heaviside step function results:", result)
Output:
Input values: [-3 -1 0 1 3]
Heaviside step function results: [0. 0. 0.5 1. 1. ]

Values less than zero return 0
, values greater than zero return 1
, and x1=0
returns 0.5
(the specified x2
value).
3. Using the out
Parameter
We store the result in a predefined output array.
import numpy as np
# Define an array of input values
x1 = np.array([-2, 0, 2])
x2 = 0.5 # Value when x1 is zero
# Create an output array with the same shape
output_array = np.ndarray(shape=x1.shape)
# Compute the Heaviside function and store in output_array
np.heaviside(x1, x2, out=output_array)
# Print the results
print("Computed Heaviside values:", output_array)
Output:
Computed Heaviside values: [0. 0.5 1. ]

4. Using the where
Parameter
Using a condition to compute Heaviside function only for selected elements.
import numpy as np
# Define an array of input values
x1 = np.array([-2, 0, 2])
x2 = 0.5 # Value when x1 is zero
# Define a mask (compute only where mask is True)
mask = np.array([True, False, True])
# Compute the Heaviside function with the mask
result = np.heaviside(x1, x2, where=mask)
# Print the results
print("Computed Heaviside values with mask:", result)
Output:
Computed Heaviside values with mask: [0. 0. 1. ]

The function is computed only for elements where mask=True
, while other values remain unchanged.