Skip to content

MAINT: silence invalid warnings for np.divide w/ NaN input - #32796

Draft
mdhaber wants to merge 2 commits into
numpy:mainfrom
mdhaber:silence-complex-divide-nan-warnings
Draft

mdhaber wants to merge 2 commits into
numpy:mainfrom
mdhaber:silence-complex-divide-nan-warnings

Conversation

@mdhaber

@mdhaber mdhaber commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

PR summary

np.divide with complex NaN input produce invalid warnings. Based on the behavior of other ufuncs and other precedent (e.g. gh-12236, gh-15988, gh-32602, gh-32706), I don't think this is desired anymore. This silences the warnings with some special case handling.

Additional information

We should sync up the release notes in gh-32751 once this merges.

AI Disclosure

OpenAI Codex was used to inspect the relevant implementation and tests, draft the code changes and regression tests, and prepare this AI disclosure.

@mdhaber
mdhaber marked this pull request as draft September 27, 2026 01:37
@mdhaber
mdhaber force-pushed the silence-complex-divide-nan-warnings branch from 23f5561 to 9abbde9 Compare September 27, 2026 03:12

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Split the NaN input -> NaN + NaN j output into three cases:

const @ftype@ in2r_abs = npy_fabs@c@(in2r);
const @ftype@ in2i_abs = npy_fabs@c@(in2i);
if (in2r_abs >= in2i_abs) {
if (npy_isnan(in2r) || npy_isnan(in2i)) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. at least one component of the denominator is NaN

Comment thread numpy/_core/src/umath/loops.c.src Outdated
Comment on lines +2220 to +2223
else if ((npy_isnan(in1r) || npy_isnan(in1i)) &&
(((npy_isinf(in1r) || npy_isinf(in1i)) &&
(in2r != 0 || in2i != 0)) ||
(npy_isinf(in2r) && npy_isinf(in2i)))) {

@mdhaber mdhaber Sep 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. At least one component of the numerator is NaN and either:
  • the numerator has an infinite component and the denominator is nonzero
  • both denomonator components are infinite.

Comment thread numpy/_core/src/umath/loops.c.src Outdated
if (in2r_abs == 0 && in2i_abs == 0) {
if ((npy_isnan(in1r) &&
(in1i == 0 || npy_isnan(in1i))) ||
(in1r == 0 && npy_isnan(in1i))) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. All zero denominator, and numerator is one of:
NaN + 0j
0 + NaN j
NaN + NaN j

assert_almost_equal(n_r[i], p_r[i], err_msg=f'Loop {i}\n')


class TestCdiv:

@mdhaber mdhaber Sep 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could be more exhaustive if desired.

Comment on lines +429 to +431
(1 + 1j, complex(np.nan, 1)),
(1 + 1j, complex(1, np.nan)),
(1 + 1j, complex(np.nan, np.nan)),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Case 1

Comment on lines +432 to +433
(complex(np.nan, 0), 0j),
(complex(0, np.nan), 0j),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Case 3

Comment on lines +434 to +435
(complex(np.nan, np.inf), 1),
(complex(np.nan, 1), complex(np.inf, np.inf)),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Case 2

This branch has not been deployed

No deployments
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.

1 participant