Ask Your Question

Revision history [back]

Here is a way:

def is_unimodal(p):
    coeffs = p.coefficients()
    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))

Example usage:

sage: R.<t> = ZZ[]
sage: p = R.random_element(degree=5); p
-t^5 - t^3 + 24*t^2 - t - 6
sage: p.coefficients()
[-6, -1, 24, -1, -1]
sage: is_unimodal(p)
True

Here is a way:

def is_unimodal(p):
    coeffs = p.coefficients()
    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))

Example usage:

sage: R.<t> = ZZ[]
sage: p = R.random_element(degree=5); R.random_element(degree=5).map_coefficients(abs); p
-t^5 - t^3 2*t^5 + 24*t^2 - t - 6
2*t^4 + 183*t^3 + 4*t
sage: p.coefficients()
[-6, -1, 24, -1, -1]
[4, 183, 2, 2]
sage: is_unimodal(p)
True

Here is a way:

def is_unimodal(p):
    coeffs = p.coefficients()
[c for c in p.coefficients() if c != 0]
    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))

Example usage:

sage: R.<t> = ZZ[]
sage: p = R.random_element(degree=5).map_coefficients(abs); p
2*t^5 + 2*t^4 + 183*t^3 + 4*t
sage: p.coefficients()
[4, 183, 2, 2]
sage: is_unimodal(p)
True

Here is a way:

def is_unimodal(p):
    coeffs = [c for c in p.coefficients() if c != 0]
p.coefficients(sparse=False)
    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))

Example usage:

sage: R.<t> = ZZ[]
sage: p = R.random_element(degree=5).map_coefficients(abs); p
2*t^5 + 2*t^4 + 183*t^3 + 4*t
sage: p.coefficients()
[4, 183, 2, 2]
sage: is_unimodal(p)
True

Here is a way:

def is_unimodal(p):
    coeffs = p.coefficients(sparse=False)
    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))

Example usage:

sage: R.<t> = ZZ[]
sage: p = R.random_element(degree=5).map_coefficients(abs); p
2*t^5 t^4 + 2*t^4 4*t^3 + 183*t^3 t^2 + 4*t
t + 1
sage: p.coefficients()
[4, 183, 2, 2]
p.coefficients(sparse=False)
[1, 1, 1, 4, 1]
sage: is_unimodal(p)
True

Here is a way:

def is_unimodal(p):
    coeffs = p.coefficients(sparse=False)
    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))

Example usage:

sage: R.<t> = ZZ[]
sage: p = R.random_element(degree=5).map_coefficients(abs); R.random_element(degree=4).map_coefficients(abs); p
t^4 + 4*t^3 + t^2 + t + 1
sage: p.coefficients(sparse=False)
[1, 1, 1, 4, 1]
sage: is_unimodal(p)
True