title: Understanding how to count FLOPs
body: <p>I am having a hard time grasping how to count FLOPs. One moment I think I get it, and the next it makes no sense to me. Some help explaining this would greatly be appreciated.  I have looked at all other posts about this topic and none have completely explained in a programming language I am familiar with (I know some MATLAB and FORTRAN).</p>
  
  <p>Here is an example, from one of my books, of what I am trying to do.</p>
  
  <p>For the following piece of code, the total number of flops can be written as <code>(n*(n-1)/2)+(n*(n+1)/2)</code> which is equivalent to <code>n^2 + O(n)</code>.</p>
  
  <pre><code>[m,n]=size(A)
  nb=n+1;
  Aug=[A b];
  x=zeros(n,1);
  x(n)=Aug(n,nb)/Aug(n,n);
  for i=n-1:-1:1
      x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);
  end
  </code></pre>
  
  <p>I am trying to apply the same principle above to find the total number of FLOPs as a function of the number of equations <code>n</code> in the following code (MATLAB).</p>
  
  <pre><code>% e = subdiagonal vector
  % f = diagonal vector
  % g = superdiagonal vector
  % r = right hand side vector
  % x = solution vector
  
  n=length(f);
  
  % forward elimination
  for k = 2:n
      factor = e(k)/f(k­‐1);
      f(k) = f(k) – factor*g(k‐1);
      r(k) = r(k) – factor*r(k‐1);
  end
  
  % back substitution
  x(n) = r(n)/f(n);
  for k = n‐1:­‐1:1
      x(k) = (r(k)‐g(k)*x(k+1))/f(k);
  end
  </code></pre>