# Inverse with destination?

**URL:** <https://discourse.julialang.org/t/inverse-with-destination/2267>\
**Category:** General Usage\
**Created:** [February 24, 2017, 7:51am UTC](https://discourse.julialang.org/t/inverse-with-destination/2267 "2017-02-24T07:51:59Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 20, 2019, 1:28am UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/21 "2019-05-20T01:28:15Z")

</div>

> [@e3c6](#):
>
> at some point there is a tridiagonal matrix

I think it would help if you could write down your whole regression problem in mathematical form, including the form of the error you’re trying to compute. At this stage it seems people are guessing at what you’re trying to do 😉

Perhaps most importantly - what are you going to do with the covariance matrix of the parameters? If it’s high dimensional will you be summarizing it in some way? Perhaps this can be done without actually computing the matrix itself?

Not that I’m necessarily against having an `inv!(dest, src)`.

---

<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:** [May 20, 2019, 6:48am UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/22 "2019-05-20T06:48:04Z")

</div>

> [@e3c6](#):
>
> `inv!(dest, A)` could be useful […] and I suspect it should not be so hard to have?

There is a trade-off between the maintenance cost of supporting in-place variants vs their usefulness (recall that these are generic functions, and need to work for many types). Given that most people think that even `inv` is rarely used, I doubt this would be a good choice.

Note that even `inv!` is not part of the API (it is neither exported, nor documented), and thus technically an internal function.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 20, 2019, 7:01am UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/23 "2019-05-20T07:01:19Z")

</div>

The cost of creating the array should in most cases be completely dwarfed by the cost of doing the factorization.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 20, 2019, 7:23am UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/24 "2019-05-20T07:23:16Z")

</div>

> [@kristoffer.carlsson](#):
>
> The cost of creating the array should in most cases be completely dwarfed by the cost of doing the factorization.

Not always. For example, when `A` is tridiagonal, its `LU` decomposition is bidiagonal, but the inverse can be dense. At least in this case it would help to have `inv!(dest, A)` with `dest` preallocated.

In many cases the `LU` decomposition of a sparse matrix is also sparse. But since the inverse `dest` will be dense, it can help to have it pre-allocated.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 20, 2019, 7:41am UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/25 "2019-05-20T07:41:01Z")

</div>

Perhaps you can show an example benchmarking the time to do the factorization vs copying the output matrix.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 20, 2019, 10:28am UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/26 "2019-05-20T10:28:42Z")

</div>

It’s about memory, not time. I want to avoid allocating space for the inverse matrix twice.

Please note that I’m not asking about an in-place `inv!(A)`.  
I just want to provide the pre-allocated space for the inverse in `inv!(dest, A)`, where `dest` is a different array from `A`.

---

<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:** [May 20, 2019, 10:45am UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/27 "2019-05-20T10:45:17Z")

</div>

> [@e3c6](#):
>
> I just want to provide the pre-allocated space for the inverse in `inv!(dest, A)` , where `dest` is a different array from `A` .

Look at the primitives in `LinearAlgebra.LAPACK`. Note that you still need to pre-allocate a temporary buffer for the pivot indices if you want to use LU.

Also note that for the particular problem we discussed above, this

1. entirely unnecessary,

2. the low hanging fruit is [avoiding formation of the covariance matrix](https://discourse.julialang.org/t/inverse-with-destination/2267/19), which will save you much, much more than simply optimizing the wrong algorithm.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 20, 2019, 12:38pm UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/28 "2019-05-20T12:38:43Z")

</div>

> [@Tamas\_Papp](#):
>
> the low hanging fruit is [avoiding formation of the covariance matrix](https://discourse.julialang.org/t/inverse-with-destination/2267/19), which will save you much, much more than simply optimizing the wrong algorithm.

I wish could. But how else can I estimate the errors of the regressed parameters? (Maybe I’m misunderstanding something you said before).

---

<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:** [May 20, 2019, 1:17pm UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/29 "2019-05-20T13:17:30Z")

</div>

> [@e3c6](#):
>
> But how else can I estimate the errors of the regressed parameters?

If you have a triadiagonal matrix, then you can compute the diagonal elements of the inverse in O(n²) work and O(n) auxiliary storage simply by (1) compute the LU (or LDLᵀ or Cholesky) factorization `F` in O(n) work and storage, (2) solving `F \ eₖ` one at a time, where the eₖ are the columns of the identity matrix, picking out the diagonal, and the discarding the rest of the vector.

In fact, you can get the whole diagonal computation for A⁻¹ down to O(n) work and O(n) memory: see [Rybicki and Hummer (1990)](https://www.lanl.gov/DLDSTP/fast/diagonal.pdf). (There are even fast algorithms to compute the diagonal of the inverse of general spd matrices, but they are [much more complicated](https://web.stanford.edu/~lexing/diagonal.pdf).)

In general, it seems pretty rare to need the whole inverse matrix explicity — you almost always want just some function of the inverse, e.g. selected elements or solves for selected right-hand-sides. (It’s still convenient to compute the inverse explicitly sometimes when you are exploring a problem, of course, even if you probably want to optimize that out of production code.)

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 20, 2019, 1:24pm UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/30 "2019-05-20T13:24:27Z")

</div>

> [@stevengj](#):
>
> (2) solving `F \ eₖ` one at a time

That’s equivalent to computing the inverse, isn’t it?

**Edit:** Well, except that discarding the rest of the vector as you said, saves lots of memory.

---

<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:** [May 20, 2019, 1:31pm UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/31 "2019-05-20T13:31:00Z")

</div>

> [@e3c6](#):
>
> how else can I estimate the errors of the regressed parameters?

First, note that for WLS, it is better to work with

\tilde{X} = UX\\ \tilde{Y} = UY

where W = U'U using a Cholesky decomposition. This reduces a lot of your formulas to the OLS case, so let’s talk about that.

Either QR or SVD is used. Eg

\tilde{X} = QR

so

(\tilde{X}'\tilde{X})^{-1} = (R'R)^{-1} = R^{-1}{R^{-1}}'

and similar in formulas.

Third, you would never form even these, just keep track parts of the same (ie either QR or SVD) decomposition throughout the calculations.

This is pretty standard and you can find the details in most texts on OLS. Or just search for “QR least squares” or similar.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 20, 2019, 2:38pm UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/32 "2019-05-20T14:38:30Z")

</div>

@stevengj @Tamas_Papp Thanks.

---

<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 22, 2019, 3:38pm UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/33 "2019-05-22T15:38:23Z")

</div>

I see a lot of logic in adding to (Every?) functions the ability to provide them the buffers needed to avoid inner allocation of memory.

It is wise both performance wise (Though indeed in many case it is order of magnitude less) and on some cases (Embedded?) it might be a constraint.

So I think while it is great there are many people with knowledge on Weighed and Ordinary Least Squares here, the feature request makes a lot of sense.

---

<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:** [May 22, 2019, 3:43pm UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/34 "2019-05-22T15:43:42Z")

</div>

> [@RoyiAvital](#):
>
> I see a lot of logic in adding to (Every?) functions the ability to provide them the buffers needed to avoid inner allocation of memory.

It is tricky to add this to the API for every function, since it does not make sense for all types (some may not even be mutable, or don’t benefit from preallocation).

There is a good reason for these methods being in the low- or mid-level implementation, since they are not really generic, and the linear algebra API is.

---

<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 22, 2019, 3:54pm UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/35 "2019-05-22T15:54:43Z")

</div>

Of course my remark about every was taking the idea to extreme.  
In my opinion on of Julia’s strength is being able to have low level control.  
One of the things is being able to define buffers for functions.

So I’d vote for having API to provide buffers for low level Linear Algebra operations.

I don’t see why your comment is valid when talking about Linear Algebra.

---

<div class="post-metadata">

**Author:** ![yingqiuz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yingqiuz/32/25165_2.png) [@yingqiuz](https://discourse.julialang.org/u/yingqiuz)\
**Post date:** [May 19, 2021, 12:03am UTC](https://discourse.julialang.org/t/inverse-with-destination/2267/36 "2021-05-19T00:03:34Z")

</div>

> [@Andrea\_Pagnani](#):
>
> p2 = pointer(dest); p1==p2

wouldn’t it work by

```julia
N=3; dest = zeros(N,N); p1 = pointer(dest); dest .= inv(rand(N,N)); p2 = pointer(dest); p1==p2

```

? It returns true.

(Sorry about it if I’m talking nonsense - just started learning Julia)

* * *

update: sorry there is still memory allocation happening inside, even the pointer remains unchanged.

[Previous page](https://discourse.julialang.org/t/inverse-with-destination/2267.md?page=1)
