import utils
import numpy as np
import math


def fillRecursive(nMax,eMax):
    D = np.zeros((nMax+1,eMax+1), dtype=np.float)
    for n in range(2,nMax+1):
        for e in range(1,eMax+1):
            if e<n-1:
                D[n,e]=0
            elif n==2:
                D[n,e]=1
            elif n==3:
                D[n,e] = 3*D[3,e-1]+1
            else:
                D[n,e] = (2*n-3)*D[n,e-1]+2*D[n-1,e-1]
    #printMatrix(D)
    utils.saveMatrix(D,'D.csv')

def dFormula(n,e):
    res = 0.0
    for i in range(0,n-1):
        res += math.pow(-1,i+n) / (2*math.factorial(i)*math.factorial(n-2-i)) * math.pow(2*i + 1,e-1)
    return res

def fillFormula(nMax, eMax):
    D = np.zeros((nMax+1,eMax+1), dtype=np.float)
    for n in range(2,nMax+1):
        for e in range(1,eMax+1):
            if e<n-1:
                D[n,e]=0
            elif n==2:
                D[n,e]=1
            else:
                D[n,e] = dFormula(n,e)
    utils.saveMatrix(D,'D_formula.csv')
