Skip to content
Merged
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
7 changes: 6 additions & 1 deletion mlx/ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,15 @@ array ones_like(const array& a, StreamOrDevice s /* = {} */) {
}

array eye(int n, int m, int k, Dtype dtype, StreamOrDevice s /* = {} */) {
if (n <= 0 || m <= 0) {
if (n < 0 || m < 0) {
throw std::invalid_argument("[eye] N and M must be positive integers.");
}
array result = zeros({n, m}, dtype, s);

if (n == 0 || m == 0) {
return result;
}

if (k >= m || -k >= n) {
return result;
}
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2513,8 +2513,12 @@ def test_large_binary(self):

def test_eye(self):
self.assertCmpNumpy([3], mx.eye, np.eye)
# Test for zero rows and columns
self.assertCmpNumpy([0], mx.eye, np.eye)
# Test for non-square matrix
self.assertCmpNumpy([3, 4], mx.eye, np.eye)
# Test for zero rows
self.assertCmpNumpy([0, 4], mx.eye, np.eye)
# Test with positive k parameter
self.assertCmpNumpy([3, 4], mx.eye, np.eye, k=1)
# Test with negative k parameter
Expand Down
5 changes: 5 additions & 0 deletions tests/ops_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3140,6 +3140,11 @@ TEST_CASE("test eye") {
CHECK_EQ(eye_3x2.shape(), Shape{3, 2});
auto expected_eye_3x2 = array({1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, {3, 2});
CHECK(array_equal(eye_3x2, expected_eye_3x2).item<bool>());

auto eye_0x0 = eye(0, 0);
CHECK_EQ(eye_0x0.shape(), Shape{0, 0});
CHECK_EQ(eye_0x0.size(), 0);
CHECK_EQ(eye_0x0.dtype(), float32);
}

TEST_CASE("test tri") {
Expand Down
Loading