8000 Refactor · hzyjerry/robotics-toolbox-python@11fd3af · GitHub
[go: up one dir, main page]

Skip to content

Commit 11fd3af

Browse files
committed
Refactor
1 parent b7249bd commit 11fd3af

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

roboticstoolbox/examples/chomp.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
fknm_ = None
2020

2121

22+
def vmatmul(mat1, mat2):
23+
# mat1: (N, a, b)
24+
# mat2: (N, b)
25+
# out: (N, a)
26+
return np.matmul(mat1, mat2[:, :, None]).squeeze(-1)
27+
28+
def vrepeat(vec, N):
29+
# vec: (...)
30+
# out: (N, ...)
31+
return np.repeat(vec[None, :], N, axis=0)
32+
33+
2234
def jacob0_loop(robot, link, pts, jacob_vec, qt=None):
2335
"""
2436
Non-parallel, use for loop
@@ -47,18 +59,18 @@ def jacob0_vec(robot, link, pts, jacob_vec, qt=None, verbose=False):
4759
fknm_=np.ctypeslib.load_library('roboticstoolbox/cuda/fknm','.')
4860
# Parallel, use cuda
4961
num_pts = len(pts)
50-
pts_tool = np.repeat(np.eye(4)[None, :], len(pts), axis=0)
62+
pts_tool = vrepeat(np.eye(4), len(pts))
5163
pts_tool[:, :3, 3] = pts
5264
link_base = robot.fkine(qt, end=link)
5365
# pts_mat = np.array((link_base @ se3_pts).A)
5466
# pts_mat = np.array(link_base.A.dot(pts_tool).swapaxes(0, 1), order='C')
5567
# pts_mat = np.einsum('ij,ljk->lik', link_base.A, pts_tool)
5668
# pts_mat = np.ascontiguousarray(link_base.A.dot(pts_tool).swapaxes(0, 1))
57-
pts_mat = np.matmul(np.repeat(link_base.A[None, :], len(pts), axis=0), pts_tool)
69+
pts_mat = np.matmul(vrepeat(link_base.A, len(pts)), pts_tool)
5870

5971
# e_pts = np.zeros((num_pts, 3))
6072
# pts_etool = np.array(SE3(e_pts).A)
61-
pts_etool = np.repeat(np.eye(4)[None, :], len(pts), axis=0)
73+
pts_etool = vrepeat(np.eye(4), len(pts))
6274
link_As = []
6375
link_axes = []
6476
link_isjoint = []
@@ -171,16 +183,6 @@ def chomp(seed=0):
171183
AA /= dt * dt * (nq + 1)
172184
Ainv = np.linalg.pinv(AA)
173185

174-
def vmatmul(mat1, mat2):
175-
# mat1: (N, a, b)
176-
# mat2: (N, b)
177-
# out: (N, a)
178-
return np.matmul(mat1, mat2[:, :, None]).squeeze(-1)
179-
180-
def vrepeat(vec, N):
181-
# vec: (...)
182-
# out: (N, ...)
183-
return np.repeat(vec[None, :], N, axis=0)
184186

185187
for t in range(iters):
186188
nabla_smooth = AA.dot(xi) + bb

0 commit comments

Comments
 (0)
0