Skip to content

Add logSumExp and logSumExpPair functions#84

Open
ohikouta wants to merge 1 commit intohaskell:masterfrom
ohikouta:feature/add-logsumexp
Open

Add logSumExp and logSumExpPair functions#84
ohikouta wants to merge 1 commit intohaskell:masterfrom
ohikouta:feature/add-logsumexp

Conversation

@ohikouta
Copy link
Copy Markdown

@ohikouta ohikouta commented Apr 4, 2026

Summary

Implements numerically stable log-sum-exp computation, as requested in #65.

  • logSumExp :: U.Vector Double -> Double — computes log(sum(exp(x_i))) using the max-subtraction trick to avoid overflow/underflow
  • logSumExpPair :: Double -> Double -> Double — optimized two-argument version using log1p for higher precision

These functions are commonly used when working with log-probabilities in statistical computing and machine learning.

Changes

  • Numeric.SpecFunctions.Internal: implementation of both functions
  • Numeric.SpecFunctions: exported under a new "Log-sum-exp" section
  • tests/Tests/SpecFunctions.hs: property tests (singleton, commutativity, identity) and unit tests (empty vector, large values, log-probability combination)

Test plan

  • All 74 tests pass (cabal test)
  • Existing tests unaffected
  • Property-based tests verify numerical correctness
  • Edge cases: empty vector returns -Infinity, large values (1000+) handled correctly

Closes #65

Implements numerically stable log-sum-exp computation (closes haskell#65).

- logSumExp: vector version using the max-subtraction trick
- logSumExpPair: optimized two-argument version using log1p

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request for LogSumExp function

1 participant