def testOrderZeroDegreeZero(self):
     """Tests the spherical harmonics of order zero and degree zero."""
     num_theta = 6
     num_phi = 4
     expected = (1.0 / jnp.sqrt(4.0 * math.pi) * jnp.ones(
         (1, 1, num_theta, num_phi)))
     theta = jnp.linspace(0, math.pi, num_theta)
     phi = jnp.linspace(0, 2.0 * math.pi, num_phi)
     sph_harm = spherical_harmonics.SphericalHarmonics(l_max=0,
                                                       theta=theta,
                                                       phi=phi)
     actual = jnp.real(sph_harm.harmonics_nonnegative_order())
     np.testing.assert_allclose(actual, expected, rtol=1.1e-7, atol=3e-8)
    def testOrderOneDegreeZero(self):
        """Tests the spherical harmonics of order one and degree zero."""
        num_theta = 4
        num_phi = 6
        theta = jnp.linspace(0, math.pi, num_theta)
        phi = jnp.linspace(0, 2.0 * math.pi, num_phi)

        expected = jnp.sqrt(3.0 / (4.0 * math.pi)) * jnp.outer(
            jnp.cos(theta), jnp.ones_like(phi))
        sph_harm = spherical_harmonics.SphericalHarmonics(l_max=1,
                                                          theta=theta,
                                                          phi=phi)
        actual = jnp.real(sph_harm.harmonics_nonnegative_order()[1, 0, :, :])
        np.testing.assert_allclose(actual, expected, rtol=7e-8, atol=1.5e-8)
    def testOrderOneDegreeOne(self):
        """Tests the spherical harmonics of order one and degree one."""
        num_theta = 7
        num_phi = 8
        theta = jnp.linspace(0, math.pi, num_theta)
        phi = jnp.linspace(0, 2.0 * math.pi, num_phi)

        expected = -1.0 / 2.0 * jnp.sqrt(3.0 / (2.0 * math.pi)) * jnp.outer(
            jnp.sin(theta), jnp.exp(1j * phi))
        sph_harm = spherical_harmonics.SphericalHarmonics(l_max=1,
                                                          theta=theta,
                                                          phi=phi)
        actual = sph_harm.harmonics_nonnegative_order()[1, 1, :, :]
        np.testing.assert_allclose(jnp.abs(actual),
                                   jnp.abs(expected),
                                   rtol=1e-8,
                                   atol=6e-8)
    def testAgainstScipySpecialSphHarmNonnegativeOrder(self):
        """Tests the accuracy against scipy.special.sph_harm."""
        l_max = 64
        num_theta = 128
        num_phi = 128
        theta = jnp.linspace(0, math.pi, num_theta)
        phi = jnp.linspace(0, 2.0 * math.pi, num_phi)

        expected = _compute_spherical_harmonics(l_max=l_max,
                                                theta=theta,
                                                phi=phi)
        sph_harm = spherical_harmonics.SphericalHarmonics(l_max=l_max,
                                                          theta=theta,
                                                          phi=phi)
        actual = sph_harm.harmonics_nonnegative_order()
        np.testing.assert_allclose(jnp.abs(actual),
                                   jnp.abs(expected),
                                   rtol=1e-8,
                                   atol=9e-5)