code: def test_legendre_val():
      """Test Legendre polynomial (derivative) equivalence."""
      rng = np.random.RandomState(0)
      # check table equiv
      xs = np.linspace(-1.0, 1.0, 1000)
      n_terms = 100
  
      # True, numpy
      vals_np = legendre.legvander(xs, n_terms - 1)
  
      # Table approximation
      for nc, interp in zip([100, 50], ["nearest", "linear"]):
          lut, n_fact = _get_legen_table(
              "eeg", n_coeff=nc, force_calc=True
          )
          lut_fun = interp1d(
              np.linspace(-1, 1, lut.shape[0]), lut, interp, axis=0
          )
          vals_i = lut_fun(xs)
          # Need a "1:" here because we omit the first coefficient in our table!
          assert_allclose(
              vals_np[:, 1 : vals_i.shape[1] + 1],
              vals_i,
              rtol=1e-2,
              atol=5e-3,
          )
  
          # Now let's look at our sums
          ctheta = rng.rand(20, 30) * 2.0 - 1.0
          beta = rng.rand(20, 30) * 0.8
          c1 = _comp_sum_eeg(
              beta.flatten(), ctheta.flatten(), lut_fun, n_fact
          )
          c1.shape = beta.shape
  
          # compare to numpy
          n = np.arange(1, n_terms, dtype=float)[
              :, np.newaxis, np.newaxis
          ]
          coeffs = np.zeros((n_terms,) + beta.shape)
          coeffs[1:] = (
              np.cumprod([beta] * (n_terms - 1), axis=0)
              * (2.0 * n + 1.0)
              * (2.0 * n + 1.0)
              / n
          )
          # can't use tensor=False here b/c it isn't in old numpy
          c2 = np.empty((20, 30))
          for ci1 in range(20):
              for ci2 in range(30):
                  c2[ci1, ci2] = legendre.legval(
                      ctheta[ci1, ci2], coeffs[:, ci1, ci2]
                  )
          assert_allclose(c1, c2, 1e-2, 1e-3)  # close enough...
  
      # compare fast and slow for MEG
      ctheta = rng.rand(20 * 30) * 2.0 - 1.0
      beta = rng.rand(20 * 30) * 0.8
      lut, n_fact = _get_legen_table("meg", n_coeff=10, force_calc=True)
      fun = interp1d(
          np.linspace(-1, 1, lut.shape[0]), lut, "nearest", axis=0
      )
      coeffs = _comp_sums_meg(beta, ctheta, fun, n_fact, False)
      lut, n_fact = _get_legen_table("meg", n_coeff=20, force_calc=True)
      fun = interp1d(
          np.linspace(-1, 1, lut.shape[0]), lut, "linear", axis=0
      )
      coeffs = _comp_sums_meg(beta, ctheta, fun, n_fact, False)
  
imports: from os import path
  from pathlib import Path
  import numpy
  import pytest
  from numpy.polynomial import legendre
  from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal
  from scipy.interpolate import interp1d
  import mne
  from mne import Epochs, make_fixed_length_events, pick_types, read_evokeds
  from mne.datasets import testing
  from mne.forward import _make_surface_mapping, make_field_map
  from mne.forward._field_interpolation import _setup_dots
  from mne.forward._lead_dots import _comp_sum_eeg, _comp_sums_meg, _do_cross_dots, _get_legen_table
  from mne.forward._make_forward import _create_meg_coils
  from mne.io import read_raw_fif
  from mne.surface import get_head_surf, get_meg_helmet_surf
label_desc: Use image node_id 2 for calling a global function with example usage: test_legendre_val() without return types