mirror of
https://github.com/ml-explore/mlx.git
synced 2025-06-24 09:21:16 +08:00
real and imag properties (#2189)
This commit is contained in:
parent
c1eb9d05d9
commit
a2cadb8218
@ -19,6 +19,8 @@ Array
|
|||||||
array.ndim
|
array.ndim
|
||||||
array.shape
|
array.shape
|
||||||
array.size
|
array.size
|
||||||
|
array.real
|
||||||
|
array.imag
|
||||||
array.abs
|
array.abs
|
||||||
array.all
|
array.all
|
||||||
array.any
|
array.any
|
||||||
|
@ -319,6 +319,18 @@ void init_array(nb::module_& m) {
|
|||||||
R"pbdoc(
|
R"pbdoc(
|
||||||
The array's :class:`Dtype`.
|
The array's :class:`Dtype`.
|
||||||
)pbdoc")
|
)pbdoc")
|
||||||
|
.def_prop_ro(
|
||||||
|
"real",
|
||||||
|
[](const mx::array& a) { return mx::real(a); },
|
||||||
|
R"pbdoc(
|
||||||
|
The real part of a complex array.
|
||||||
|
)pbdoc")
|
||||||
|
.def_prop_ro(
|
||||||
|
"imag",
|
||||||
|
[](const mx::array& a) { return mx::imag(a); },
|
||||||
|
R"pbdoc(
|
||||||
|
The imaginary part of a complex array.
|
||||||
|
)pbdoc")
|
||||||
.def(
|
.def(
|
||||||
"item",
|
"item",
|
||||||
&to_scalar,
|
&to_scalar,
|
||||||
|
@ -2022,6 +2022,15 @@ class TestArray(mlx_tests.MLXTestCase):
|
|||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
mx.add(y, x)
|
mx.add(y, x)
|
||||||
|
|
||||||
|
def test_real_imag(self):
|
||||||
|
x = mx.array([1.0])
|
||||||
|
self.assertEqual(x.real.item(), 1.0)
|
||||||
|
self.assertEqual(x.imag.item(), 0.0)
|
||||||
|
|
||||||
|
x = mx.array([1.0 + 1.0j])
|
||||||
|
self.assertEqual(x.imag.item(), 1.0)
|
||||||
|
self.assertEqual(x.real.item(), 1.0)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user