# Problems with Diagonal from LinearAlgebra

**URL:** <https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429>\
**Category:** General Usage\
**Tags:** question, package, linearalgebra\
**Created:** [July 16, 2019, 3:28pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429 "2019-07-16T15:28:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cholt](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@cholt](https://discourse.julialang.org/u/cholt)\
**Post date:** [July 16, 2019, 3:28pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/1 "2019-07-16T15:28:02Z")

</div>

I have been using the Diagonal function to take a vector array and produce a matrix with entries of the vector along the diagonal. It used to work fine but now instead of producing a matrix with entries along the diagonal, it now just makes a 1x1 scalar.

```julia
using LinearAlgebra

myvec = [1 2 3]
mymat = Diagonal(myvec)

# previously produced 
mymat = [1 0 0; 0 2 0; 0 0 3]

#now produces 
mymat = 1

```

I think it has something to do with types? For instance, when it works it is a 3x3 Diagonal{type, Array{type, 1}}, and when it doesn’t it’s a 1x1 Diagoanl{similar}. The weird thing is that I didn’t change my code from last week to this, and it used to work, but now it does not. Any ideas would be most helpful.

---

<div class="post-metadata">

**Author:** ![cholt](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@cholt](https://discourse.julialang.org/u/cholt)\
**Post date:** [July 16, 2019, 3:30pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/2 "2019-07-16T15:30:21Z")

</div>

Also even when I try something like

```julia
using LinearAlgebra

myvec = [1 2 3]
mymat = Diagonal(myvec')

```

I sometimes get the same problem, and sometimes I don’t.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 16, 2019, 3:42pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/3 "2019-07-16T15:42:36Z")

</div>

> [@cholt](#):
>
> I sometimes get the same problem, and sometimes I don’t.

I think there may be some state in your session that is not part of your minimal working example, which causes the seemingly different behavior (eg you are overwriting some variables). This should of course be deterministic, and the expected behavior is

```julia
julia> using LinearAlgebra

julia> Diagonal(1:3)
3×3 Diagonal{Int64,UnitRange{Int64}}:
 1 ⋅ ⋅
 ⋅ 2 ⋅
 ⋅ ⋅ 3

julia> Diagonal(ones(3))
3×3 Diagonal{Float64,Array{Float64,1}}:
 1.0 ⋅ ⋅ 
  ⋅ 1.0 ⋅ 
  ⋅ ⋅ 1.0

```

as documented (`?Diagonal`).

---

<div class="post-metadata">

**Author:** ![cholt](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@cholt](https://discourse.julialang.org/u/cholt)\
**Post date:** [July 16, 2019, 3:48pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/4 "2019-07-16T15:48:37Z")

</div>

Agreed, but if all I’m doing is overwriting some variables, why does that change things? And how can I prevent that from happening?

The problem arises from a 1x3 Array{Int64, 2} vector I’ve defined in my code analogously to my toy example. Furthermore it is defined within a function, which I believe means that it is defined each time I run that function (so I can’t be overwriting it in the REPL). I’ve also tried defining it as a 3x1 Array{Int64,2} to no success. I haven’t updated Julia to the most recent version or anything. I’m still using 1.1.1

---

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [July 16, 2019, 3:54pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/5 "2019-07-16T15:54:41Z")

</div>

For your intended behavior, you should provide a vector (Array{Int64, 1}), not a matrix (Array{Int64, 2}). `Diagonal` has different methods for these two different types.

```julia
help?> Diagonal
search: Diagonal Bidiagonal Tridiagonal SymTridiagonal
  Diagonal(A::AbstractMatrix)
  Construct a matrix from the diagonal of A.
  Examples
  ≡≡≡≡≡≡≡≡≡≡
  julia> A = [1 2 3; 4 5 6; 7 8 9]
  3×3 Array{Int64,2}:
   1 2 3
   4 5 6
   7 8 9
  julia> Diagonal(A)
  3×3 Diagonal{Int64,Array{Int64,1}}:
   1 ⋅ ⋅
   ⋅ 5 ⋅
   ⋅ ⋅ 9
-------------------------------------------------------------------------
  Diagonal(V::AbstractVector)
  Construct a matrix with V as its diagonal.
  Examples
  ≡≡≡≡≡≡≡≡≡≡
  julia> V = [1, 2]
  2-element Array{Int64,1}:
   1
   2
  julia> Diagonal(V)
  2×2 Diagonal{Int64,Array{Int64,1}}:
   1 ⋅
   ⋅ 2

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 16, 2019, 4:01pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/6 "2019-07-16T16:01:32Z")

</div>

> [@cholt](#):
>
> Agreed, but if all I’m doing is overwriting some variables, why does that change things

By changing the inputs, the functions give different results.

> [@cholt](#):
>
> And how can I prevent that from happening?

It is hard to answer this without concrete code.

> [@cholt](#):
>
> Furthermore it is defined within a function

It would be much easier to help you if you provided the a self-contained example that demonstrates the problem in a freshly started Julia session.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![cholt](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@cholt](https://discourse.julialang.org/u/cholt)\
**Post date:** [July 17, 2019, 3:40pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/7 "2019-07-17T15:40:44Z")

</div>

I guess what I would like to highlight is there seem to be some simple errors in how Diagonal is defined. Or perhaps I just don’t understand the usefulness of how it is currently defined. Why does

```julia
x = [1 2 3]
different_x = [1, 2, 3]

```

yield different results when calling

```julia

Diagonal(x)

Diagonal(different_x)

```

---

<div class="post-metadata">

**Author:** ![cholt](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@cholt](https://discourse.julialang.org/u/cholt)\
**Post date:** [July 17, 2019, 3:49pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/8 "2019-07-17T15:49:52Z")

</div>

Also you misunderstand my statement. I can re-write it back to the original but it still behaves strangely, and I don’t understand why that is.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 17, 2019, 3:53pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/9 "2019-07-17T15:53:48Z")

</div>

Because the former is a matrix and the latter a vector (you should in general avoid using 1x3 matrices as vector, unfortunately, a lot of people are doing it).

If you input a matrix to the `Diagonal` constructor it will just use its diagonal, which for a 1x3 matrix is only a single element.

---

<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:** [July 17, 2019, 3:59pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/10 "2019-07-17T15:59:09Z")

</div>

The key is that `[1 2 3]` creates a one row _matrix_, not a vector. Calling `Diagonal(::Matrix)` _extracts_ the diagonal from the matrix and constructs a new matrix with it. The reason `Diagonal(myvec')` sometimes works and sometimes doesn’t has to do with where `myvec` came from.

The sure-fire way to get the behavior you want is to use `Diagonal(vec(x))`, which will flatten `x` into a vector and ensure it gets placed on the diagonal.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 17, 2019, 4:02pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/11 "2019-07-17T16:02:14Z")

</div>

In addition to what @DNF and @mbauman explained, you can use the introspection/reflection facilities of Julia to investigate problems like this next time you encounter something unexpected. Eg try

```julia
@which Diagonal([1 2 3])
@which Diagonal([1, 2, 3])

```

and also `@edit` instead of `@which` which should take you to the relevant method.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [July 17, 2019, 7:55pm UTC](https://discourse.julialang.org/t/problems-with-diagonal-from-linearalgebra/26429/12 "2019-07-17T19:55:06Z")

</div>

…also just typing in an expression and looking what the REPL tells is quite helpful. Like trying out a few construction ways to create arrays/vectors/matrices:

```julia
julia> [1 2 3]
1×3 Array{Int64,2}:
 1 2 3

julia> [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> [1; 2; 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> [[1] [2] [3]]
1×3 Array{Int64,2}:
 1 2 3

julia> [[1], [2], [3]]
3-element Array{Array{Int64,1},1}:
 [1]
 [2]
 [3]

```
