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 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] x_transformed = (0.5 * iv.square(a) * z + b * z + 1) * x y_transformed = (0.5 * iv.square(a) * z + b * z + 1) * y z_transformed = z return iv.stack([x_transformed, y_transformed, z_transformed], 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]]]: 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 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 gradient_points( 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] zero = iv.zeros_like(z) return [ iv.stack([(0.5 * iv.square(a) * z + b * z + 1), zero, zero], axis=1, convert=isinstance(a, Interval)), iv.stack([zero, (0.5 * iv.square(a) * z + b * z + 1), zero], axis=1, convert=isinstance(a, Interval)), iv.stack([(0.5 * iv.square(a) + b) * x, (0.5 * iv.square(a) + b) * y, iv.ones_like(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]]]: a, b = params x = points[:, 0] zero = iv.zeros_like(x) one = iv.ones_like(x) 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) * one, zero, 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([zero, (0.5 * iv.square(a) + b) * one, zero], axis=1, convert=isinstance(a, Interval)), ], [ iv.stack([(0.5 * iv.square(a) + b) * one, zero, zero], axis=1, convert=isinstance(a, Interval)), iv.stack([zero, (0.5 * iv.square(a) + b) * one, zero], axis=1, convert=isinstance(a, Interval)), iv.stack([zero, zero, zero], axis=1, convert=isinstance(a, Interval)), ]]
def encode(transformation: Transformation, points: np.ndarray, params: List[Interval]) -> LinearBounds: center = [p.center() for p in params] zero_order_term = transformation.transform(points, center) gradients = transformation.gradient_params(points, center) hessian = transformation.hessian_params(points, params) offset = zero_order_term for i in range(len(params)): offset = offset - (gradients[i] * center[i]) second_order_bounds = 0.0 for i, j in itertools.product(range(len(params)), range(len(params))): factor = iv.square(params[i] - center[i]) if i == j else (params[i] - center[i]) * (params[j] - center[j]) second_order_bounds = second_order_bounds + (0.5 * hessian[i][j] * factor) return LinearBounds( upper_slope=np.stack(gradients, axis=2), lower_slope=np.stack(gradients, axis=2), upper_offset=offset + second_order_bounds.upper_bound, lower_offset=offset + second_order_bounds.lower_bound, )
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 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.square(a))
def test_single_interval(self, a: Interval, expected: Interval): self.assertAlmostEqual(expected.lower_bound, iv.square(a).lower_bound) self.assertAlmostEqual(expected.upper_bound, iv.square(a).upper_bound) self.assertSound(iv.square(a), [a], np.square)
def test_nd_array(self, a): self.assertAlmostEqualNumpy(np.square(a), iv.square(a))
def test_nd_interval(self, a): self.assertSound(iv.square(a), [a], np.square)