added reverse-mode code

This is the original code for reverse mode derivation.