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

**URL:** https://discourse.julialang.org/t/what-is-the-julian-equivalent-of-pythons-np-eye-5-5-1/98746
**Category:** New to Julia
**Tags:** linearalgebra, python
**Created:** [May 12, 2023, 2:53pm UTC](https://discourse.julialang.org/t/what-is-the-julian-equivalent-of-pythons-np-eye-5-5-1/98746 "2023-05-12T14:53:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![originalsouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/originalsouth/32/32061_2.png) [@originalsouth](https://discourse.julialang.org/u/originalsouth)
#### Post date: [May 12, 2023, 2:53pm UTC](https://discourse.julialang.org/t/what-is-the-julian-equivalent-of-pythons-np-eye-5-5-1/98746/1 "2023-05-12T14:53:12Z")

</div>

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

```julia
% 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.]])

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 12, 2023, 2:56pm UTC](https://discourse.julialang.org/t/what-is-the-julian-equivalent-of-pythons-np-eye-5-5-1/98746/2 "2023-05-12T14:56:04Z")

</div>

It’s `I` in the `LinearAlgebra` standard library:

```julia
In [1]: using LinearAlgebra

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

```

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [May 12, 2023, 3:02pm UTC](https://discourse.julialang.org/t/what-is-the-julian-equivalent-of-pythons-np-eye-5-5-1/98746/4 "2023-05-12T15:02:03Z")

</div>

Probably using [`diagm()`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.diagm).

In your case:

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

```

If you want efficient data structure you may use [`BandedMatrices.jl`](https://github.com/JuliaLinearAlgebra/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).

---

<div class="post-metadata">

### Author: ![originalsouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/originalsouth/32/32061_2.png) [@originalsouth](https://discourse.julialang.org/u/originalsouth)
#### Post date: [May 12, 2023, 3:02pm UTC](https://discourse.julialang.org/t/what-is-the-julian-equivalent-of-pythons-np-eye-5-5-1/98746/5 "2023-05-12T15:02:08Z")

</div>

Note that the third index shifts the diagonal up creating an [upper shift matrix](https://en.wikipedia.org/wiki/Shift_matrix) instead of an identity.

---

<div class="post-metadata">

### Author: ![originalsouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/originalsouth/32/32061_2.png) [@originalsouth](https://discourse.julialang.org/u/originalsouth)
#### Post date: [May 12, 2023, 3:04pm UTC](https://discourse.julialang.org/t/what-is-the-julian-equivalent-of-pythons-np-eye-5-5-1/98746/6 "2023-05-12T15:04:24Z")

</div>

Almost `diagm(1 => [1, 1, 1, 1])` because of 5x5. Thanks!

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 12, 2023, 3:10pm UTC](https://discourse.julialang.org/t/what-is-the-julian-equivalent-of-pythons-np-eye-5-5-1/98746/7 "2023-05-12T15:10:11Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 12, 2023, 4:23pm UTC](https://discourse.julialang.org/t/what-is-the-julian-equivalent-of-pythons-np-eye-5-5-1/98746/8 "2023-05-12T16:23:45Z")

</div>

2 posts were split to a new topic: [What’s the `In [N]:` prompt? Does it support copy-paste?](https://discourse.julialang.org/t/whats-the-in-n-prompt-does-it-support-copy-paste/98750)
