Rewrite gradient function to handle single dimension.

Creating an array on each iteration was slow. Also, that implementation had quite
a bit of duplicated logic.
Merge pull request #4 from tonysyu/inpaint-grad-refactor

Inpaint gradient function refactor