Adds radians and degrees (#1011)

This commit is contained in:
Aneesh Shetty
2024-04-22 13:17:49 -05:00
committed by GitHub
parent 3d405fb3b1
commit d0dbfe0b97
7 changed files with 120 additions and 1 deletions

View File

@@ -1032,6 +1032,40 @@ void init_ops(nb::module_& m) {
Returns:
array: The inverse hyperbolic tangent of ``a``.
)pbdoc");
m.def(
"degrees",
&mlx::core::degrees,
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def degrees(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Convert angles from radians to degrees.
Args:
a (array): Input array.
Returns:
array: The angles in degrees.
)pbdoc");
m.def(
"radians",
&mlx::core::radians,
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def radians(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Convert angles from degrees to radians.
Args:
a (array): Input array.
Returns:
array: The angles in radians.
)pbdoc");
m.def(
"log",
&mlx::core::log,

View File

@@ -893,6 +893,22 @@ class TestOps(mlx_tests.MLXTestCase):
self.assertTrue(np.allclose(result, expected))
def test_degrees(self):
a = mx.array(
[0, math.pi / 4, math.pi / 2, math.pi, 3 * math.pi / 4, 2 * math.pi]
)
result = mx.degrees(a)
expected = np.degrees(a, dtype=np.float32)
self.assertTrue(np.allclose(result, expected))
def test_radians(self):
a = mx.array([0.0, 45.0, 90.0, 180.0, 270.0, 360.0])
result = mx.radians(a)
expected = np.radians(a, dtype=np.float32)
self.assertTrue(np.allclose(result, expected))
def test_log1p(self):
a = mx.array([1, 0.5, 10, 100])
result = mx.log1p(a)

View File

@@ -49,8 +49,9 @@ class TestVmap(mlx_tests.MLXTestCase):
"sin",
"sqrt",
"square",
"degrees",
"radians",
]
ops = ["erfinv"]
for opname in ops:
with self.subTest(op=opname):
op = getattr(mx, opname)