Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Numerically stable implementation of addition on log-scaled numbers
ResourceFunction["LogSumExp"][array, spec] reduces array by aggregating the specified dimensions with the LogSumExp operation. |
Compute the LogSumExp of a list of numbers:
| In[1]:= |
| Out[1]= |
This is the same as:
| In[2]:= |
| Out[2]= |
However, LogSumExp can be computed in a numerically stable way using MachinePrecision even when larger numbers are involved:
| In[3]:= |
| Out[3]= |
The naive implementation loses precision and switches to arbitrary precision to compensate:
| In[4]:= |
| Out[4]= |
Compare with the exact result:
| In[5]:= |
| Out[5]= |
Compute the LogSumExp on symbolic data:
| In[6]:= |
| Out[6]= |
Reduce a matrix by applying LogSumExp to its rows:
| In[7]:= |
| Out[7]= |
| In[8]:= |
| Out[8]= |
Apply to columns:
| In[9]:= |
| Out[9]= |
Aggregate both rows and columns:
| In[10]:= |
| Out[10]= |
This is equivalent to:
| In[11]:= |
| Out[11]= |
The LogSumExp operation allows you to do addition outside of a Log in the same way that addition allows you to multiply outside of a Log:
| In[12]:= |
| Out[13]= |
A Log transform turns multiplication into addition:
| In[14]:= |
| Out[14]= |
And it turns addition into LogSumExp:
| In[15]:= |
| Out[15]= |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License