4

I've a set of (n) geometrical shapes that are defined by a fixed number (p) of 2D points. Those shapes are independent but for efficiency reason, I stored time in a single numpy array. Scaling or translating those shapes are easy, but I would like to rotate them and I'm not sure how to do that. I suspect np.tensordot is my friend but I can't find the way to properly use it.

n = 100 # Number of shape
p = 4   # Points per shape
P = np.random.uniform(0, 1, (n, p, 2))

# Scaling
S = 0.5*np.ones(n)
P *= S

# Translating
T = np.random.uniform(0, 1, (n, 1, 2))
P += T

# Rotating
A = np.random.uniform(0, 2*np.pi, n)
cosA, sinA = np.cos(A), np.sin(A)

R = np.empty((n,2,2))
R[:,0,0] = cosA
R[:,1,0] = sinA
R[:,0,1] = -sinA
R[:,1,1] = cosA

np.tensordot(P, R, axes=???)
2
  • What would be the shape of the output array?
    – Divakar
    Commented Nov 26, 2016 at 20:52
  • The shape should be the same as P (each point of P is rotated) Commented Nov 26, 2016 at 20:55

1 Answer 1

4

It seems you are keeping the first axis between the two arrays - P and R aligned and sum-reducing one each off the remaining axes from the input arrays. So, we can use np.einsum as it will allow us the axis-alignment criteria.

You are using the last axis from P for the sum-reduction. Now, depending on which axis of R you are losing with sum-reduction for the rotation calculation, one of these should do the job -

np.einsum('ijk,ilk->ijl',P,R) # Using last dim of R for sum-reduction
np.einsum('ijk,ikl->ijl',P,R) # Using second dim of R for sum-reduction
3
  • Thanks ! What would be the geometrical interpretation between the first and last option ? Commented Nov 26, 2016 at 21:12
  • @NicolasRougier Well I can help you with the code part, not sure about the geometry as it relates here. So, I can't comment on it, sorry.
    – Divakar
    Commented Nov 26, 2016 at 21:13
  • I'll experiment then. Thanks again. Commented Nov 26, 2016 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.