Added constency checks for derivative functions, #970
Fixes #970
Added quick return in XFunctionInternal::grad