w3resource logo
Python Exercises

Python NumPy: Compute logarithm of the sum of exponentiations of the inputs

Python NumPy Math, Statistics, Trigonometry and Linear algebra: Exercise-2 with Solution

Write a Python program to compute logarithm of the sum of exponentiations of the inputs, sum of exponentiations of the inputs in base-2.

Sample Solution:-

Python Code:

import numpy as np
l1 = np.log(1e-50)
l2 = np.log(2.5e-50)
print("Logarithm of the sum of exponentiations:")
print(np.logaddexp(l1, l2))
print("Logarithm of the sum of exponentiations of the inputs in base-2:")
print(np.logaddexp2(l1, l2))

Sample Output:

Logarithm of the sum of exponentiations:                               
-113.876491681                                                         
Logarithm of the sum of exponentiations of the inputs in base-2:       
-113.599555228 

Python Code Editor:

import numpy as np
l1 = np.log(1e-50)
l2 = np.log(2.5e-50)
print("Logarithm of the sum of exponentiations:")
print(np.logaddexp(l1, l2))
print("Logarithm of the sum of exponentiations of the inputs in base-2:")
print(np.logaddexp2(l1, l2))

Improve this sample solution and post your code through Disqus