def test_vector_to_radial_roundtrip(self): # round trip some random values. for i in range(10): magnitude = np.random.uniform(0., 5.) direction = np.random.uniform(-np.pi, np.pi) vector = angles.radial_to_vector(magnitude, direction) radial = angles.vector_to_radial(*vector) self.assertAlmostEqual(radial[0], magnitude, 5) self.assertAlmostEqual(radial[1], direction, 5)
def test_vector_radial(self): pairs = [((0., 1.), (1., 0.)), ((1., 0.), (1., np.pi / 2)), ((-1., 0.), (1., - np.pi / 2)), ((0., -1.), (1., np.pi)), ((3., 4.), (5., np.arccos(4. / 5.))), ((0., 0.), (0., 0.))] for vector, radial in pairs: actual = angles.vector_to_radial(*vector) # convert from radial to vector and compare np.testing.assert_almost_equal(actual, radial, err_msg=("vector_to_radial\n" "actual: %s " "expected: %s " "args: %s" % (actual, radial, vector))) # now do the same in reverse actual = angles.radial_to_vector(*radial) np.testing.assert_almost_equal(actual, vector, err_msg=("radial_to_vector\n" "actual: %s " "expected: %s " "args: %s" % (actual, vector, radial))) # now try with direction indicating where the field is flowing from. actual = angles.vector_to_radial(*vector, orientation="from") # add pi to the expected value. Note this is done in a way that # keeps the result in [-pi, pi). radial = (radial[0], np.mod(radial[1] + 2 * np.pi, 2 * np.pi) - np.pi) # convert from radial to vector and compare np.testing.assert_almost_equal(actual, radial, err_msg=("vector_to_radial\n" "actual: %s " "expected: %s " "args: %s" % (actual, radial, vector)))