archimedes.ones_like¶

archimedes.ones_like(x: ArrayLike, dtype: DTypeLike | None = None, kind: str | None = None) SymbolicArray¶

Create a symbolic array of ones with the same shape and dtype as an input array.

This function constructs a symbolic array filled with ones, matching the dimensions and data type of an existing array. It’s useful for creating masks, weights, or initial values 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.

  • 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 ones 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 ones_like. While a standard NumPy array of ones can only hold numeric values, this function works with both symbolic and numeric entries, preserving the symbolic nature when needed.

When used inside a function decorated with :py:func:compile, this function helps create arrays of ones that match the input’s shape, which is useful for for initializing accumulators, creating masks, or setting default values.

Examples

>>> import numpy as np
>>> import archimedes as arc
>>>
>>> # With a symbolic input array
>>> x_sym = arc.sym("x", shape=(2, 3), kind="SX")
>>> o_sym = arc.ones_like(x_sym)
>>> print(o_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]])
>>> o_num = arc.ones_like(x_num)
>>> print(o_num.shape)
(3, 2)
>>>
>>> # Changing the kind of symbolic variable
>>> o_mx = arc.ones_like(x_sym, kind="MX")
>>>
>>> # In a function that will be traced symbolically:
>>> @arc.compile
>>> def process_array(x):
>>>     # Initialize result with ones. Dispatches to this function when x is a
>>>     # SymbolicArray
>>>     result = np.ones_like(x)
>>>     for i in range(x.shape[0]):
>>>         result[i] *= x[i]
>>>     return result

See also

numpy.ones_like

NumPy’s equivalent function for numeric arrays

ones

Create a symbolic array of ones with specified shape

zeros_like

Create a symbolic array of zeros with same shape as input

empty_like

Create an uninitialized array with same shape as input