Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-148

Log-Sum-Exp

Problem

Compute the numerically stable log-sum-exp of a vector:

LSE(v) = m + log(sum(exp(v[i] - m))), where m = max(v)

This avoids overflow for large entries.

Hint

Subtract the maximum before exponentiating, then add it back.

Test cases

case 1
in: [[0,0]]
expected: 0.6931471805599453
case 2
in: [[1,2,3]]
expected: 3.4076059644443806
case 3
in: [[1000,1000]]
expected: 1000.6931471805599
case 4
in: [[-1,-2]]
expected: -0.6867383124817772
Starter codePython
def log_sum_exp(v):
    # Your code here
    pass

4 test cases · run your code against them in the editor