def TrapezoidalRule(data, dx):
  """ Calculate the integral according to the trapezoidal rule

  TrapezoidalRule approximates the definite integral of f from a to b by
  the composite trapezoidal rule, using n subintervals.
  http://en.wikipedia.org/wiki/Trapezoidal_rule#Uniform_grid

  Args:
    data: A list of samples
    dx: The uniform distance along the x axis between any two samples

  Returns:
    The area under the curve defined by the samples and the uniform distance
    according to the trapezoidal rule.
  """

  n = len(data) - 1
  s = data[0] + data[n]

  if n == 0:
    return 0.0

  for i in range(1, n):
    s += 2 * data[i]

  return s * dx / 2.0