# LinearAlgebra.inv! not exported, in-place inversion

**URL:** <https://discourse.julialang.org/t/linearalgebra-inv-not-exported-in-place-inversion/109563>\
**Category:** Performance\
**Tags:** linearalgebra\
**Created:** [February 1, 2024, 11:18am UTC](https://discourse.julialang.org/t/linearalgebra-inv-not-exported-in-place-inversion/109563 "2024-02-01T11:18:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdaemmerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adaemmerp/32/52351_2.png) [@AdaemmerP](https://discourse.julialang.org/u/AdaemmerP)\
**Post date:** [February 1, 2024, 11:18am UTC](https://discourse.julialang.org/t/linearalgebra-inv-not-exported-in-place-inversion/109563/1 "2024-02-01T11:18:59Z")

</div>

Why is `inv!()` not exported from LinearAlgebra? What is the fastest and most efficient way to invert a Matrix? I have to invert (I - A) where I is the identity Matrix. In some cases A may be sparse.

```julia

Imat = I(10)
Imatx = Imat - rand(10, 10);
y = copy(Imatx);
LinearAlgebra.inv!(lu!(y)) == inv(Imatx)
 true

@btime inv($Imatx);
  1.081 μs (4 allocations: 6.14 KiB)
@btime LinearAlgebra.inv!(lu!($y));
 1.041 μs (3 allocations: 5.27 KiB)

```

`LinearAlgebra.inv!` still allocates.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [February 1, 2024, 11:41am UTC](https://discourse.julialang.org/t/linearalgebra-inv-not-exported-in-place-inversion/109563/2 "2024-02-01T11:41:21Z")

</div>

I don’t think there is any way to invert a Matrix without allocating some additional memory.

That being said, actually forming the inverse of matrix is almost never what one should do. What do you need the inverse for, if I may ask? Usually you want to apply to some other vector/matrix and then you should use `/` or `\` for better accuracy 🙂

---

<div class="post-metadata">

**Author:** ![AdaemmerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adaemmerp/32/52351_2.png) [@AdaemmerP](https://discourse.julialang.org/u/AdaemmerP)\
**Post date:** [February 1, 2024, 11:47am UTC](https://discourse.julialang.org/t/linearalgebra-inv-not-exported-in-place-inversion/109563/3 "2024-02-01T11:47:50Z")

</div>

Thanks. Yes, I need to multiply the inverse to another (random) matrix.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [February 1, 2024, 11:57am UTC](https://discourse.julialang.org/t/linearalgebra-inv-not-exported-in-place-inversion/109563/4 "2024-02-01T11:57:05Z")

</div>

Then you should just use something like

```julia
A = I + rand(10,10)
B = rand(10)
A\B # equivalent to inv(A)*B
A/B # equivalent to A*inv(B)

```

If you need to apply the inverse multiple times, you can factorize the matrix beforehand:

```julia
Af = factorize(A)
# now use Af instead of A to apply inverse multiple times efficiently
Af\B

```

If you wish to preallocate outputs of the application of the inverse you can use `ldiv!` and `rdiv!` instead of `\` and `/`.

```julia
Y = similar(B)
ldiv!(Y, Af, B) # computes Y = inv(Af)*B by overwriting Y

```

Be careful that you don’t reuse the input matrix for output like `ldiv!(B,Af,B)`!

---

<div class="post-metadata">

**Author:** ![AdaemmerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adaemmerp/32/52351_2.png) [@AdaemmerP](https://discourse.julialang.org/u/AdaemmerP)\
**Post date:** [February 1, 2024, 11:58am UTC](https://discourse.julialang.org/t/linearalgebra-inv-not-exported-in-place-inversion/109563/5 "2024-02-01T11:58:00Z")

</div>

Great, thanks.

---

<div class="post-metadata">

**Author:** ![AdaemmerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adaemmerp/32/52351_2.png) [@AdaemmerP](https://discourse.julialang.org/u/AdaemmerP)\
**Post date:** [February 1, 2024, 12:00pm UTC](https://discourse.julialang.org/t/linearalgebra-inv-not-exported-in-place-inversion/109563/6 "2024-02-01T12:00:47Z")

</div>

But I still do not understand why `inv!()` is not exported.
