给定一组术语||(p_i' - sum{w_ji*(R_j*p_i+v_j)})||^2
,其中||...||^2
表示平方范数,我想在Python中有效地建立一个由这些术语填充的数组(或列表)。 p_i'
,p_i
,v_j
是三维向量,R_j
是3x3矩阵。
我已经尝试过了,但是我不知道如何在j
上加和。
new_points = r_mesh.points() # p', return Nx3 array
old_points = avg_mesh.points() # p
n_joints = 3
rv = np.arange(n_joints * 15) # R_j and v_j are stored in rv
weights = np.random.rand(n_joints, len(new_points)) # w
func = [[np.linalg.norm(
new_points[i] - (weights[j, i] * ((np.array(rv[j * 15:j * 15 + 9]).reshape(3, 3) @ old_points[i]) + np.array(
rv[j * 9 + 9: j * 9 + 12])))) for j in range(n_joints)] for i in range(len(new_points))]
为了使情况更清楚,我将原始方程式转换为非线性函数,以将其输入Levenberg-Marquardt方法。
编辑:对不起,以前有一个错误的图像。
答案 0 :(得分:2)
最简单的方法(“自动驾驶”,无需实际思考)为np.einsum
:
# set up example:
n_i, n_j = 20, 30
p = np.random.random((n_i, 3))
pp = np.random.random((n_i, 3))
R = np.random.random((n_j, 3, 3))
w = np.random.random((n_j, n_i))
v = np.random.random((n_j, 3))
# now just tell einsum which index is where and let it
# do its magic
# R_j p_i
Rp = np.einsum('jkl,il', R,p)
# by Einstein convention this will sum over l,
# so Rp has indices ijk
# w_ji (Rp_ij + v_j)
wRpv = np.einsum('ji,ijk->ik', w,Rp+v)
# pure Einstein convention would sum over i and j,
# we override this by passing explicit output indices
# ik to keep i alive
# squared norm
d = pp - wRpv
result = np.einsum('ik,ik', d,d)
答案 1 :(得分:-2)
尽管我不明白您要做什么,但我看到您想对一个索引求和。您不在代码的任何地方使用sum()。您可能要检查函数sum()。 https://docs.python.org/3/library/functions.html#sum或https://www.programiz.com/python-programming/methods/built-in/sum