archimedes.zeros_like¶
- archimedes.zeros_like(x: ArrayLike, dtype: DTypeLike | None = None, sparse: bool = True, kind: str | None = None) SymbolicArray ¶
Create a symbolic array of zeros with the same shape and dtype as an input array.
This function constructs a symbolic array filled with zeros, matching the dimensions and data type of an existing array. It’s useful for creating compatible zero arrays in functions that need to work with both symbolic and numeric inputs.
- Parameters:
x (array_like) – The array whose shape and dtype will be used. Can be a NumPy ndarray, SymbolicArray, or any array-like object that can be converted to an array.
dtype (numpy.dtype, optional) – Data type of the new array. If None (default), uses the dtype of
x
.sparse (bool, optional) – If True (default), creates “structural” zeros, which are symbolic placeholders rather than actual numeric values. These are more efficient for building computational graphs. If False, creates numerical zero values.
kind ({"SX", "MX"} or None, optional) –
Kind of symbolic variable to create. If None (default), uses the kind of
x
if it’s a SymbolicArray, otherwise uses “MX”.SX: Scalar-based symbolic type. Each element has its own symbolic representation. Generally more efficient for element-wise operations.
MX: Matrix-based symbolic type. The entire array is represented by a single symbolic object. Supports a broader range of operations.
- Returns:
Symbolic array of zeros with the same shape as
x
, and with the specified dtype and symbolic kind.- Return type:
SymbolicArray
Notes
This function is the symbolic counterpart to NumPy’s
np.zeros_like()
. While creating a standard NumPy array of zeros requires numeric inputs, this function works with both symbolic and numeric arrays, preserving the symbolic nature when needed.When used inside a function decorated with
compile()
, this function helps create arrays that match the input’s shape, which is useful for maintaining compatibility between symbolic and numeric execution paths.The
sparse
parameter determines whether the zeros are “structural” (symbolic placeholders) or actual numeric zeros, which can affect memory usage and computational efficiency.When the array is not sparse, it is preferred to use
np.zeros_like
ornp.zeros(..., like=x)
, wherex
is either a SymbolicArray or NumPy array. This can provide better compatibility across both numeric and symbolic execution paths. The exception is when you specifically need sparse/structural zeros, which are only available through this direct function call withsparse=True
.Examples
>>> import numpy as np >>> import archimedes as arc >>> >>> # With a symbolic input array >>> x_sym = arc.sym("x", shape=(2, 3)) >>> z_sym = arc.zeros_like(x_sym) >>> print(z_sym.shape) (2, 3) >>> >>> # With a numeric input array >>> x_num = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) >>> z_num = arc.zeros_like(x_num) >>> print(z_num.shape) (3, 2) >>> >>> # Creating numerical zeros instead of structural zeros >>> z_dense = arc.zeros_like(x_sym, sparse=False) >>> >>> # Changing the kind of symbolic variable >>> z_mx = arc.zeros_like(x_sym, kind="MX") >>> >>> # In a function that will be traced symbolically: >>> @arc.compile >>> def process_array(x): >>> # Create a result array with same shape as input >>> # Dispatches to this function when x is a SymbolicArray >>> result = np.zeros_like(x) >>> for i in range(x.shape[0]): >>> result[i] = x[i] ** 2 >>> return result
See also
numpy.zeros_like
NumPy’s equivalent function for numeric arrays
zeros
Create a symbolic array of zeros with specified shape
ones_like
Create a symbolic array of ones with same shape as input
array
Create an array from data