Bug in gradient_matrix