0

I am trying to understand how many FLOPs are there if I use a certain algorithm to find the exponential approximated sum, specially If I use math.factorial(n) in python. I understand FLOPs for binary operation, so is factorial also a binary operation here within a function? Not being a computer science major, I have some difficulties with these. My code looks like this:

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import math



x = input ("please enter a number for which you want to run the exponential summation e^{x}")
N = input ("Please enter an integer before which term you want to turncate your summation")

function= math.exp(x)
exp_sum = 0.0
abs_err = 0.0
rel_err = 0.0



for n in range (0, N):
    factorial = math.factorial(n)            #How many FLOPs here?
    power     = x**n                         # calculates N-1 times
    nth_term  = power/factorial              #calculates N-1 times
    exp_sum   = exp_sum + nth_term           #calculates N-1 times
    abs_err   = abs(function - exp_sum)
    rel_err   = abs(abs_err)/abs(function)

Please help me understand this. I might also be wrong about the other FLOPs!

bhjghjh
  • 889
  • 3
  • 16
  • 42
  • There are some issues with your code : `x` is not defined and `function= math.exp(x)` is both unclear and never reused. Could you clarify ? – jadsq Sep 20 '16 at 05:43
  • I updated my code, please take a look – bhjghjh Sep 20 '16 at 06:00
  • What is typical N? What is maximum N? – osgx Sep 20 '16 at 06:01
  • The sum runs for 0 to N-1, so I set the range to be 0 to N, the user sets how many iterations to be done, maximum N could be v really big number close to infinity. I tried UpTo 200 though for my research. – bhjghjh Sep 20 '16 at 13:02

1 Answers1

1

According to that SO answer and to the C source code, in python2.7 math.factorial(n) uses a naive algorithm to compute the factorial so it computes using about n operations as factorial(n)=1*2*3*4*...*n.

A small mistake regarding the rest is that for n in range(0,N) will loop N times , not N-1 (from n=0 to n=N-1).

A final note is that counting FLOP may not be representative of the actual algorithm real world performance especially in python that is an interpretted language and that it tends to hide most of its inner working behind clever syntax that links to compiled C code(eg: exp_sum + nth_term is actualy exp_sum.__add__(nth_term)).

Community
  • 1
  • 1
jadsq
  • 3,033
  • 3
  • 20
  • 32