efficient derivative computation function