Exponent of a summation


Exponent of a summation



I am a very new python user.
I am trying to calculate an exponent of a summation. The array has more parameters.


import math

a = [[1, 2, 3, 4],
[5, 6, 7, 8]]

def y(i):
p = 2
total = 0
for j in range (4):
total += math.exp(a[i][j] * (p**j))

return total



Answer from this method: 7.89629603455e+13


7.89629603455e+13



The answer is way different than manual calculation below:


y = math.exp(1*(2**0) + 2*(2**1) + 3*(2**2) + 4*(2**3))



Answer: 1.9073465725e+21


1.9073465725e+21



Equation





Can you post expected answer for this
– Venkata Gogu
Jun 29 at 17:47





The expected output has the sum inside the exponential, while in the loop you take the exponential first and then sum. These aren't the same.
– Jay Calamari
Jun 29 at 17:50






@RBalasubramanian, I put the correct indentation in Python. When I copy and paste, it changes.
– Duchess
Jun 29 at 17:51





I am not sure what you are trying to do but I think you have a mistake.. exp(x+y) != exp(x) + exp(y) - perhaps change to total *= ...
– Daniel M
Jun 29 at 17:53



exp(x+y) != exp(x) + exp(y)


total *= ...





@MohammadAthar, I was playing around with different methods, I put the correct code. Please ignore the indentation.
– Duchess
Jun 29 at 17:54




2 Answers
2



Your mistake appears to not be a python error, but a math error in decomposing the equation. You can make one of two changes:



Solution 1: Sum all first, then take e^ of the total


import math

a = [[1, 2, 3, 4],
[5, 6, 7, 8]]

def y(i):
p = 2
total = 0
for j in range (4):
total += a[i][j] * (p**j)

return math.exp(total)



Solution 2: correctly decompose the exponent and change total += to total *=


import math

a = [[1, 2, 3, 4],
[5, 6, 7, 8]]

def y(i):
p = 2
total = 0
for j in range (4):
total *= math.exp(a[i][j] * (p**j))

return total



Solution 1 is more efficient, as it does not make duplicate calls to math.exp()


import math

a = [[1, 2, 3, 4],
[5, 6, 7, 8]]

def y(i):
p = 2
total = 1
for j in range (4):
total *= math.exp(a[i][j] * (p**j))
return total



Multiplication of exponentianals with same base is same with summing the power values.


exp(a+b)=exp(a)*exp(b)



Optimization of the code:


import math

a = [[1, 2, 3, 4],
[5, 6, 7, 8]]

def y(i):
p = 2
total = 0
for j in range (4):
total += a[i][j] * (p**j)
return math.exp(total)





Much better to simply compute the sum, then pass that as the argument to a single call to math.exp.
– chepner
Jun 29 at 18:00


math.exp





@chepner you are right but he is starter so i wrote first code because it is easier to find the false.
– MIRMIX
Jun 29 at 18:04






Note that the "optimized" code implements the mathematical formula directly.
– Code-Apprentice
Jun 29 at 18:05





@Code-Apprentice yes nice point.
– MIRMIX
Jun 29 at 18:06






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV