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.

fill_diagonal not yet implemented

See original GitHub issue

Just a feature request to implement jnp.fill_diagonal, mainly for consistency with NumPy. Though is the reason this hasn’t been implemented yet that it isn’t pure in NumPy? I don’t see an issue with having a non-pure version that returns a new array.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
jakevdpcommented, Mar 22, 2021

I see; here’s a quick implementation similar to np.fill_diagonal that should work for any batch size without reshapes or vmaps:

import jax.numpy as jnp

def fill_diagonal(a, val):
  assert a.ndim >= 2
  i, j = jnp.diag_indices(min(a.shape[-2:]))
  return a.at[..., i, j].set(val)

a = jnp.zeros((2, 3, 4, 4))

# works for scalars
a1 = fill_diagonal(a, 2)

# or for batched vectors
a2 = fill_diagonal(a, jnp.arange(24).reshape(2, 3, 4))
3reactions
hawkinspcommented, Apr 14, 2020

My main hesitation here is that np.fill_diagonal is specified to update its input in-place. We can’t do that in JAX. We could provide an alternate API that returns an updated array, although that wouldn’t satisfy the “consistent with NumPy” constraint. What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy fill_diagonal return None - python - Stack Overflow
I want to generate symmetric zero diagonal matrices.
Read more >
numpy.fill_diagonal — NumPy v1.24 Manual
This function modifies the input array in-place, it does not return a value. ... implementation that never constructs the indices and uses simple...
Read more >
diag: Matrix Diagonals - Rdrr.io
(when x is a matrix) logical indicating if the resulting vector, the diagonal of x , should inherit names from dimnames(x) if available....
Read more >
Efficiently compute sums of diagonals of a matrix
Condition for Principal Diagonal: The row-column condition is row = column. ... Auxiliary Space: O(1), as we are not using any extra space....
Read more >
Compute Weights for 'rma' Objects - metafor
The method is not yet implemented for objects of class "rma.glmm" . type. character string to specify whether to return only the diagonal...
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