Ensure a given array is of the correct type.
(
self,
data: Union[ArrayXd, Sequence[ArrayXd], Sequence[Any]],
*,
dtype: Optional[DTypes] = None,
)
| 739 | return cast(IntsXd, self.asarray(data, dtype=dtype)) |
| 740 | |
| 741 | def asarray( |
| 742 | self, |
| 743 | data: Union[ArrayXd, Sequence[ArrayXd], Sequence[Any]], |
| 744 | *, |
| 745 | dtype: Optional[DTypes] = None, |
| 746 | ) -> ArrayXd: |
| 747 | """Ensure a given array is of the correct type.""" |
| 748 | if isinstance(data, self.xp.ndarray): |
| 749 | if dtype is None: |
| 750 | return data |
| 751 | elif data.dtype == dtype: |
| 752 | return data |
| 753 | else: |
| 754 | return self.xp.asarray(data, dtype=dtype) |
| 755 | elif hasattr(data, "numpy"): |
| 756 | # Handles PyTorch Tensor |
| 757 | return data.numpy() # type: ignore[union-attr] |
| 758 | elif dtype is not None: |
| 759 | return self.xp.array(data, dtype=dtype) |
| 760 | else: |
| 761 | return self.xp.array(data) |
| 762 | |
| 763 | def as_contig(self, data: ArrayT, dtype: Optional[DTypes] = None) -> ArrayT: |
| 764 | """Allow the backend to make a contiguous copy of an array. |
no outgoing calls