Nicolas Rougier - 1 year ago 87

Python Question

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`

`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=???)

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**