# Can I avoid inverting a matrix?

**URL:** https://discourse.julialang.org/t/can-i-avoid-inverting-a-matrix/105011
**Category:** General Usage
**Tags:** question, linearalgebra
**Created:** [October 16, 2023, 12:57pm UTC](https://discourse.julialang.org/t/can-i-avoid-inverting-a-matrix/105011 "2023-10-16T12:57:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 16, 2023, 12:57pm UTC](https://discourse.julialang.org/t/can-i-avoid-inverting-a-matrix/105011/1 "2023-10-16T12:57:57Z")

</div>

I have the following code:

```julia
  M = s*I-SS.A
  G = SS.C*inv(M)*SS.B + SS.D

```

It calculates the transfer function of a linear system in state space form.

Can I avoid inverting the matrix M? I heard that this can result in bad accuracy  
and should be avoided.

---

<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: [October 16, 2023, 1:13pm UTC](https://discourse.julialang.org/t/can-i-avoid-inverting-a-matrix/105011/2 "2023-10-16T13:13:09Z")

</div>

I am not sure to what extend it improves the accuracy but generally it is preferred to use an appropriate matrix factorization of M and the use one of the division operators to apply the inverse. See [docs of `factorize`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.factorize) for some more info on what factorization to use. It looks like your matrix might be positive definite so you should use `cholesky!` to factorize.

```julia
M = s*I-SS.A
Mfact = cholesky!(M) # lu! or other suitable other factorization
G = SS.C*(Mfact(M)\SS.B) + SS.D

```

---

<div class="post-metadata">

### Author: ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)
#### Post date: [October 16, 2023, 1:13pm UTC](https://discourse.julialang.org/t/can-i-avoid-inverting-a-matrix/105011/3 "2023-10-16T13:13:25Z")

</div>

Just use M\SS.B

---

<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: [October 16, 2023, 2:18pm UTC](https://discourse.julialang.org/t/can-i-avoid-inverting-a-matrix/105011/4 "2023-10-16T14:18:12Z")

</div>

> [@ufechner7](#):
>
> Can I avoid inverting the matrix M? I heard that this can result in bad accuracy  
> and should be avoided.

It’s less a question of accuracy (the loss of accuracy is usually minor), and more a question of efficiency. Inverting a matrix to compute `A^-1 * b` is simply a lot of pointless extra effort compared to directly solving the system via `A \ b` (which forms a factorization of `A`, but does not form the inverse).

For example, with `A = randn(1000,1000)` and `b = randn(1000)`, inverting the matrix is slower by a factor of nearly 3:

```julia
julia> @btime inv($A) * $b;
  14.184 ms (6 allocations: 8.13 MiB)

julia> @btime $A \ $b;
  5.108 ms (4 allocations: 7.64 MiB)

```

Whereas if you are computing `C * A^-1 * B` where `C` and `B` are also 1000x1000 matrices, the slowdown is proportionately less:

```julia
julia> @btime $C * inv($A) * $B;
  31.570 ms (9 allocations: 23.38 MiB)

julia> @btime $C * ($A \ $B);
  22.695 ms (7 allocations: 22.90 MiB)

```

(It is much worse when `A` is sparse, because inverting the matrix throws away all the sparsity whereas factorizing it does not.)

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [October 16, 2023, 2:59pm UTC](https://discourse.julialang.org/t/can-i-avoid-inverting-a-matrix/105011/5 "2023-10-16T14:59:28Z")

</div>

`ControlSystemsBase.freqresp` [uses the Hessenberg factorization](https://github.com/JuliaControl/ControlSystems.jl/blob/master/lib/ControlSystemsBase/src/freqresp.jl#L99C1-L138C4) to efficiently compute C(i\omega I - A)^{-1}B + D for multiple different \omega, is there a reason you can’t just call `freqresp(SS, w)`? Or if you have a complex `s` not on the imaginary axis, `evalfr(SS, s)`
