tested shorter d matrix calculation, provides same results.