Skip to content
Oakfield Operator Calculus Function Reference Site

Log-Sum-Exp

For x=(x1,,xn)Rnx = (x_1,\dots,x_n) \in \mathbb{R}^n,

LSE(x)=logi=1nexi\operatorname{LSE}(x) = \log \sum_{i=1}^{n} e^{x_i}

To avoid overflow or underflow, use a shift c=maxixic = \max_i x_i and compute

LSE(x)=c+logi=1nexic,c=maxixi\operatorname{LSE}(x) = c + \log \sum_{i=1}^{n} e^{x_i - c}, \qquad c = \max_i x_i

Domain: real vectors or arrays. Codomain: real values; extends to complex arguments via the principal logarithm.


Invariance to uniform shifts: LSE(x+c1)=c+LSE(x)\operatorname{LSE}(x + c\mathbf{1}) = c + \operatorname{LSE}(x). Gradients are softmax weights:

xiLSE(x)=exijexj\frac{\partial}{\partial x_i} \operatorname{LSE}(x) = \frac{e^{x_i}}{\sum_{j} e^{x_j}}

The function is convex and provides a smooth approximation to maxixi\max_i x_i.


  • Dominant entry: if one xix_i is much larger than the rest, LSE(x)maxixi\operatorname{LSE}(x)\approx \max_i x_i.
  • Equal entries: if xi=ax_i=a for all ii, then LSE(x)=a+logn\operatorname{LSE}(x)=a+\log n.
  • Two values: LSE(x1,x2)\operatorname{LSE}(x_1,x_2) is a smooth max with a soft transition region.

Log-sum-exp is built from the exponential and logarithm and is tightly coupled to softmax (its gradient). It is also related to log-absolute-value as a stabilized log-domain primitive.


Oakfield does not currently expose a dedicated “log-sum-exp” operator, but the same stabilization pattern shows up in a few places:

  • Math utilities: core/math_utils.h provides sim_logsumexp2_double and sim_logsumexp2_complex as reusable helpers for numerically stable log-domain accumulation.
  • Softplus-style clamps: the thermostat operator uses log1p(exp(kx)), which is a special case of log-sum-exp (log(exp(0) + exp(kx))) used for smooth, stable clamping.
  • Future-facing: these primitives are intended for operator kernels that need stable “smooth max / free energy” style reductions without overflow.

Expressions of the form logiexi\log\sum_i e^{x_i} arise naturally in statistical mechanics (log partition functions) and large-deviation/Laplace principles, where they summarize ensembles in a stable log domain.

Log-sum-exp is a standard numerical stabilization primitive in optimization and machine learning, avoiding overflow/underflow while retaining differentiability.


  • Boyd & Vandenberghe, Convex Optimization
  • Cover & Thomas, Elements of Information Theory