def test_BDNmat(S1S2):
    S1, S2 = S1S2

    f_hat = np.zeros(N)
    fj = np.random.random(N)
    f_hat = S2.fst(fj, f_hat)
    fj = S2.ifst(f_hat, fj)

    if S1.__class__.__name__ == "ShenNeumannBasis":
        B = BNDmat(np.arange(N).astype(np.float), S1.quad)
    else:
        B = BDNmat(np.arange(N).astype(np.float), S1.quad)

    f_hat = S2.fst(fj, f_hat)
    u2 = B.matvec(f_hat)
    u0 = np.zeros(N)
    u0 = S1.fastShenScalar(fj, u0)

    assert np.allclose(u0, u2)

    # Multidimensional version
    fj = fj.repeat(16).reshape((N, 4, 4)) + 1j * fj.repeat(16).reshape((N, 4, 4))
    f_hat = f_hat.repeat(16).reshape((N, 4, 4)) + 1j * f_hat.repeat(16).reshape((N, 4, 4))

    u0 = np.zeros((N, 4, 4), dtype=np.complex)
    u0 = S1.fastShenScalar(fj, u0)
    u2 = B.matvec(f_hat)
    assert np.linalg.norm(u2 - u0) / (N * 16) < 1e-12
def test_Mult_Div():

    SD = ShenDirichletBasis("GC")
    SN = ShenDirichletBasis("GC")

    Cm = CNDmat(np.arange(N).astype(np.float))
    Bm = BNDmat(np.arange(N).astype(np.float), "GC")

    uk = np.random.randn((N)) + np.random.randn((N)) * 1j
    vk = np.random.randn((N)) + np.random.randn((N)) * 1j
    wk = np.random.randn((N)) + np.random.randn((N)) * 1j

    b = np.zeros(N, dtype=np.complex)
    uk0 = np.zeros(N, dtype=np.complex)
    vk0 = np.zeros(N, dtype=np.complex)
    wk0 = np.zeros(N, dtype=np.complex)

    uk0 = SD.fst(uk, uk0)
    uk = SD.ifst(uk0, uk)
    uk0 = SD.fst(uk, uk0)
    vk0 = SD.fst(vk, vk0)
    vk = SD.ifst(vk0, vk)
    vk0 = SD.fst(vk, vk0)
    wk0 = SD.fst(wk, wk0)
    wk = SD.ifst(wk0, wk)
    wk0 = SD.fst(wk, wk0)

    SFTc.Mult_Div_1D(N, 7, 7, uk0[: N - 2], vk0[: N - 2], wk0[: N - 2], b[1 : N - 2])

    uu = Cm.matvec(uk0)
    uu += 1j * 7 * Bm.matvec(vk0) + 1j * 7 * Bm.matvec(wk0)

    # from IPython import embed; embed()
    assert np.allclose(uu, b)

    uk0 = uk0.repeat(4 * 4).reshape((N, 4, 4)) + 1j * uk0.repeat(4 * 4).reshape((N, 4, 4))
    vk0 = vk0.repeat(4 * 4).reshape((N, 4, 4)) + 1j * vk0.repeat(4 * 4).reshape((N, 4, 4))
    wk0 = wk0.repeat(4 * 4).reshape((N, 4, 4)) + 1j * wk0.repeat(4 * 4).reshape((N, 4, 4))
    b = np.zeros((N, 4, 4), dtype=np.complex)
    m = np.zeros((4, 4)) + 7
    n = np.zeros((4, 4)) + 7
    SFTc.Mult_Div_3D(N, m, n, uk0[: N - 2], vk0[: N - 2], wk0[: N - 2], b[1 : N - 2])

    uu = Cm.matvec(uk0)
    uu += 1j * 7 * Bm.matvec(vk0) + 1j * 7 * Bm.matvec(wk0)

    assert np.allclose(uu, b)