# Storage of a diagonal matrix (and best way to update)?

**URL:** <https://discourse.julialang.org/t/storage-of-a-diagonal-matrix-and-best-way-to-update/30841>\
**Category:** New to Julia\
**Created:** [November 7, 2019, 6:40pm UTC](https://discourse.julialang.org/t/storage-of-a-diagonal-matrix-and-best-way-to-update/30841 "2019-11-07T18:40:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [November 7, 2019, 6:40pm UTC](https://discourse.julialang.org/t/storage-of-a-diagonal-matrix-and-best-way-to-update/30841/1 "2019-11-07T18:40:30Z")

</div>

I have a typical situation,

P is an m x n matrix  
Q is an m x m matrix, but only has diagonal elements, i.e.

Q[i,j] = q[k], i=j=k  
Q[i,j] = 0, i != j

and eventually I have to multiply Q and P,

```julia
R = Q * P

```

Naturally, m in my situation is a fairly large number, ~1000, which, if Diagonal is actually stored as a full Matrix is a million elements. I _really_ expect that it is _NOT_ stored that way but is stored as a Diagonal type which is associated with a vector of 1000 elements, i.e. it uses 1000 memory locations.

The values of Q are going to be updated via iteration, therefore allocate once and update is the way to go. There are two ways to do this,

Create the diagonal matrix,

```julia
Q = Diagonal(q)

```

and then update directly,

```julia
for i=1:m 
  Q[i,i] = update(i)
end
R = Q * P

```

or update the base vector and create the Diagonal again before multiplying,

```julia
for i=1:m
  q[i] = update(i)
end

R = Diagonal(q) * P

```

which of course seems like a bad idea since i’m creating the Diagonal instance on every iteration…

I’m _really_ expecting the right answer to be “directly update the Diagonal matrix Q”, just wanted to be sure.

---

<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:** [November 7, 2019, 6:58pm UTC](https://discourse.julialang.org/t/storage-of-a-diagonal-matrix-and-best-way-to-update/30841/2 "2019-11-07T18:58:55Z")

</div>

I don’t think it’ll matter. `Diagonal(q)` doesn’t make a copy of `q` — it’s just a simple wrapper that exposes the vector as a matrix with it on the diagonal. These sorts of things are very frequently no-cost abstractions. I’d just do whichever feels most natural.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 7, 2019, 7:08pm UTC](https://discourse.julialang.org/t/storage-of-a-diagonal-matrix-and-best-way-to-update/30841/3 "2019-11-07T19:08:18Z")

</div>

As @mbauman says, for `D = diagonal(q)`, if you update the `q` matrix it will update `D` (since `D` just refers to `q` without making a copy).

If you care about this sort of thing, you should also consider using `mul!(R, Q, P)` so that you perform the matrix multiplication in-place.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [November 7, 2019, 10:24pm UTC](https://discourse.julialang.org/t/storage-of-a-diagonal-matrix-and-best-way-to-update/30841/4 "2019-11-07T22:24:03Z")

</div>

> [@stevengj](#):
>
> consider using `mul!(R, Q, P)`

Thanks, that’s a very important detail. This is an iterative algorithm so I will be multiplying a fairly large matrix many times.

Which reminds me, there’s also an svd! routine.
