def test_eval_splines():

    from interpolation.splines.eval_splines import eval_linear, eval_cubic
    from interpolation.multilinear.mlinterp import mlinterp
    import numpy as np

    N = 1000
    K = 100
    d = 2
    grid = ((0.0, 1.0, K), ) * d
    grid_nu = ((0.0, 1.0, K), np.linspace(0, 1, K))
    n_x = 6

    V = np.random.random((K, K))
    VV = np.random.random((K, K, n_x))

    C = np.random.random((K + 2, K + 2))
    CC = np.random.random((K + 2, K + 2, n_x))

    points = np.random.random((N, 2))

    out = np.random.random((N))
    Cout = np.random.random((N, n_x))

    eval_linear(grid, V, points, out)
    # eval_cubic(grid, C, points[0,:], out[0,0])
    eval_linear(grid, VV, points, Cout)
    eval_linear(grid, VV, points[0, :], Cout[0, :])

    mlinterp(grid, V, points)
    eval_linear(grid, V, points)
    eval_linear(grid, V, points)
    eval_linear(grid, V, points, out)

    print("OK 2")
    eval_linear(grid, V, points[0, :])
    res_0 = eval_linear(grid, VV, points)
    res_1 = eval_linear(grid, VV, points[0, :])

    #nonuniform grid:
    res_0_bis = eval_linear(grid_nu, VV, points)
    res_1_bis = eval_linear(grid_nu, VV, points[0, :])

    assert abs(res_0 - res_0_bis).max() < 1e-10
    assert abs(res_1 - res_1_bis).max() < 1e-10

    print("OK 3")
    eval_cubic(grid, C, points, out)
    # eval_cubic(grid, C, points[0,:], out[0,0])
    eval_cubic(grid, CC, points, Cout)
    eval_cubic(grid, CC, points[0, :], Cout[0, :])

    print("OK 4")
    eval_cubic(grid, C, points)
    eval_cubic(grid, C, points[0, :])
    eval_cubic(grid, CC, points)
    eval_cubic(grid, CC, points[0, :])

    print("OK 5")
grid_u = ((0.0, 1.0, K), ) * d

from interpolation.cartesian import mlinspace
s = mlinspace(a, b, n)

f = lambda x: (x**2).sum(axis=1)

x = f(s)
v = x.reshape(n)

from interpolation.splines.eval_splines import eval_linear

pp = np.linspace(-0.5, 1.5, 100)[:, None]

from interpolation.splines.option_types import options
xx_ = eval_linear(grid_u, v, pp)
xx_cst = eval_linear(grid_u, v, pp, options.CONSTANT)
xx_lin = eval_linear(grid_u, v, pp, options.LINEAR)
xx_nea = eval_linear(grid_u, v, pp, options.NEAREST)

yy_ = np.array([eval_linear(grid_u, v, p_) for p_ in pp])
yy_cst = np.array([eval_linear(grid_u, v, p_, options.CONSTANT) for p_ in pp])
yy_lin = np.array([eval_linear(grid_u, v, p_, options.LINEAR) for p_ in pp])
yy_nea = np.array([eval_linear(grid_u, v, p_, options.NEAREST) for p_ in pp])

zz_ = xx_.copy() * 0
zz_cst = xx_cst.copy() * 0
zz_lin = xx_lin.copy() * 0
zz_nea = xx_nea.copy() * 0

print("Inplace")
def test_multilinear_extrap():

    import numpy as np

    N=100000
    K = 10
    d = 1
    a = np.array([0.0]*d)
    b = np.array([1.0]*d)
    n = np.array([K]*d)
    grid_u = ((0.0,1.0,K),)*d

    from interpolation.cartesian import mlinspace
    s = mlinspace(a,b,n)

    f = lambda x: (x**2).sum(axis=1)

    x = f(s)
    v = x.reshape(n)

    from interpolation.splines.eval_splines import eval_linear

    pp = np.linspace(-0.5, 1.5, 100)[:,None]

    from interpolation.splines.option_types import options
    xx_ = eval_linear(grid_u, v, pp)
    xx_cst = eval_linear(grid_u, v, pp, options.CONSTANT)
    xx_lin = eval_linear(grid_u, v, pp, options.LINEAR)
    xx_nea = eval_linear(grid_u, v, pp, options.NEAREST)

    yy_ = np.array([eval_linear(grid_u, v, p_) for p_ in pp])
    yy_cst = np.array([eval_linear(grid_u, v, p_, options.CONSTANT) for p_ in pp])
    yy_lin = np.array([eval_linear(grid_u, v, p_, options.LINEAR) for p_ in pp])
    yy_nea = np.array([eval_linear(grid_u, v, p_, options.NEAREST) for p_ in pp])

    zz_ = xx_.copy()*0
    zz_cst = xx_cst.copy()*0
    zz_lin = xx_lin.copy()*0
    zz_nea = xx_nea.copy()*0

    print("Inplace")
    eval_linear(grid_u, v, pp, zz_)
    eval_linear(grid_u, v, pp, zz_cst, options.CONSTANT)
    eval_linear(grid_u, v, pp, zz_lin, options.LINEAR)
    eval_linear(grid_u, v, pp, zz_nea, options.NEAREST)

    assert ((abs(xx_-yy_)+(abs(xx_-zz_))).max() <1e-10)
    assert ((abs(xx_cst-yy_cst)+(abs(xx_cst-zz_cst))).max()<1e-10)
    assert ((abs(xx_lin-yy_lin)+(abs(xx_lin-zz_lin))).max()<1e-10)
    assert ((abs(xx_nea-yy_nea)+(abs(xx_nea-zz_nea))).max()<1e-10)