import copy


def finite_difference(target, values, p, DELTA = 1.e-7):
  """ Compute finite difference given a target function """
  functional = target(values)
  tempvals = copy.deepcopy(values)
  tempvals.reference[p] += DELTA

  dfunctional = target(tempvals)
  #calculate by finite_difference
  finite_g = (dfunctional-functional )/DELTA
  return finite_g