Esempio n. 1
0
class TestTwistingZ(RelaxationTestCase):
    @parameterized.expand(
        zip(sample_points(), np.random.uniform(-10, 10, (100, 1))))
    def test_transformation_float(self, x, alpha):
        x_rotated = x[:, 0] * np.cos(alpha * x[:, 2]) - x[:, 1] * np.sin(
            alpha * x[:, 2])
        y_rotated = x[:, 0] * np.sin(alpha * x[:, 2]) + x[:, 1] * np.cos(
            alpha * x[:, 2])
        z_rotated = x[:, 2]
        expected = np.stack([x_rotated, y_rotated, z_rotated], axis=1)
        transformation = TwistingZ()
        actual = transformation.transform(x, alpha)
        self.assertAlmostEqualNumpy(expected, actual)

    @parameterized.expand(zip(sample_points(), sample_params(num_params=1)))
    def test_transformation_interval(self, x, params):
        transformation = TwistingZ()
        actual = transformation.transform(x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))

    @parameterized.expand(zip(sample_points(), sample_params(num_params=1)))
    def test_transformation_taylor(self, x, params):
        transformation = TwistingZ()
        actual = taylor.encode(transformation, x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))
Esempio n. 2
0
class TestShearingZ(RelaxationTestCase):
    @parameterized.expand(
        zip(sample_points(), np.random.uniform(-10, 10, (100, 2))))
    def test_transformation_float(self, x, params):
        sx, sy = params
        transformed_x = x[:, 0] + sx * x[:, 2]
        transformed_y = x[:, 1] + sy * x[:, 2]
        transformed_z = x[:, 2]
        expected = np.stack([transformed_x, transformed_y, transformed_z],
                            axis=1)
        transformation = ShearingZ()
        actual = transformation.transform(x, params)
        self.assertAlmostEqualNumpy(expected, actual)

    @parameterized.expand(zip(sample_points(), sample_params(num_params=2)))
    def test_transformation_interval(self, x, params):
        transformation = ShearingZ()
        actual = transformation.transform(x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))

    @parameterized.expand(zip(sample_points(), sample_params(num_params=2)))
    def test_transformation_taylor(self, x, params):
        transformation = ShearingZ()
        actual = taylor.encode(transformation, x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))
Esempio n. 3
0
class TestCompositionSoundness(RelaxationTestCase):
    @parameterized.expand(zip(sample_points(), sample_params(num_params=3)))
    def test_taylor_twisting_tapering(self, x, params):
        transformation = Composition(TwistingZ(), TaperingZ())
        actual = taylor.encode(transformation, x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))

    @parameterized.expand(zip(sample_points(), sample_params(num_params=2)))
    def test_taylor_twisting_rotation(self, x, params):
        transformation = Composition(TwistingZ(), RotationZ())
        actual = taylor.encode(transformation, x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))

    @parameterized.expand(zip(sample_points(), sample_params(num_params=4)))
    def test_taylor_twisting_rotation_tapering(self, x, params):
        transformation = Composition(TwistingZ(),
                                     Composition(RotationZ(), TaperingZ()))
        actual = taylor.encode(transformation, x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))

    @parameterized.expand(zip(sample_points(), sample_params(num_params=3)))
    def test_taylor_rot_zyx(self, x, params):
        transformation = Composition(RotationZ(),
                                     Composition(RotationY(), RotationX()))
        actual = taylor.encode(transformation, x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))
Esempio n. 4
0
class TestRotationZ(RelaxationTestCase):

    @parameterized.expand(zip(
        sample_points(),
        np.random.uniform(-10, 10, (100, 1))
    ))
    def test_transformation_float(self, x, alpha):
        transformation = RotationZ()
        expected = rotate_z(x, alpha.item())
        actual = transformation.transform(x, alpha)
        self.assertAlmostEqualNumpy(expected, actual)

    @parameterized.expand(zip(
        sample_points(),
        sample_params(num_params=1)
    ))
    def test_transformation_interval(self, x, params):
        transformation = RotationZ()
        actual = transformation.transform(x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))

    @parameterized.expand(zip(
        sample_points(),
        sample_params(num_params=1)
    ))
    def test_transformation_taylor(self, x, params):
        transformation = RotationZ()
        actual = taylor.encode(transformation, x, params)
        self.assertSound(actual, params, partial(transformation.transform, x))
Esempio n. 5
0
class TestCompositionRotZX(RelaxationTestCase):
    @parameterized.expand(
        zip(sample_points(), np.random.uniform(-10, 10, (100, 2))))
    def test_transformation_float(self, points, params):
        manual_transformation = RotationZX()
        auto_transformation = Composition(RotationZ(), RotationX())
        expected = manual_transformation.transform(points, params)
        actual = auto_transformation.transform(points, params)
        self.assertAlmostEqualNumpy(expected, actual)

    @parameterized.expand(zip(sample_points(), sample_params(2)))
    def test_transformation_interval(self, points, params):
        manual_transformation = RotationZX()
        auto_transformation = Composition(RotationZ(), RotationX())
        self.assertSound(manual_transformation.transform(points, params),
                         params,
                         partial(manual_transformation.transform, points))
        self.assertSound(auto_transformation.transform(points, params), params,
                         partial(auto_transformation.transform, points))

    @parameterized.expand(
        zip(sample_points(), np.random.uniform(-10, 10, (100, 2))))
    def test_gradient_params_float(self, points, params):
        expected = RotationZX().gradient_params(points, params)
        actual = Composition(RotationZ(),
                             RotationX()).gradient_params(points, params)
        self.assertAlmostEqualList(expected, actual)

    @parameterized.expand(
        zip(sample_points(), np.random.uniform(-10, 10, (100, 2))))
    def test_gradient_points_float(self, points, params):
        expected = RotationZX().gradient_points(points, params)
        actual = Composition(RotationZ(),
                             RotationX()).gradient_points(points, params)
        self.assertAlmostEqualList(expected, actual)

    @parameterized.expand(
        zip(sample_points(), np.random.uniform(-10, 10, (100, 2))))
    def test_hessian_params_float(self, points, params):
        expected = RotationZX().hessian_params(points, params)
        actual = Composition(RotationZ(),
                             RotationX()).hessian_params(points, params)
        self.assertAlmostEqualList(expected, actual)

    @parameterized.expand(
        zip(sample_points(), np.random.uniform(-10, 10, (100, 2))))
    def test_hessian_points_float(self, points, params):
        expected = RotationZX().hessian_points(points, params)
        actual = Composition(RotationZ(),
                             RotationX()).hessian_points(points, params)
        self.assertAlmostEqualList(expected, actual)

    @parameterized.expand(
        zip(sample_points(), np.random.uniform(-10, 10, (100, 2))))
    def test_hessian_points_params_float(self, points, params):
        expected = RotationZX().hessian_points_params(points, params)
        actual = Composition(RotationZ(), RotationX()).hessian_points_params(
            points, params)
        self.assertAlmostEqualList(expected, actual)

    @parameterized.expand(zip(sample_points(), sample_params(num_params=2)))
    def test_taylor_approximation(self, x, params):
        manual_transformation = RotationZX()
        auto_transformation = Composition(RotationZ(), RotationX())
        manual = taylor.encode(manual_transformation, x, params)
        auto = taylor.encode(auto_transformation, x, params)
        self.assertSound(manual, params,
                         partial(manual_transformation.transform, x))
        self.assertSound(auto, params, partial(auto_transformation.transform,
                                               x))