Add protection against negative values in eval() and eval_gradients() methods.
Reformat code