mirror of
https://github.com/ml-explore/mlx.git
synced 2025-12-11 23:14:50 +08:00
Adds radians and degrees (#1011)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user