class CanonicalChange(unittest.TestCase):

    def setUp(self):

        """Set up an example from Hill's equations."""

        x_2 = Powers((2, 0, 0, 0, 0, 0))
        px2 = Powers((0, 2, 0, 0, 0, 0))
        y_2 = Powers((0, 0, 2, 0, 0, 0))
        py2 = Powers((0, 0, 0, 2, 0, 0))
        z_2 = Powers((0, 0, 0, 0, 2, 0))
        pz2 = Powers((0, 0, 0, 0, 0, 2))
        xpy = Powers((1, 0, 0, 1, 0, 0))
        ypx = Powers((0, 1, 1, 0, 0, 0))
        terms = {px2: 0.5, py2: 0.5, pz2: 0.5,
                 xpy: -1.0, ypx: 1.0,
                 x_2: -4.0, y_2: 2.0, z_2: 2.0}
        assert len(terms) == 8

        self.h_2 = Polynomial(6, terms=terms)
        self.lie = LieAlgebra(3)
        self.diag = Diagonalizer(self.lie)
        self.eq_type = 'scc'
        e = []
        e.append(+sqrt(2.0*sqrt(7.0)+1.0))
        e.append(-e[-1])
        e.append(complex(0.0, +sqrt(2.0*sqrt(7.0)-1.0)))
        e.append(-e[-1])
        e.append(complex(0.0, +2.0))
        e.append(-e[-1])
        self.eig_vals = e

    def test_basic_matrix(self):

        """This test is rather monolithic, but at least it implements
        a concrete example that we can compare with our earlier
        computations.  It also tests the mutual-inverse character of
        the equi-to-diag and diag-to-equi transformations."""

        tolerance = 5.0e-15
        eig = self.diag.compute_eigen_system(self.h_2, tolerance)
        self.diag.compute_diagonal_change()

        eq_type = eig.get_equilibrium_type()
        self.assertEquals(eq_type, self.eq_type)

        eigs = [pair.val for pair in eig.get_raw_eigen_value_vector_pairs()]
        for actual, expected in zip(eigs, self.eig_vals):
            self.assert_(abs(actual-expected) < tolerance, (actual, expected))

        mat = self.diag.get_matrix_diag_to_equi()
        assert self.diag.matrix_is_symplectic(mat)

        sub_diag_into_equi = self.diag.matrix_as_vector_of_row_polynomials(mat)
        mat_inv = LinearAlgebra.inverse(MLab.array(mat))
        sub_equi_into_diag = self.diag.matrix_as_vector_of_row_polynomials(mat_inv)
        h_diag_2 = self.h_2.substitute(sub_diag_into_equi)
        h_2_inv = h_diag_2.substitute(sub_equi_into_diag)
        self.assert_(h_2_inv) #non-zero
        self.assert_(not h_2_inv.is_constant())
        self.assert_(self.lie.is_isograde(h_2_inv, 2))
        self.assert_((self.h_2-h_2_inv).l1_norm() < 1.0e-14)
        comp = Complexifier(self.diag.get_lie_algebra(), eq_type)
        sub_complex_into_real = comp.calc_sub_complex_into_real()
        h_comp_2 = h_diag_2.substitute(sub_complex_into_real)
        h_comp_2 = h_comp_2.with_small_coeffs_removed(tolerance)
        self.assert_(self.lie.is_diagonal_polynomial(h_comp_2))
Beispiel #2
0
class CanonicalChange(unittest.TestCase):
    def setUp(self):
        """Set up an example from Hill's equations."""

        x_2 = Powers((2, 0, 0, 0, 0, 0))
        px2 = Powers((0, 2, 0, 0, 0, 0))
        y_2 = Powers((0, 0, 2, 0, 0, 0))
        py2 = Powers((0, 0, 0, 2, 0, 0))
        z_2 = Powers((0, 0, 0, 0, 2, 0))
        pz2 = Powers((0, 0, 0, 0, 0, 2))
        xpy = Powers((1, 0, 0, 1, 0, 0))
        ypx = Powers((0, 1, 1, 0, 0, 0))
        terms = {
            px2: 0.5,
            py2: 0.5,
            pz2: 0.5,
            xpy: -1.0,
            ypx: 1.0,
            x_2: -4.0,
            y_2: 2.0,
            z_2: 2.0
        }
        assert len(terms) == 8

        self.h_2 = Polynomial(6, terms=terms)
        self.lie = LieAlgebra(3)
        self.diag = Diagonalizer(self.lie)
        self.eq_type = 'scc'
        e = []
        e.append(+sqrt(2.0 * sqrt(7.0) + 1.0))
        e.append(-e[-1])
        e.append(complex(0.0, +sqrt(2.0 * sqrt(7.0) - 1.0)))
        e.append(-e[-1])
        e.append(complex(0.0, +2.0))
        e.append(-e[-1])
        self.eig_vals = e

    def test_basic_matrix(self):
        """This test is rather monolithic, but at least it implements
        a concrete example that we can compare with our earlier
        computations.  It also tests the mutual-inverse character of
        the equi-to-diag and diag-to-equi transformations."""

        tolerance = 5.0e-15
        eig = self.diag.compute_eigen_system(self.h_2, tolerance)
        self.diag.compute_diagonal_change()

        eq_type = eig.get_equilibrium_type()
        self.assertEquals(eq_type, self.eq_type)

        eigs = [pair.val for pair in eig.get_raw_eigen_value_vector_pairs()]
        for actual, expected in zip(eigs, self.eig_vals):
            self.assert_(
                abs(actual - expected) < tolerance, (actual, expected))

        mat = self.diag.get_matrix_diag_to_equi()
        assert self.diag.matrix_is_symplectic(mat)

        sub_diag_into_equi = self.diag.matrix_as_vector_of_row_polynomials(mat)
        mat_inv = LinearAlgebra.inverse(MLab.array(mat))
        sub_equi_into_diag = self.diag.matrix_as_vector_of_row_polynomials(
            mat_inv)
        h_diag_2 = self.h_2.substitute(sub_diag_into_equi)
        h_2_inv = h_diag_2.substitute(sub_equi_into_diag)
        self.assert_(h_2_inv)  #non-zero
        self.assert_(not h_2_inv.is_constant())
        self.assert_(self.lie.is_isograde(h_2_inv, 2))
        self.assert_((self.h_2 - h_2_inv).l1_norm() < 1.0e-14)
        comp = Complexifier(self.diag.get_lie_algebra(), eq_type)
        sub_complex_into_real = comp.calc_sub_complex_into_real()
        h_comp_2 = h_diag_2.substitute(sub_complex_into_real)
        h_comp_2 = h_comp_2.with_small_coeffs_removed(tolerance)
        self.assert_(self.lie.is_diagonal_polynomial(h_comp_2))