def atan2(y, x):
""" My implementation of atan2 in tensorflow. Returns in -pi .. pi."""
tan = tf.atan(y / (x + 1e-8)) # this returns in -pi/2 .. pi/2
one_map = tf.ones_like(tan)
# correct quadrant error
correction = tf.where(tf.less(x + 1e-8, 0.0), 3.141592653589793*one_map, 0.0*one_map)
tan_c = tan + correction # this returns in -pi/2 .. 3pi/2
# bring to positive values
correction = tf.where(tf.less(tan_c, 0.0), 2*3.141592653589793*one_map, 0.0*one_map)
tan_zero_2pi = tan_c + correction # this returns in 0 .. 2pi
# make symmetric
correction = tf.where(tf.greater(tan_zero_2pi, 3.141592653589793), -2*3.141592653589793*one_map, 0.0*one_map)
tan_final = tan_zero_2pi + correction # this returns in -pi .. pi
return tan_final
评论列表
文章目录