offset in fill diagonal

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

offset in fill diagonal

josef.pktd
Is there a simple way to fill in diagonal elements in an array for other than main diagonal?

As far as I can see, the diagxxx functions that have offset can only read and not inplace modify, and the functions for modifying don't have offset and only allow changing the main diagonal.

Usecase: creating banded matrices (2-D arrays) similar to toeplitz.


Josef



_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: offset in fill diagonal

Julian Taylor-3
On 21.01.2017 16:10, [hidden email] wrote:
> Is there a simple way to fill in diagonal elements in an array for other
> than main diagonal?
>
> As far as I can see, the diagxxx functions that have offset can only
> read and not inplace modify, and the functions for modifying don't have
> offset and only allow changing the main diagonal.
>
> Usecase: creating banded matrices (2-D arrays) similar to toeplitz.
>

you can construct index arrays or boolean masks to index using the
np.tri* functions.
e.g.

a = np.arange(5*5).reshape(5,5)
band = np.tri(5, 5, 1, dtype=np.bool) & ~np.tri(5, 5, -2, dtype=np.bool)
a[band] = -1
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: offset in fill diagonal

Ian Henriksen
On Sat, Jan 21, 2017 at 9:23 AM Julian Taylor <[hidden email]> wrote:
On 21.01.2017 16:10, [hidden email] wrote:
> Is there a simple way to fill in diagonal elements in an array for other
> than main diagonal?
>
> As far as I can see, the diagxxx functions that have offset can only
> read and not inplace modify, and the functions for modifying don't have
> offset and only allow changing the main diagonal.
>
> Usecase: creating banded matrices (2-D arrays) similar to toeplitz.
>

you can construct index arrays or boolean masks to index using the
np.tri* functions.
e.g.

a = np.arange(5*5).reshape(5,5)
band = np.tri(5, 5, 1, dtype=np.bool) & ~np.tri(5, 5, -2, dtype=np.bool)
a[band] = -1
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/numpy-discussion

You can slice the array you're filling before passing it to fill_diagonal.
For example:

import numpy as np
a = np.zeros((4, 4))
b = np.ones(3)
np.fill_diagonal(a[1:], b)
np.fill_diagonal(a[:,1:], -b)

yields

array([[ 0., -1.,  0.,  0.],
       [ 1.,  0., -1.,  0.],
       [ 0.,  1.,  0., -1.],
       [ 0.,  0.,  1.,  0.]])

Hope this helps,

Ian Henriksen

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Loading...