RF: Simplified the implementation of `gradient`