Add diagonal offset argument to all functions that are missing it
![](https://secure.gravatar.com/avatar/6c746c93bad1aa2312e48e9973657bd9.jpg?s=120&d=mm&r=g)
The following functions accept a diagonal offset argument: - https://numpy.org/doc/stable/reference/generated/numpy.diag.html - https://numpy.org/doc/stable/reference/generated/numpy.diagflat.html - https://numpy.org/doc/stable/reference/generated/numpy.diagonal.html - https://numpy.org/doc/stable/reference/generated/numpy.eye.html - https://numpy.org/doc/stable/reference/generated/numpy.trace.html - https://numpy.org/doc/stable/reference/generated/numpy.tri.html - https://numpy.org/doc/stable/reference/generated/numpy.tril.html - https://numpy.org/doc/stable/reference/generated/numpy.tril_indices.html - https://numpy.org/doc/stable/reference/generated/numpy.tril_indices_from.htm... - https://numpy.org/doc/stable/reference/generated/numpy.triu.html - https://numpy.org/doc/stable/reference/generated/numpy.triu_indices.html - https://numpy.org/doc/stable/reference/generated/numpy.triu_indices_from.htm... The following functions lack such an argument: - https://numpy.org/doc/stable/reference/generated/numpy.diag_indices.html - https://numpy.org/doc/stable/reference/generated/numpy.diag_indices_from.htm... - https://numpy.org/doc/stable/reference/generated/numpy.fill_diagonal.html - https://github.com/numpy/numpy/issues/14402 - https://github.com/numpy/numpy/issues/18000 - https://github.com/numpy/numpy/pull/15079 - https://numpy.org/doc/stable/reference/generated/numpy.identity.html Feature request: Add a diagonal offset argument to all of the functions that are missing it. (fill_diagonal is already tracked by the aforementioned issues, but the rest aren't, to my knowledge.)
![](https://secure.gravatar.com/avatar/5f88830d19f9c83e2ddfd913496c5025.jpg?s=120&d=mm&r=g)
On Sun, Feb 9, 2025 at 6:34 PM Carlos Martin <cgmartin@andrew.cmu.edu> wrote:
This sounds quite reasonable to me. The `k=0` keyword is quite badly named, which is my one concern. Especially when tacking it on at the end of a signature with already 3-4 keywords, it's not a good name. How about something like `diag_offset`? Cheers, Ralf
![](https://secure.gravatar.com/avatar/a2c1b891fe9dd5c60430e823bfe8c298.jpg?s=120&d=mm&r=g)
Ralf Gommers wrote:
FWIW, we chose `offset` for `array_api_extra.create_diagonal`, instead of inheriting `k` from `np.diag`. `np.diagonal` and `np.linalg.trace` also use `offset`. Given that all of the proposed functions apart from `np.identity` already have "diag" as a substring of their name, I think just `offset` would be fine. What else could `offset` mean in the case of `np.identity`? I suppose there is an argument for actually leaving `np.identity` as is—if someone wants a square off-diagonal matrix of ones, which isn't an identity matrix, their code might be more readable with `np.diag(np.ones(...), k=1)` or just `np.eye` instead, right? Maybe it is weird for "eye" but not "identity" to have this capability, though. Cheers, Lucas
![](https://secure.gravatar.com/avatar/5f88830d19f9c83e2ddfd913496c5025.jpg?s=120&d=mm&r=g)
On Tue, Feb 11, 2025 at 10:55 AM Lucas Colley via NumPy-Discussion < numpy-discussion@python.org> wrote:
`offset` sounds good to me.
Yeah I was thinking about commenting on that - I can see an argument for not touching `identity`. But then I thought it's the same for some other matrices, like a diagonal or triangular one - with an offset, they don't really meet their mathematical description anymore. So I am can see arguments either way. Cheers, Ralf
![](https://secure.gravatar.com/avatar/5f88830d19f9c83e2ddfd913496c5025.jpg?s=120&d=mm&r=g)
On Sun, Feb 9, 2025 at 6:34 PM Carlos Martin <cgmartin@andrew.cmu.edu> wrote:
This sounds quite reasonable to me. The `k=0` keyword is quite badly named, which is my one concern. Especially when tacking it on at the end of a signature with already 3-4 keywords, it's not a good name. How about something like `diag_offset`? Cheers, Ralf
![](https://secure.gravatar.com/avatar/a2c1b891fe9dd5c60430e823bfe8c298.jpg?s=120&d=mm&r=g)
Ralf Gommers wrote:
FWIW, we chose `offset` for `array_api_extra.create_diagonal`, instead of inheriting `k` from `np.diag`. `np.diagonal` and `np.linalg.trace` also use `offset`. Given that all of the proposed functions apart from `np.identity` already have "diag" as a substring of their name, I think just `offset` would be fine. What else could `offset` mean in the case of `np.identity`? I suppose there is an argument for actually leaving `np.identity` as is—if someone wants a square off-diagonal matrix of ones, which isn't an identity matrix, their code might be more readable with `np.diag(np.ones(...), k=1)` or just `np.eye` instead, right? Maybe it is weird for "eye" but not "identity" to have this capability, though. Cheers, Lucas
![](https://secure.gravatar.com/avatar/5f88830d19f9c83e2ddfd913496c5025.jpg?s=120&d=mm&r=g)
On Tue, Feb 11, 2025 at 10:55 AM Lucas Colley via NumPy-Discussion < numpy-discussion@python.org> wrote:
`offset` sounds good to me.
Yeah I was thinking about commenting on that - I can see an argument for not touching `identity`. But then I thought it's the same for some other matrices, like a diagonal or triangular one - with an offset, they don't really meet their mathematical description anymore. So I am can see arguments either way. Cheers, Ralf
participants (3)
-
Carlos Martin
-
Lucas Colley
-
Ralf Gommers