How to convert a polar tensor into cartesian?

[batch, channel, r, theta] is the input tensor. I want to convert it into [batch, channel, x, y], which x, and y are in cartesian coordinates. How am I able to implement this?

If input is something like [batch, channel , 2] where the first entries are r and the second entries are theta,
then you can use this.

#batch = 4, channels = 3
polar = torch.randn(4,3,2)

r = polar[:,:,0]
theta = polar[:,:,1]

x = r * torch.cos(theta)
y = r * torch.sin(theta)

cartesian = torch.stack([x,y], axis = -1) # resulting in (4,3,2)

If the input is something like [batch, channel, r, theta], then you’d have an output Tensor of size [batch, channel, 2, r, theta] ie to say, for every r and theta, you will have two values which are (x,y).

1 Like