fix bug when calculating the dense matrix, I didn't assign value to the matrix properly if there is a location appeared in multiple times