Given two tensors X, Y with each component interpreted as an angle within [-pi, pi], I am trying to compute their periodic distance in following way:

d = root of (summation of d_i^2)

where d_i = min (abs(x_i -y_i), abs(x_i -y_i + 2 pi), abs(x_i -y_i - 2 pi))

does anyone know what would be a good way to implement this and would there be any problem with backprop when using min function?

Thank you!