sarcomp.convert#
- sarcomp.convert(array: ndarray | Array, dtype: str | dtype) Array #
Convert an array to a specified target data type.
This function converts the input array, which can be either a numpy ndarray or an instance of a custom Array class, to a new Array instance with the specified target data type. The conversion supports standard numpy data types as well as custom data types defined within the Array class, including split-complex formats.
- Parameters:
array (Union[np.ndarray, Array]) – The input array to convert. Can be a numpy ndarray or an instance of the custom Array class.
dtype (Union[str, np.dtype]) – The target data type for the conversion, specified as a string (e.g., ‘float32’, ‘complex64’) or directly using an np.dtype object. This parameter also supports custom data type strings defined within the Array class (e.g., ‘split_complex64’).
- Returns:
A new Array instance containing the data from array converted to dtype.
- Return type:
Array
- Raises:
ValueError – If the conversion from the input data type to dtype is not supported or if dtype is not a recognized data type.
Examples
>>> import numpy as np >>> from sarcomp.memory import Array, convert # Adjust import path as needed
>>> np_array = np.arange(10, dtype=np.float32) >>> converted_array = convert(np_array,'float64') >>> converted_array.dtype dtype('float64')
>>> custom_array = Array(np_array, dtype='float32') >>> converted_array = convert(custom_array,'complex64') >>> converted_array.dtype dtype('complex64')
Notes
The function is designed to facilitate easy conversion between different array data types, enhancing flexibility in numerical computations and data processing tasks. Special attention is given to supporting both numpy’s standard data types and custom data types specific to the Array class, including handling of split-complex arrays.