Use write_matrix in calculate method