Skip to content

Conversation

@mtsokol
Copy link
Member

@mtsokol mtsokol commented Feb 23, 2024

Hi @asmeurer,

This PR fixes broadcasting of rtol argument in matrix_rank function that you pointed out in #25437 (comment).

@asmeurer
Copy link
Member

This looks like it fixes the problem, although I'm not sure if the broadcast_to is necessary.

Should we add tests?

@rgommers
Copy link
Member

Thanks! A regression test would be great indeed.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small comments...


if rtol is None:
rtol = max(A.shape[-2:]) * finfo(S.dtype).eps
rtol = broadcast_to(rtol, A.shape[:-2])[..., newaxis]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be inside the if tol is None branch. Indeed, the if rtol is None statement should be moved there - no point calculating rtol if it is not going to be used anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to rtol = asarray(rtol)[..., newaxis] as we still want to call asarray (similarly asarray(tol) later) to accept list inputs, like: rtol = [0.2, 0.3, 0.1].

assert_equal(matrix_rank(ms), np.array([3, 4, 0]))
# works on scalar
assert_equal(matrix_rank(1), 1)
assert_equal(matrix_rank(I, tol=0.0), matrix_rank(I, rtol=0.0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly sure this would not have failed without the fix - would suggest using the explicit example from the issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I added a regression test with an example from the issue.

@charris charris changed the title FIX: Include broadcasting for rtol argument in matrix_rank BUG: Include broadcasting for rtol argument in matrix_rank Feb 26, 2024
@mtsokol mtsokol force-pushed the matrix_rank-rtol-fix branch 2 times, most recently from 0009fa6 to cd652a7 Compare February 26, 2024 09:32
@mtsokol
Copy link
Member Author

mtsokol commented Feb 26, 2024

This looks like it fixes the problem, although I'm not sure if the broadcast_to is necessary.

Should we add tests?

I changed broadcast_to to asarray to accept list inputs also (asarray(tol) is already present).
I added a regression test.

if rtol is not None and tol is not None:
raise ValueError("`tol` and `rtol` can't be both set.")

if rtol is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you still move this and the next two lines inside the if tol is None branch? Also, since in this branch, rtol is guaranteed to be a scalar, it can be,

if tol is None:
    if rtol is None:
        rtol = max(A.shape[-2:]) * finfo(S.dtype).eps
    else:
        rtol = asarray(rtol)[..., newaxis]
    tol = S.max(axis=-1, keepdims=True) * rtol
else:
    ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I confused tol with rtol in your previous comment - now I should be right.

@mtsokol mtsokol force-pushed the matrix_rank-rtol-fix branch from cd652a7 to b5f5d18 Compare February 26, 2024 14:42
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for the quick change. Looks all good now!

@mhvk mhvk merged commit cde8bc8 into numpy:main Feb 26, 2024
@mtsokol mtsokol deleted the matrix_rank-rtol-fix branch February 26, 2024 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants