sarcomp.copy#
- sarcomp.copy(array: ndarray | Array, dtype: str | dtype | None = None, p2=False, axis=1) Array #
Create a copy of an array, optionally converting it to a different data type and zero-padding if requested.
This function makes a deep copy of the input array, which can be either a numpy ndarray or an instance of the custom Array class. It allows for optional conversion of the copy to a specified target data type. If no target data type is specified, the copy retains the original data type of the input array.
Additionally, if p2 is set to True, the array will be zero-padded to the next power of two along the specified axis (0, 1, or 2).
- Parameters:
array (Union[np.ndarray, Array]) – The input array to be copied. Can be a numpy ndarray or an instance of the custom Array class.
dtype (Optional[Union[str, np.dtype]], optional) – The target data type for the copy, specified as a dtype object or a string (e.g., ‘float32’, ‘complex64’). If None (the default), the copy retains the input array’s data type.
p2 (bool, optional) – If True, the array will be zero-padded to the next power of two along the specified axis. Defaults to False.
axis (int, optional) – The axis along which to apply the zero-padding (if p2 is True). Must be one of 0, 1, or 2. Defaults to 1.
- Returns:
A new Array instance that is a copy of the input array, converted to dtype if specified, and optionally zero-padded.
- Return type:
Array
- Raises:
ValueError – If the conversion from the input array’s data type to dtype is not supported.
TypeError – If axis is not one of 0, 1, or 2.
Examples
>>> import numpy as np >>> import sarcomp as sc >>> >>> # Example 1: Simple copy >>> np_array = np.array([1, 2, 3], dtype=np.float32) >>> copied_array = sc.copy(np_array) >>> >>> # Example 2: Copy and convert to a different data type >>> converted_array = sc.copy(np_array, dtype='f8') # float64 >>> >>> # Example 3: Copy and zero-pad to the next power of two along axis 1 >>> padded_array = sc.copy(np_array, p2=True, axis=1)
Notes
The copy function is particularly useful when working with large arrays or when needing to ensure that modifications to new instances do not affect the original array. The optional data type conversion and zero-padding features add flexibility, allowing users to switch between different numerical representations and optimize array sizes for computational tasks involving FFT or similar operations.