Skip to content

ENH: add k offsets to diagonal helper functions#31493

Open
ricas28 wants to merge 1 commit into
numpy:mainfrom
GoncaloTorcato:enh/add-diagonal-offset-args
Open

ENH: add k offsets to diagonal helper functions#31493
ricas28 wants to merge 1 commit into
numpy:mainfrom
GoncaloTorcato:enh/add-diagonal-offset-args

Conversation

@ricas28
Copy link
Copy Markdown

@ricas28 ricas28 commented May 24, 2026

PR summary

Add support for a k argument to diagonal helper functions so they can operate on diagonals above or below the main diagonal. This extends fill_diagonal, diag_indices, diag_indices_from, identity, and ma.identity.

For example, np.fill_diagonal(a, value, k=1) now fills the first diagonal above the main diagonal, while k=-1 targets the first diagonal below it.

Also update documentation, typing stubs, release notes, and tests for the new offset behavior.

Closes #28310.

Co-authored-by: Ricardo Curveira Santos ricardo.curveira.santos@tecnico.ulisboa.pt

AI Disclosure

Yes. I used OpenAI Codex/ChatGPT to help inspect the existing NumPy code paths, update tests/docs/typing stubs.
I reviewed the generated changes, adjusted the implementation where
needed, and verified the final result locally with spin test.

    Add support for a k argument to diagonal helper functions so they can
    operate on diagonals above or below the main diagonal. This extends
    fill_diagonal, diag_indices, diag_indices_from, identity, and
    ma.identity.

    For example, np.fill_diagonal(a, value, k=1) now fills the first diagonal
    above the main diagonal, while k=-1 targets the first diagonal below it.

    Also update documentation, typing stubs, release notes, and tests for the new
    offset behavior.

    Closes numpy#28310.

    Co-authored-by: Ricardo Curveira Santos <ricardo.curveira.santos@tecnico.ulisboa.pt>
@ricas28 ricas28 force-pushed the enh/add-diagonal-offset-args branch from 251cd1f to 8572463 Compare May 25, 2026 16:31
@Ramshetty02
Copy link
Copy Markdown

Ramshetty02 commented May 27, 2026

Tested on macOS (Apple M-series), Python 3.14.5, numpy 2.6.0.dev0.
All 385 quantile tests pass including test_float16_gh_29003.
python -m pytest numpy/lib/tests/test_function_base.py -k quantile -v → 385 passed, 72 skipped.

import numpy as np # type: ignore
a = np.zeros(65505)
a[-1] = 1.0
result = np.quantile(a, 0.9)
print('Result:', result)
print('Is nan?', np.isnan(result))

Result: 0.0
Is nan? False

Copy link
Copy Markdown
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

This may still need a bit of API decisions. fill_diagonal makes a lot of sense to me to match diagonal. identity to me doesn't make sense.
The others seem fine, although I would tend to go go with *, offset= generally (yes, existing functions use k=, but I think they usually pass it by position).

Comment thread numpy/_core/numeric.py
@finalize_array_function_like
@set_module('numpy')
def identity(n, dtype=None, *, like=None):
def identity(n, dtype=None, *, k=0, like=None):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Personally, I am very much against adding it to identity, eye is the same and identity+offset is a misnomer.

@array_function_dispatch(_fill_diagonal_dispatcher)
def fill_diagonal(a, val, wrap=False):
"""Fill the main diagonal of the given array of any dimensionality.
def fill_diagonal(a, val, wrap=False, *, k=0):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Diagonal uses offset=. If we add it now and maybe also for the other functions (not currently in the PR) add it as a kwarg only, should we use that name? This needs a final API decision.

def test_offset_out_of_bounds(self):
a = np.zeros((3, 3), int)
fill_diagonal(a, 5, k=3)
assert_array_equal(a, np.zeros((3, 3), int))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you are missing a test for wrap=True?

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.

ENH: Add diagonal offset argument to all functions that are missing it

5 participants