def prod_all_but_j(vector):
""" returns a vector where the jth term is the product of all the entries except the jth one """
zeros = np.where(vector==0)[0]
if len(zeros) > 1:
return np.zeros(len(vector))
if len(zeros) == 1:
result = np.zeros(len(vector))
j = zeros[0]
result[j] = np.prod(vector[np.arange(len(vector)) != j])
return result
joint = np.prod(vector)
return np.true_divide(joint,vector)
评论列表
文章目录