Source code for adam.numpy.numpy_like

# Copyright (C) Istituto Italiano di Tecnologia (IIT). All rights reserved.


from dataclasses import dataclass
from typing import Union

import numpy as np
import numpy.typing as npt

from adam.core.spatial_math import ArrayLike, ArrayLikeFactory, SpatialMath


@dataclass
[docs] class NumpyLike(ArrayLike): """Class wrapping NumPy types"""
[docs] array: np.ndarray
[docs] def __setitem__(self, idx, value: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides set item operator""" if type(self) is type(value): self.array[idx] = value.array.reshape(self.array[idx].shape) else: self.array[idx] = value
[docs] def __getitem__(self, idx) -> "NumpyLike": """Overrides get item operator""" return NumpyLike(self.array[idx])
@property
[docs] def shape(self): return self.array.shape
[docs] def reshape(self, *args): return self.array.reshape(*args)
@property
[docs] def T(self) -> "NumpyLike": """ Returns: NumpyLike: transpose of the array """ return NumpyLike(self.array.T)
[docs] def __matmul__(self, other: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides @ operator""" if type(self) is type(other): return NumpyLike(self.array @ other.array) else: return NumpyLike(self.array @ np.array(other))
[docs] def __rmatmul__(self, other: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides @ operator""" if type(self) is type(other): return NumpyLike(other.array @ self.array) else: return NumpyLike(other @ self.array)
[docs] def __mul__(self, other: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides * operator""" if type(self) is type(other): return NumpyLike(self.array * other.array) else: return NumpyLike(self.array * other)
[docs] def __rmul__(self, other: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides * operator""" if type(self) is type(other): return NumpyLike(other.array * self.array) else: return NumpyLike(other * self.array)
[docs] def __truediv__(self, other: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides / operator""" if type(self) is type(other): return NumpyLike(self.array / other.array) else: return NumpyLike(self.array / other)
[docs] def __add__(self, other: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides + operator""" if type(self) is not type(other): return NumpyLike(self.array.squeeze() + other.squeeze()) return NumpyLike(self.array.squeeze() + other.array.squeeze())
[docs] def __radd__(self, other: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides + operator""" if type(self) is not type(other): return NumpyLike(self.array + other) return NumpyLike(self.array + other.array)
[docs] def __sub__(self, other: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides - operator""" if type(self) is not type(other): return NumpyLike(self.array.squeeze() - other.squeeze()) return NumpyLike(self.array.squeeze() - other.array.squeeze())
[docs] def __rsub__(self, other: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """Overrides - operator""" if type(self) is not type(other): return NumpyLike(other.squeeze() - self.array.squeeze()) return NumpyLike(other.array.squeeze() - self.array.squeeze())
[docs] def __neg__(self): """Overrides - operator""" return NumpyLike(-self.array)
[docs] class NumpyLikeFactory(ArrayLikeFactory): @staticmethod
[docs] def zeros(*x) -> "NumpyLike": """ Returns: NumpyLike: zero matrix of dimension x """ return NumpyLike(np.zeros(x))
@staticmethod
[docs] def eye(x: int) -> "NumpyLike": """ Args: x (int): matrix dimension Returns: NumpyLike: Identity matrix of dimension x """ return NumpyLike(np.eye(x))
@staticmethod
[docs] def array(x) -> "NumpyLike": """ Returns: NumpyLike: Vector wrapping *x """ return NumpyLike(np.array(x))
[docs] class SpatialMath(SpatialMath): def __init__(self): super().__init__(NumpyLikeFactory()) @staticmethod
[docs] def sin(x: npt.ArrayLike) -> "NumpyLike": """ Args: x (npt.ArrayLike): angle value Returns: NumpyLike: sin value of x """ return NumpyLike(np.sin(x))
@staticmethod
[docs] def cos(x: npt.ArrayLike) -> "NumpyLike": """ Args: x (npt.ArrayLike): angle value Returns: NumpyLike: cos value of x """ return NumpyLike(np.cos(x))
@staticmethod
[docs] def outer(x: npt.ArrayLike, y: npt.ArrayLike) -> "NumpyLike": """ Args: x (npt.ArrayLike): vector y (npt.ArrayLike): vector Returns: NumpyLike: outer product of x and y """ x = np.array(x) y = np.array(y) return NumpyLike(np.outer(x, y))
@staticmethod
[docs] def vertcat(*x: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """ Returns: NumpyLike: vertical concatenation of x """ if isinstance(x[0], NumpyLike): v = np.vstack([x[i].array for i in range(len(x))]) else: v = np.vstack([x[i] for i in range(len(x))]) return NumpyLike(v)
@staticmethod
[docs] def horzcat(*x: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """ Returns: NumpyLike: horrizontal concatenation of x """ if isinstance(x[0], NumpyLike): v = np.hstack([x[i].array for i in range(len(x))]) else: v = np.hstack([x[i] for i in range(len(x))]) return NumpyLike(v)
@staticmethod
[docs] def skew(x: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike": """ Args: x (Union[NumpyLike, npt.ArrayLike]): vector Returns: NumpyLike: the skew symmetric matrix from x """ if not isinstance(x, NumpyLike): return -np.cross(np.array(x), np.eye(3), axisa=0, axisb=0) x = x.array return NumpyLike(-np.cross(np.array(x), np.eye(3), axisa=0, axisb=0))