bugfix:
1. Position compare function error
2. marginal error for j==0 when calculating derivatives.