Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/src/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ void init_array(nb::module_& m) {
},
"other"_a)
.def("__neg__", [](const mx::array& a) { return -a; })
.def("__pos__", [](const mx::array& a) { return mx::copy(a); })
.def("__bool__", [](mx::array& a) { return nb::bool_(to_scalar(a)); })
.def(
"__repr__",
Expand Down
5 changes: 4 additions & 1 deletion python/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,11 +1041,14 @@ def test_array_comparison(self):
self.assertEqual((a > 1).tolist(), [False, False, True])
self.assertEqual((a >= 1).tolist(), [False, True, True])

def test_array_neg(self):
def test_array_unary_ops(self):
a = mx.array([-1.0, 4.0, 0.0])

self.assertEqual((-a).tolist(), [1.0, -4.0, 0.0])

self.assertEqual((+a).tolist(), [-1.0, 4.0, 0.0])
assert +a is not a

def test_array_type_cast(self):
a = mx.array([0.1, 2.3, -1.3])
b = [0, 2, -1]
Expand Down