SM-7-Unimodal

4142 days ago by MathFest

This function determines if log-concavity holds up to a certain $P(n)$. Requires function $P$
def logConcave(n): M = P(n) L = [0,0,0] for n in range(4,n+1): for k in range(1,n-1): if (M[n-1,k]^2<M[n-1,k-1]*M[n-1,k+1]): L.append(n-(k+1)) break return L 
       
This function finds the values of $P(n,k)$ from $n=$start to $n=$finish. The function determines if $P_n$ is unimodal and returns the $k$ value for the first appearance of the mode. Requires function $P$.
def improvedunimodal(input,start,finish): L = input modes = [] for row in range(start-1,finish): #First, delete unnecessary entries for i in range(floor((row)/2)): del(L[i][0]) #Then, we create the next row of entries if (row!=0): #The first column is always a 1 L[0].append(1) #The 2 through floor((row-1)/2)+1 columns are calculated using both summands for column in range(1,floor((row-1)/2)+1): L[column].append(L[column-1][column-1]+L[column][0]) #The floor((row-1)/2)+2 through n-1 columns are calculated using the P(n-1,k-1) only for column in range(floor((row-1)/2)+1,row): L[column].append(L[column-1][row-column]) #The last column is always a 1 if (mod(row,2)==0): L[floor((row-1)/2)+1][len(L[floor((row-1)/2)+1])-1]=L[floor((row-1)/2)][row-floor((row-1)/2)-2] L.append([1]) #Then we find our mode value = 0 mode = 0 increasing = 1 center = (row-1)/2 maximum = ceil(row/2)+1 for column in range(row+1): index = maximum-floor(abs(center-column))-1 if (increasing == 1): if (L[column][index] < value): increasing = 0 #This means all values should decrease from here on out. if L[column][index]>value: mode = column value = L[column][index] else: if (L[column][index] > value): #If a row is not unimodal, the function immediately terminates #and displays the n-value which is not unimodal. print 'n=',n+1,'is not unimodal.' return 0 value = L[column][index] modes.append(mode+1) return [L,modes] 
       
The function below returns a $n\times n$ matrix in which each entry is $P(n,k)$
def P(howfar): M = matrix(ZZ,howfar,howfar) for n in range(howfar): for k in range(n+1): if (k==0 or n==k): M[n,k]=1 else: M[n,k]=(M[n-1,k-1]+M[n-k-1,k]) return M 
       
This function takes a matrix as input and returns a list of the positions of each row's maximal element. mode(P(a)) calculates the rank of the mode of $P_n$ for $n<=a$. Requires function $P$.
def mode(M): List = [] for n in range(M.nrows()): maxval = 0 for k in range(n+1): if (M[n,k] > maxval): max = k+1 maxval = M[n,k] List.append(max) return List 
       
This function ensures that each row of the $P(n,k)$ matrix is unimodal. Requires $P$. If each $n$ is unimodal it returns a value of 1.
def unimodal(M): for n in range(M.nrows()): increasing = 1; #This means that values should start out increasing. value = 0 for k in range(n+1): if (increasing == 1): if (M[n,k] < value): increasing = 0 #This means all values should decrease from here on out. value = M[n,k] else: if (M[n,k] > value): #If a row is not unimodal, the function immediately terminates #and displays the n-value which is not unimodal. print 'n=',n+1,'is not unimodal.' return 0 value = M[n,k] return 1 
       
An example of the above functions.
#Find the matrix for up to n=10. This function will only print the matrix if n<20 print "The P(n,k) matrix is" print P(10) #Find the mode for all rows up to n=10 print "The mode for the rows for the P(n,k) matrix is" print mode(P(10)) #Check that P(n,k) is unimodal up to n=10 print "If P(n,k) is unimodal up to n=10 print 1" print unimodal(P(10)) #Check log-concavity print "Checking log-concavity for P(n,k)" print logConcave(10) 
       
The P(n,k) matrix is
[1 0 0 0 0 0 0 0 0 0]
[1 1 0 0 0 0 0 0 0 0]
[1 1 1 0 0 0 0 0 0 0]
[1 2 1 1 0 0 0 0 0 0]
[1 2 2 1 1 0 0 0 0 0]
[1 3 3 2 1 1 0 0 0 0]
[1 3 4 3 2 1 1 0 0 0]
[1 4 5 5 3 2 1 1 0 0]
[1 4 7 6 5 3 2 1 1 0]
[1 5 8 9 7 5 3 2 1 1]
The mode for the rows for the P(n,k) matrix is
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4]
If P(n,k) is unimodal up to n=10 print 1
1
Checking log-concavity for P(n,k)
[0, 0, 0, 1, 1, 1, 1, 3, 3, 3]
The P(n,k) matrix is
[1 0 0 0 0 0 0 0 0 0]
[1 1 0 0 0 0 0 0 0 0]
[1 1 1 0 0 0 0 0 0 0]
[1 2 1 1 0 0 0 0 0 0]
[1 2 2 1 1 0 0 0 0 0]
[1 3 3 2 1 1 0 0 0 0]
[1 3 4 3 2 1 1 0 0 0]
[1 4 5 5 3 2 1 1 0 0]
[1 4 7 6 5 3 2 1 1 0]
[1 5 8 9 7 5 3 2 1 1]
The mode for the rows for the P(n,k) matrix is
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4]
If P(n,k) is unimodal up to n=10 print 1
1
Checking log-concavity for P(n,k)
[0, 0, 0, 1, 1, 1, 1, 3, 3, 3]