Skip to content

UniformGrid weight="Fourier2" integrates everything to about zero #349

Open

Description

@SajalDevX

UniformGrid(..., weight="Fourier2") gives weights that are antisymmetric along each axis, so they add up to roughly zero and any integral comes out near 0.

Small example: a Gaussian centred in a cube that is large enough for it to vanish on the boundary (exact value pi**1.5 = 5.5683):

import numpy as np
from grid.cubic import UniformGrid

for M in [11, 21, 41]:
    h = 10 / (M - 1)
    grid = UniformGrid(np.array([-5.0, -5.0, -5.0]), np.eye(3) * h, np.array([M, M, M]), weight="Fourier2")
    print(M, grid.integrate(np.exp(-np.sum(grid.points**2, axis=1))), np.pi**1.5)

On master:

11 -3.9569722993014565e-06 5.568327996831708
21 1.5712268105027362e-16 5.568327996831708
41 1.7702371968570676e-17 5.568327996831708

The weights come from the rectangle rule for sine series with interior points x_i = (2i - 1) / (2M) on [0, 1] (Boyd):

w_i = 2 sin((i - 1/2) pi) sin^2(M pi / 2) / (M^2 pi) + 4 / (M pi) * sum_{p=1}^{M-1} sin(p pi x_i) sin^2(p pi / 2) / p

In _fourier2 the sine argument is built as (2i - 1) / M * p * pi, i.e. x_i is missing the factor 1/2, which is what makes the weights antisymmetric. The weights are also scaled by the "alternative" volume (M - 1) h per axis, but the points x_i above sit half a spacing in from each end of an interval of length M h, so the scale should be the plain grid volume. With only the first change the integral still converges like ((M - 1) / M)**3; with both it converges spectrally:

11 5.582472297052459 5.568327996831708
21 5.568326882604342 5.568327996831708
41 5.568327996822337 5.568327996831708

The existing test_fourier2_weights_are_correct re-implements the same formula with the same missing factor, so it does not catch this. I have a fix (two lines in cubic.py, plus the docstring) and a test that checks the Gaussian integral above. Happy to open a PR if that's fine.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions