What is the Julian equivalent of Python’s `np.eye(5, 5, 1)`

```
% python -q
>>> import numpy as np
>>> np.eye(5, 5, 1)
array([[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0.]])
```

It’s `I`

in the `LinearAlgebra`

standard library:

```
In [1]: using LinearAlgebra
In [2]: I(5)
5×5 Diagonal{Bool, Vector{Bool}}:
1 ⋅ ⋅ ⋅ ⋅
⋅ 1 ⋅ ⋅ ⋅
⋅ ⋅ 1 ⋅ ⋅
⋅ ⋅ ⋅ 1 ⋅
⋅ ⋅ ⋅ ⋅ 1
```

Probably using `diagm()`

.

In your case:

```
diagm(1 => [1, 1, 1, 1]);
```

If you want efficient data structure you may use `BandedMatrices.jl`

.

I am not aware of a data structure specialized for the case the diagonals have the values `0`

or `1`

(Namely no need for multiplication for Matrix Multiplication).

1 Like

Note that the third index shifts the diagonal up creating an upper shift matrix instead of an identity.

1 Like

Almost `diagm(1 => [1, 1, 1, 1])`

because of 5x5. Thanks!

Ah, apologies - an unusual use of `eye`

then, given that the name as I understand it comes from Matlab and is a pun on the I dentity matrix.

1 Like

mbauman
Split this topic
May 12, 2023, 4:23pm
8