import numpy as np
from pyspec import spectrum as spec

def test_parseval_1d(rtol=1.e-14, debug=False):
    """ Make sure 1D spectrum satisfied Parseval relation """
 
    N = np.array([10,11,21,500,871,1000,1001])
    dta = np.array([.25,1.,1.67,6.,20.])

    for dt in dta: 

        for n in N:
        
            phi = np.random.randn(n)
            spec_phi = spec.Spectrum(phi,dt)

            # var(P) from Fourier coefficients
            P_var_spec = spec_phi.var 

            # var(P) in physical space
            P_var_phys = phi.var()

            # relative error
            error = np.abs(P_var_phys - P_var_spec)/P_var_phys

            if debug:
                print "N = %i,  dt = %2.5f" %(n,dt)
                print "Variance in physical space: %5.16f" %P_var_phys
                print "Variance from spectrum: %5.16f" %P_var_spec
                print "error = %5.16f" %error
                print " "

            assert error<rtol, " *** 1D spectrum Does not satisfy Parseval's relation "

def test_parseval_2d(rtol=1.e-14, debug=False):
    """ Make sure 2D spectrum satisfied Parseval relation """
 
    N = np.array([10,11,21,500,871,1000,1001])
    dta = np.array([.25,1.,1.67,6.,20.])

    for dt in dta: 

        for n in N:
        
            phi = np.random.randn(n,n)
            phi = phi - phi.mean()
            spec_phi = spec.TWODimensional_spec(phi,dt,dt)

            # var(P) from Fourier coefficients
            P_var_spec = spec_phi.var 

            # var(P) in physical space
            P_var_phys = phi.var()

            # relative error
            error = np.abs(P_var_phys - P_var_spec)/P_var_phys

            if debug:
                print "N = %i,  dt = %2.5f" %(n,dt)
                print "Variance in physical space: %5.16f" %P_var_phys
                print "Variance from spectrum: %5.16f" %P_var_spec
                print "error = %5.16f" %error
                print " "

            assert error<rtol, " *** 2D spectrum Does not satisfy Parseval's relation "


if __name__ == "__main__":
    test_parseval_2d(debug=True)

import numpy as np
from pyspec import spectrum as spec

def test_parseval_1d(rtol=1.e-14, debug=False):
    """ Make sure 1D spectrum satisfied Parseval relation """
 
    N = np.array([10,11,21,500,871,1000,1001])
    dta = np.array([.25,1.,1.67,6.,20.])

    for dt in dta: 

        for n in N:
        
            phi = np.random.randn(n)
            spec_phi = spec.Spectrum(phi,dt)

            # var(P) from Fourier coefficients
            P_var_spec = spec_phi.var 

            # var(P) in physical space
            P_var_phys = phi.var()

            # relative error
            error = np.abs(P_var_phys - P_var_spec)/P_var_phys

            if debug:
                print "N = %i,  dt = %2.5f" %(n,dt)
                print "Variance in physical space: %5.16f" %P_var_phys
                print "Variance from spectrum: %5.16f" %P_var_spec
                print "error = %5.16f" %error
                print " "

            assert error<rtol, " *** 1D spectrum Does not satisfy Parseval's relation "

def test_parseval_2d(rtol=1.e-14, debug=False):
    """ Make sure 2D spectrum satisfied Parseval relation """
 
    N = np.array([10,11,21,500,871,1000,1001])
    dta = np.array([.25,1.,1.67,6.,20.])

    for dt in dta: 

        for n in N:
        
            phi = np.random.randn(n,n)
            phi = phi - phi.mean()
            spec_phi = spec.TWODimensional_spec(phi,dt,dt)

            # var(P) from Fourier coefficients
            P_var_spec = spec_phi.var 

            # var(P) in physical space
            P_var_phys = phi.var()

            # relative error
            error = np.abs(P_var_phys - P_var_spec)/P_var_phys

            if debug:
                print "N = %i,  dt = %2.5f" %(n,dt)
                print "Variance in physical space: %5.16f" %P_var_phys
                print "Variance from spectrum: %5.16f" %P_var_spec
                print "error = %5.16f" %error
                print " "

            assert error<rtol, " *** 2D spectrum Does not satisfy Parseval's relation "


if __name__ == "__main__":
    test_parseval_2d(debug=True)

Make sure 1D spectrum satisfied Parseval relation # var(P) from Fourier coefficients # var(P) in physical space # relative error Make sure 2D spectrum satisfied Parseval relation # var(P) from Fourier coefficients # var(P) in physical space # relative error