def gradient_points( self, points: np.ndarray, params: Union[List[float], List[Interval]] ) -> Union[List[np.ndarray], List[Interval]]: (alpha, ) = params x = points[:, 0] y = points[:, 1] z = points[:, 2] zero = iv.zeros_like(z) one = iv.ones_like(z) return [ iv.stack( [iv.cos(alpha * z), iv.sin(alpha * z), zero], axis=1, convert=isinstance(alpha, Interval)), iv.stack([-iv.sin(alpha * z), iv.cos(alpha * z), zero], axis=1, convert=isinstance(alpha, Interval)), iv.stack([ alpha * (-iv.sin(alpha * z) * x - iv.cos(alpha * z) * y), alpha * (iv.cos(alpha * z) * x - iv.sin(alpha * z) * y), one ], axis=1, convert=isinstance(alpha, Interval)), ]
def hessian_points(self, points: np.ndarray, params: Union[List[float], List[Interval]]) -> Union[List[List[np.ndarray]], List[List[Interval]]]: a, b, theta = params z = points[:, 2] zero = iv.zeros_like(z) one = iv.ones_like(z) return [ [ iv.stack([ zero, zero, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ zero, zero, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ (0.5 * iv.square(a) + b) * iv.cos(theta) * one, (0.5 * iv.square(a) + b) * iv.sin(theta) * one, zero ], axis=1, convert=isinstance(a, Interval)), ], [ iv.stack([ zero, zero, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ zero, zero, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ -(0.5 * iv.square(a) + b) * iv.sin(theta) * one, (0.5 * iv.square(a) + b) * iv.cos(theta) * one, zero ], axis=1, convert=isinstance(a, Interval)) ], [ iv.stack([ (0.5 * iv.square(a) + b) * iv.cos(theta) * one, (0.5 * iv.square(a) + b) * iv.sin(theta) * one, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ -(0.5 * iv.square(a) + b) * iv.sin(theta) * one, (0.5 * iv.square(a) + b) * iv.cos(theta) * one, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ zero, zero, zero ], axis=1, convert=isinstance(a, Interval)) ], ]
def transform( self, points: np.ndarray, params: Union[List[float], List[Interval]]) -> Union[np.ndarray, Interval]: assert len(params) == 1 alpha = params[0] x = points[:, 0] y = points[:, 1] z = points[:, 2] x_transformed = x * iv.cos(alpha * z) - y * iv.sin(alpha * z) y_transformed = x * iv.sin(alpha * z) + y * iv.cos(alpha * z) z_transformed = z return iv.stack([x_transformed, y_transformed, z_transformed], axis=1, convert=isinstance(alpha, Interval))
def gradient_params( self, points: np.ndarray, params: Union[List[float], List[Interval]] ) -> Union[List[np.ndarray], List[Interval]]: assert len(params) == 1 alpha = params[0] x = points[:, 0] y = points[:, 1] z = points[:, 2] d_alpha_x = -z * (x * iv.sin(z * alpha) + y * iv.cos(z * alpha)) d_alpha_y = z * (x * iv.cos(z * alpha) - y * iv.sin(z * alpha)) d_alpha_z = iv.zeros_like(z) return [ iv.stack([d_alpha_x, d_alpha_y, d_alpha_z], axis=1, convert=isinstance(alpha, Interval)) ]
def gradient_params(self, points: np.ndarray, params: Union[List[float], List[Interval]]) -> Union[List[np.ndarray], List[Interval]]: a, b = params x = points[:, 0] y = points[:, 1] z = points[:, 2] return [ iv.stack([ -iv.sin(a) * x - iv.cos(a) * iv.cos(b) * y + iv.cos(a) * iv.sin(b) * z, iv.cos(a) * x - iv.sin(a) * iv.cos(b) * y + iv.sin(a) * iv.sin(b) * z, iv.zeros_like(z) ], axis=1, convert=isinstance(a, Interval)), iv.stack([ iv.sin(a) * iv.sin(b) * y + iv.sin(a) * iv.cos(b) * z, -iv.cos(a) * iv.sin(b) * y - iv.cos(a) * iv.cos(b) * z, iv.cos(b) * y - iv.sin(b) * z ], axis=1, convert=isinstance(a, Interval)), ]
def hessian_params( self, points: np.ndarray, params: Union[List[float], List[Interval]] ) -> Union[List[List[np.ndarray]], List[List[Interval]]]: assert len(params) == 1 alpha = params[0] x = points[:, 0] y = points[:, 1] z = points[:, 2] d_alpha_alpha_x = -iv.square(z) * (x * iv.cos(alpha * z) - y * iv.sin(alpha * z)) d_alpha_alpha_y = -iv.square(z) * (x * iv.sin(alpha * z) + y * iv.cos(alpha * z)) d_alpha_alpha_z = iv.zeros_like(z) return [[ iv.stack([d_alpha_alpha_x, d_alpha_alpha_y, d_alpha_alpha_z], axis=1, convert=isinstance(alpha, Interval)) ]]
def hessian_points_params(self, points: np.ndarray, params: Union[List[float], List[Interval]]) -> Union[List[List[np.ndarray]], List[List[Interval]]]: a, b = params x = points[:, 0] zero = iv.zeros_like(x) one = iv.ones_like(x) return [ [ iv.stack([ -iv.sin(a) * one, iv.cos(a) * one, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ zero, zero, zero ], axis=1, convert=isinstance(a, Interval)), ], [ iv.stack([ -iv.cos(a) * iv.cos(b) * one, -iv.sin(a) * iv.cos(b) * one, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ iv.sin(a) * iv.sin(b) * one, -iv.cos(a) * iv.sin(b) * one, iv.cos(b) * one ], axis=1, convert=isinstance(a, Interval)), ], [ iv.stack([ iv.cos(a) * iv.sin(b) * one, iv.sin(a) * iv.sin(b) * one, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ iv.sin(a) * iv.cos(b) * one, -iv.cos(a) * iv.cos(b) * one, -iv.sin(b) * one ], axis=1, convert=isinstance(a, Interval)), ] ]
def gradient_points(self, points: np.ndarray, params: Union[List[float], List[Interval]]) -> Union[List[np.ndarray], List[Interval]]: a, b, theta = params x = points[:, 0] y = points[:, 1] z = points[:, 2] zero = iv.zeros_like(z) one = iv.ones_like(z) return [ iv.stack([ (0.5 * iv.square(a) * z + b * z + 1) * iv.cos(theta), (0.5 * iv.square(a) * z + b * z + 1) * iv.sin(theta), zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ -(0.5 * iv.square(a) * z + b * z + 1) * iv.sin(theta), (0.5 * iv.square(a) * z + b * z + 1) * iv.cos(theta), zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ (0.5 * iv.square(a) + b) * (iv.cos(theta) * x - iv.sin(theta) * y), (0.5 * iv.square(a) + b) * (iv.sin(theta) * x + iv.cos(theta) * y), one ], axis=1, convert=isinstance(a, Interval)) ]
def transform(self, points: np.ndarray, params: Union[List[float], List[Interval]]) -> Union[np.ndarray, Interval]: a, b = params x = points[:, 0] y = points[:, 1] z = points[:, 2] return iv.stack([ iv.cos(a) * x - iv.sin(a) * iv.cos(b) * y + iv.sin(a) * iv.sin(b) * z, iv.sin(a) * x + iv.cos(a) * iv.cos(b) * y - iv.cos(a) * iv.sin(b) * z, iv.sin(b) * y + iv.cos(b) * z ], axis=1, convert=isinstance(a, Interval))
def hessian_points_params( self, points: np.ndarray, params: Union[List[float], List[Interval]] ) -> Union[List[List[np.ndarray]], List[List[Interval]]]: (alpha, ) = params x = points[:, 0] y = points[:, 1] z = points[:, 2] zero = iv.zeros_like(x) return [ [ iv.stack([ -z * iv.sin(alpha * z), z * iv.cos(alpha * z), zero, ], axis=1, convert=isinstance(alpha, Interval)) ], [ iv.stack([ -z * iv.cos(alpha * z), -z * iv.sin(alpha * z), zero, ], axis=1, convert=isinstance(alpha, Interval)) ], [ iv.stack([ -x * iv.sin(alpha * z) - alpha * x * z * iv.cos(alpha * z) + alpha * y * z * iv.sin(alpha * z) - y * iv.cos(alpha * z), -alpha * x * z * iv.sin(alpha * z) + x * iv.cos(alpha * z) - y * iv.sin(alpha * z) - alpha * y * z * iv.cos(alpha * z), zero, ], axis=1, convert=isinstance(alpha, Interval)) ], ]
def hessian_params(self, points: np.ndarray, params: Union[List[float], List[Interval]]) -> Union[List[List[np.ndarray]], List[List[Interval]]]: a, b = params x = points[:, 0] y = points[:, 1] z = points[:, 2] zero = iv.zeros_like(x) return [ [ iv.stack([ -iv.cos(a) * x + iv.sin(a) * iv.cos(b) * y - iv.sin(a) * iv.sin(b) * z, -iv.sin(a) * x - iv.cos(a) * iv.cos(b) * y + iv.cos(a) * iv.sin(b) * z, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ iv.cos(a) * iv.sin(b) * y + iv.cos(a) * iv.cos(b) * z, iv.sin(a) * iv.sin(b) * y + iv.sin(a) * iv.cos(b) * z, zero ], axis=1, convert=isinstance(a, Interval)), ], [ iv.stack([ iv.cos(a) * iv.sin(b) * y + iv.cos(a) * iv.cos(b) * z, iv.sin(a) * iv.sin(b) * y + iv.sin(a) * iv.cos(b) * z, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ iv.sin(a) * iv.cos(b) * y - iv.sin(a) * iv.sin(b) * z, -iv.cos(a) * iv.cos(b) * y + iv.cos(a) * iv.sin(b) * z, -iv.sin(b) * y - iv.cos(b) * z ], axis=1, convert=isinstance(a, Interval)), ] ]
def hessian_points( self, points: np.ndarray, params: Union[List[float], List[Interval]] ) -> Union[List[List[np.ndarray]], List[List[Interval]]]: (alpha, ) = params x = points[:, 0] y = points[:, 1] z = points[:, 2] zero = iv.zeros_like(x) return [ [ iv.stack([ zero, zero, zero, ], axis=1, convert=isinstance(alpha, Interval)), iv.stack([ zero, zero, zero, ], axis=1, convert=isinstance(alpha, Interval)), iv.stack([ -alpha * iv.sin(alpha * z), alpha * iv.cos(alpha * z), zero, ], axis=1, convert=isinstance(alpha, Interval)), ], [ iv.stack([ zero, zero, zero, ], axis=1, convert=isinstance(alpha, Interval)), iv.stack([ zero, zero, zero, ], axis=1, convert=isinstance(alpha, Interval)), iv.stack([ -alpha * iv.cos(alpha * z), -alpha * iv.sin(alpha * z), zero, ], axis=1, convert=isinstance(alpha, Interval)), ], [ iv.stack([ -alpha * iv.sin(alpha * z), alpha * iv.cos(alpha * z), zero, ], axis=1, convert=isinstance(alpha, Interval)), iv.stack([ -alpha * iv.cos(alpha * z), -alpha * iv.sin(alpha * z), zero, ], axis=1, convert=isinstance(alpha, Interval)), iv.stack([ iv.square(alpha) * (-iv.cos(alpha * z) * x + iv.sin(alpha * z) * y), iv.square(alpha) * (-iv.sin(alpha * z) * x - iv.cos(alpha * z) * y), zero, ], axis=1, convert=isinstance(alpha, Interval)), ], ]
def test_single_scalar(self, a: float, expected: float): self.assertAlmostEqual(expected, iv.cos(a))
def test_nd_array(self, a): self.assertAlmostEqualNumpy(np.cos(a), iv.cos(a))
def test_nd_interval(self, a): self.assertSound(iv.cos(a), [a], np.cos)
def hessian_params(self, points: np.ndarray, params: Union[List[float], List[Interval]]) -> Union[List[List[np.ndarray]], List[List[Interval]]]: a, b, theta = params x = points[:, 0] y = points[:, 1] z = points[:, 2] zero = iv.zeros_like(z) return [ [ iv.stack([ z * (iv.cos(theta) * x - iv.sin(theta) * y), z * (iv.sin(theta) * x + iv.cos(theta) * y), zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ zero, zero, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ -a * z * (iv.sin(theta) * x + iv.cos(theta) * y), a * z * (iv.cos(theta) * x - iv.sin(theta) * y), zero ], axis=1, convert=isinstance(a, Interval)) ], [ iv.stack([ zero, zero, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ zero, zero, zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ -z * (iv.sin(theta) * x + iv.cos(theta) * y), z * (iv.cos(theta) * x - iv.sin(theta) * y), zero ], axis=1, convert=isinstance(a, Interval)) ], [ iv.stack([ -a * z * (iv.sin(theta) * x + iv.cos(theta) * y), a * z * (iv.cos(theta) * x - iv.sin(theta) * y), zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ -z * (iv.sin(theta) * x + iv.cos(theta) * y), z * (iv.cos(theta) * x - iv.sin(theta) * y), zero ], axis=1, convert=isinstance(a, Interval)), iv.stack([ (0.5 * iv.square(a) * z + b * z + 1) * (-iv.cos(theta) * x + iv.sin(theta) * y), (0.5 * iv.square(a) * z + b * z + 1) * (-iv.sin(theta) * x - iv.cos(theta) * y), zero ], axis=1, convert=isinstance(a, Interval)) ] ]
def test_single_interval(self, a: Interval, expected: Interval): self.assertAlmostEqual(expected.lower_bound, iv.cos(a).lower_bound) self.assertAlmostEqual(expected.upper_bound, iv.cos(a).upper_bound) self.assertSound(iv.cos(a), [a], np.cos)