question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

diag_indices function issue?

See original GitHub issue

https://github.com/numpy/numpy/blob/d9b1e32cb8ef90d6b4a47853241db2a28146a57d/numpy/lib/index_tricks.py#L883-L949

Hello there! Is it ok that diag_indices function returns two links on one index array?

For example, I’m trying to use this function to acces not only to the main diagonal but also to the diagonal with the given offset (like using offset parameter in np.diagonal). To implement that I do something like this:

X = np.array(...) # some 2D array
n, m = X.shape # without loss of generality assume that n <= m
xi, yi = np.diag_indices(n)
yi += offset
xi, yi = xi[np.logical_and(yi < m,yi >= 0)], yi[np.logical_and(yi < m, yi >= 0)]

print(xi, yi) # print to check if values are different (they aren't)
print(xi is yi) # print to check out if instances are different (they aren't)

To fix this issue I suggest to do the following:

...
return list(arange(n) for i in range(ndim))

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Mar 4, 2020

The benefit of having [np.arange(n)] * ndim wrapped up in it’s own function call

Note that (np.arange(n),) * ndim is different in an important way from what you say there, in that only the tuple version works for indexing (without a deprecation warning).

Agreed that this function is inconvenient though.

One thing we could do here is make the return value warn-on-write. Another would be to make the return value read-only.

I wonder if we can finally make np.diagonal return a read-write view, so that the need for this function vanishes.

0reactions
sebergcommented, Mar 4, 2020

Yeah, the diagonal thing has been warning since forever (at least it feels like it). I am happy with considering a full deprecation, or going the write-only/warn-on-write way. Going to the mailing list (best with a preferred/suggested choice maybe) sounds like a good idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.diag_indices() in Python - GeeksforGeeks
The numpy.diag_indices() function returns indices in order to access the elements of main diagonal of a array with minimum dimension = 2.
Read more >
Diagonal< MatrixType, DiagIndex_ > Class Template Reference
DiagIndex, the index of the sub/super diagonal. The default is 0 and it means the main diagonal. A positive value means a superdiagonal,...
Read more >
Given a multi-dimensional array, return an array containing ...
Create an initial array of numbers (for the diagonal sums) first, then use reduce to iterate, using the x index and y index...
Read more >
mvst source: R/reparamT.R - RDRR.io
reparamT = function(N, p, particles){ # Reparameterization function n.pmat.indices.wo = p * (p-1) ... detOmega = numeric(N) detG = numeric(N) diagIndices ...
Read more >
TLS-3XX Series Consoles - Troubleshooting Guide
CSLD Problem 3 - Increase Rate Warning for Manifolded Tanks 2 and 3 ... In-Tank Leak Result Diagnostic Function Diagram . ... consoles/diagindex.eps....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found