Open the context
Choose the project folder and name the files or behavior involved.
Aggregate up to 1000 log-values from minus 1e300 through 1e300 by shifting their maximum before exponentiation. Check empty input, repeated large values and very negative values, returning negative infinity only for empty input and rejecting Boolean, non-finite or out-of-range items.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from math import exp, fsum, log, inf
def log_sum_exp(values):
if type(values) not in (list, tuple) or len(values) > 1000:
raise ValueError("values.container")
values = tuple(values)
if len(values) > 1000 or any(
type(v) not in (int, float) or not -1e300 <= v <= 1e300 for v in values
):
raise ValueError("values.range")
if not values:
return -inf
peak = max(values)
return peak + log(fsum(exp(v - peak) for v in values))The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.