grad_share: Split function and gradient calculation
Description
Calculate function and gradient together but access separately.
Reduces computation since they share data in calculation.
Doesn't have to be function and gradient, can be any two
values calculated together but accessed separately.
Useful in optimization when function evaluation is expensive
since the chain rule means many parts of function and gradient
are the same.
Usage
grad_share(fn_gr)
Arguments
fn_gr
A function that returns a list of two values.
Both are calculated when fn is called, but only the first
is returned. The second is returned when gr is called
but nothing is recalculated.