import sys
sys.path.append("/home/gabor/github/butools/Python")
import math
import numpy as np
import numpy.matlib as ml
import matplotlib.pyplot as plt
import butools
from butools.utils import *
from butools.ph import *
from butools.dph import *
from butools.map import *
from butools.moments import *
from butools.reptrans import*
from butools.mc import *
from butools.dmap import *
from butools.trace import *
from butools.mam import *
from butools.queues import *
from butools.fitting import *
from contextlib import redirect_stdout
import os


print('---BuTools: Moments package test file---')
print('Enable the verbose messages with the BuToolsVerbose flag')
butools.verbose = True
print('Enable input parameter checking with the BuToolsCheckInput flag')
butools.checkInput = True
np.set_printoptions(precision=5,linewidth=1024)
print('========================================')
print('Testing BuTools function NormMomsFromMoms')
print('Input:')
print('------')
M = [1.2, 5., 38., 495., 9215.]
print('M = ')
print(M)
print('Test:')
print('-----')
print('nmoms = NormMomsFromMoms(M):')
nmoms = NormMomsFromMoms(M)
print('nmoms = ')
print(nmoms)
print('moms = MomsFromNormMoms(nmoms):')
moms = MomsFromNormMoms(nmoms)
print('moms = ')
print(moms)
print('err = la.norm(np.array(moms)-np.array(M)):')
err = la.norm(np.array(moms)-np.array(M))
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function MomsFromNormMoms')
print('Input:')
print('------')
M = [1.2, 5., 38., 495., 9215.]
print('M = ')
print(M)
print('Test:')
print('-----')
print('nmoms = NormMomsFromMoms(M):')
nmoms = NormMomsFromMoms(M)
print('nmoms = ')
print(nmoms)
print('moms = MomsFromNormMoms(nmoms):')
moms = MomsFromNormMoms(nmoms)
print('moms = ')
print(moms)
print('err = la.norm(np.array(moms)-np.array(M)):')
err = la.norm(np.array(moms)-np.array(M))
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function ReducedMomsFromMoms')
print('Input:')
print('------')
M = [1.2, 5., 38., 495., 9215.]
print('M = ')
print(M)
print('Test:')
print('-----')
print('rmoms = ReducedMomsFromMoms(M):')
rmoms = ReducedMomsFromMoms(M)
print('rmoms = ')
print(rmoms)
print('moms = MomsFromReducedMoms(rmoms):')
moms = MomsFromReducedMoms(rmoms)
print('moms = ')
print(moms)
print('err = la.norm(np.array(moms)-np.array(M)):')
err = la.norm(np.array(moms)-np.array(M))
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function MomsFromReducedMoms')
print('Input:')
print('------')
M = [1.2, 5., 38., 495., 9215.]
print('M = ')
print(M)
print('Test:')
print('-----')
print('rmoms = ReducedMomsFromMoms(M):')
rmoms = ReducedMomsFromMoms(M)
print('rmoms = ')
print(rmoms)
print('moms = MomsFromReducedMoms(rmoms):')
moms = MomsFromReducedMoms(rmoms)
print('moms = ')
print(moms)
print('err = la.norm(np.array(moms)-np.array(M)):')
err = la.norm(np.array(moms)-np.array(M))
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function FactorialMomsFromMoms')
print('Input:')
print('------')
M = [1.3, 2.4, 6.03, 20.5, 89.5, 474.9]
print('M = ')
print(M)
print('Test:')
print('-----')
print('fmoms = FactorialMomsFromMoms(M):')
fmoms = FactorialMomsFromMoms(M)
print('fmoms = ')
print(fmoms)
print('moms = MomsFromFactorialMoms(fmoms):')
moms = MomsFromFactorialMoms(fmoms)
print('moms = ')
print(moms)
print('err = la.norm(np.array(moms)-np.array(M)):')
err = la.norm(np.array(moms)-np.array(M))
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function MomsFromFactorialMoms')
print('Input:')
print('------')
M = [1.3, 2.4, 6.03, 20.5, 89.5, 474.9]
print('M = ')
print(M)
print('Test:')
print('-----')
print('fmoms = FactorialMomsFromMoms(M):')
fmoms = FactorialMomsFromMoms(M)
print('fmoms = ')
print(fmoms)
print('moms = MomsFromFactorialMoms(fmoms):')
moms = MomsFromFactorialMoms(fmoms)
print('moms = ')
print(moms)
print('err = la.norm(np.array(moms)-np.array(M)):')
err = la.norm(np.array(moms)-np.array(M))
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function JFactorialMomsFromJMoms')
print('Input:')
print('------')
MM = ml.matrix([[0.7, 2., 3., 4.],[5., 6., 7., 8.],[9., 10., 11., 12.]])
print('MM = ')
print(MM)
print('Test:')
print('-----')
print('JFmoms = JFactorialMomsFromJMoms(MM):')
JFmoms = JFactorialMomsFromJMoms(MM)
print('JFmoms = ')
print(JFmoms)
print('Jmoms = JMomsFromJFactorialMoms(JFmoms):')
Jmoms = JMomsFromJFactorialMoms(JFmoms)
print('Jmoms = ')
print(Jmoms)
print('err = la.norm(np.array(Jmoms)-MM):')
err = la.norm(np.array(Jmoms)-MM)
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function JMomsFromJFactorialMoms')
print('Input:')
print('------')
MM = ml.matrix([[0.7, 2., 3., 4.],[5., 6., 7., 8.],[9., 10., 11., 12.]])
print('MM = ')
print(MM)
print('Test:')
print('-----')
print('JFmoms = JFactorialMomsFromJMoms(MM):')
JFmoms = JFactorialMomsFromJMoms(MM)
print('JFmoms = ')
print(JFmoms)
print('Jmoms = JMomsFromJFactorialMoms(JFmoms):')
Jmoms = JMomsFromJFactorialMoms(JFmoms)
print('Jmoms = ')
print(Jmoms)
print('err = la.norm(np.array(Jmoms)-MM):')
err = la.norm(np.array(Jmoms)-MM)
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function HankelMomsFromMoms')
print('Input:')
print('------')
M = [1.3, 2.4, 6.03, 20.5, 89.5, 474.9]
print('M = ')
print(M)
print('Test:')
print('-----')
print('hmoms = HankelMomsFromMoms(M):')
hmoms = HankelMomsFromMoms(M)
print('hmoms = ')
print(hmoms)
print('moms = MomsFromHankelMoms(hmoms):')
moms = MomsFromHankelMoms(hmoms)
print('moms = ')
print(moms)
print('err = la.norm(np.array(moms)-np.array(M)):')
err = la.norm(np.array(moms)-np.array(M))
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function MomsFromHankelMoms')
print('Input:')
print('------')
M = [1.3, 2.4, 6.03, 20.5, 89.5, 474.9]
print('M = ')
print(M)
print('Test:')
print('-----')
print('hmoms = HankelMomsFromMoms(M):')
hmoms = HankelMomsFromMoms(M)
print('hmoms = ')
print(hmoms)
print('moms = MomsFromHankelMoms(hmoms):')
moms = MomsFromHankelMoms(hmoms)
print('moms = ')
print(moms)
print('err = la.norm(np.array(moms)-np.array(M)):')
err = la.norm(np.array(moms)-np.array(M))
print('err = ')
print(err)
assert err<10**-10, "Calling the moment conversion and its inverse did not give back the original moments!"
print('========================================')
print('Testing BuTools function CheckMoments')
print('Input:')
print('------')
M = [1.2, 5., 8., 29., 3412.]
print('M = ')
print(M)
print('Test:')
print('-----')
print('flag = CheckMoments(M):')
flag = CheckMoments(M)
print('flag = ')
print(flag)
assert flag==False, "CheckMoments did not recognize a valid moment sequence!"
print('Input:')
print('------')
M = [1.3, 2.4, 6.03, 20.5, 89.5]
print('M = ')
print(M)
print('Test:')
print('-----')
print('flag = CheckMoments(M):')
flag = CheckMoments(M)
print('flag = ')
print(flag)
assert flag==True, "CheckMoments did not recognize an invalid moment sequence!"

