Ask Your Question

Revision history [back]

From @rburing's answer... slightly modified since taking coefficients in the polynomial ring somehow only gives the non-zero coefficients:

t = var('t')
R = ZZ[t]
def is_unimodal(p):
coeffs = [R(p).constant_coefficient()]
for i in range(1,p.degree(t)+1):
    coeffs = coeffs + [SR(p).coefficient(t^i)]
max_coeff_idx = max(range(len(coeffs)), key=lambda i: coeffs[i])
return all(coeffs[i] <= coeffs[i+1] for i in range(max_coeff_idx)) and \
       all(coeffs[i] >= coeffs[i+1] for i in range(max_coeff_idx, len(coeffs) - 1))

From @rburing's answer... slightly modified since taking coefficients in the polynomial ring somehow only gives the non-zero coefficients:

t = var('t')
R = ZZ[t]
def is_unimodal(p):
 coeffs = [R(p).constant_coefficient()]
 for i in range(1,p.degree(t)+1):
    coeffs = coeffs + [SR(p).coefficient(t^i)]
 max_coeff_idx = max(range(len(coeffs)), key=lambda i: coeffs[i])
 return all(coeffs[i] <= coeffs[i+1] for i in range(max_coeff_idx)) and \
       all(coeffs[i] >= coeffs[i+1] for i in range(max_coeff_idx, len(coeffs) - 1))