NumPy logaddexp adds positive quantities while keeping their representation logarithmic

A logarithmic representation stores the logarithm of a quantity rather than the quantity itself. Combining two such records requires attention: adding their stored numbers directly describes multiplication of the original quantities. Academia Master Trader presents this English lesson for readers in Myanmar, using NumPy logaddexp to explain addition in logarithmic form.

Inputs already represent logarithms

The function computes the natural logarithm of the sum of the exponentials of two inputs. In mathematical notation, the relationship is log(exp(a) + exp(b)). The output remains in the natural-logarithm representation.

After import numpy as np, define a = np.log(2.) and b = np.log(3.). The call np.logaddexp(a,b) returns approximately 1.6094379124, which is the natural logarithm of five.

The input quantities were two and three. Their sum is five, but the returned number is not five because the output is still logarithmic.

To express that result on the original scale, apply np.exp to the returned value. Small floating-point differences can appear in the displayed decimal representation.

Adding quantities represented by natural logarithms

Direct addition means something different

The expression a + b gives the logarithm of six in this example, not the logarithm of five. Adding logarithms corresponds to multiplying the original positive quantities.

Likewise, np.logaddexp(2.,3.) does not calculate the logarithm of two plus three. It treats two and three as the stored logarithms themselves and combines their exponentials.

Write down which scale each input uses before choosing the operation. A quantity and its logarithm may both be ordinary floating-point numbers, so their storage type alone cannot reveal their meaning.

Large magnitudes show why the operation matters

The call np.logaddexp(1000.,1000.) returns approximately 1000.6931471806. The two inputs are equal, so their combined result is the common input plus the natural logarithm of two.

Computing both exponentials separately in ordinary floating-point arithmetic can exceed the available range for this example. Logaddexp allows the combined logarithmic value to remain representable without explicitly forming those enormous quantities first.

The call np.logaddexp(-1000.,-1000.) similarly returns approximately -999.3068528194. Keeping the representation logarithmic also helps with extremely small original quantities.

This does not grant arbitrary precision or unlimited range to every later operation. Converting a result back with exp still depends on whether the original-scale quantity fits the chosen numeric representation.

Arrays combine corresponding positions

Both inputs may be arrays, or one may be a scalar. Their shapes must be compatible for broadcasting, and each output combines the corresponding pair of logarithmic inputs.

The function uses natural logarithms. A different base needs a matching operation or an explicit conversion; changing a label does not change the mathematics.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Keep the input scale and output scale explicit whenever combining logarithmic records.