# Avoiding computing inverse of matrix, don't think it's possible in my case

**URL:** <https://discourse.julialang.org/t/avoiding-computing-inverse-of-matrix-dont-think-its-possible-in-my-case/98478>\
**Category:** Performance\
**Tags:** linearalgebra, linearsolve\
**Created:** [May 8, 2023, 2:21pm UTC](https://discourse.julialang.org/t/avoiding-computing-inverse-of-matrix-dont-think-its-possible-in-my-case/98478 "2023-05-08T14:21:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![p\_f](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@p\_f](https://discourse.julialang.org/u/p_f)\
**Post date:** [May 8, 2023, 2:21pm UTC](https://discourse.julialang.org/t/avoiding-computing-inverse-of-matrix-dont-think-its-possible-in-my-case/98478/1 "2023-05-08T14:21:48Z")

</div>

I have a system of (6+3N) equations given by:

`A*inv(B)*C*dX = K`

which I then solve using

`dX = (A*inv(B)*C) \ K`

A\*inv(B)\*C form a square (6+3N) matrix but the submatrices have sizes:

A: (6+3N) x (6N)  
B: (6N) x (6N)  
C: (6N) x (6+3N)

The bulk of time taken to solve this sytem of equations is spent calculating the inverse of B. I know generally it is advised to avoid calculating inverses of matrices and transform the problem into a solution of a linear system. However, in this case, I don’t think it’s possible to do this? I just wanted a quick opinion to see if there’s anything clever I can do here. Cheers

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 8, 2023, 2:22pm UTC](https://discourse.julialang.org/t/avoiding-computing-inverse-of-matrix-dont-think-its-possible-in-my-case/98478/2 "2023-05-08T14:22:37Z")

</div>

What’s wrong with `dX = (B \ A * C) \ K`

---

<div class="post-metadata">

**Author:** ![p\_f](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@p\_f](https://discourse.julialang.org/u/p_f)\
**Post date:** [May 8, 2023, 2:35pm UTC](https://discourse.julialang.org/t/avoiding-computing-inverse-of-matrix-dont-think-its-possible-in-my-case/98478/3 "2023-05-08T14:35:19Z")

</div>

Because A, B and C are different sizes, B \ A \* C cannot be done sadly. If they were all square then I think this would be much simpler.

```julia
julia> size.((A,B,C,K))
((66, 120), (120, 120), (120, 66), (66,))

julia> B \ A*C
ERROR: DimensionMismatch: arguments must have the same number of rows
Stacktrace:
 [1] \(F::LU{Float64, Matrix{Float64}, Vector{Int64}}, B::Matrix{Float64})
   @ LinearAlgebra C:\Users\hj20005\AppData\Local\Programs\Julia-1.8.3\share\julia\stdlib\v1.8\LinearAlgebra\src\LinearAlgebra.jl:472
 [2] \(A::Matrix{Float64}, B::Matrix{Float64})
   @ LinearAlgebra C:\Users\hj20005\AppData\Local\Programs\Julia-1.8.3\share\julia\stdlib\v1.8\LinearAlgebra\src\generic.jl:1110
 [3] top-level scope
   @ REPL[46]:1

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 8, 2023, 2:43pm UTC](https://discourse.julialang.org/t/avoiding-computing-inverse-of-matrix-dont-think-its-possible-in-my-case/98478/4 "2023-05-08T14:43:19Z")

</div>

Oops, sorry. It’s supposed to be `(A/B * C) \ K` (or `A * (B \ C)) \ K` depending on how you want to compute it).

---

<div class="post-metadata">

**Author:** ![p\_f](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@p\_f](https://discourse.julialang.org/u/p_f)\
**Post date:** [May 8, 2023, 4:03pm UTC](https://discourse.julialang.org/t/avoiding-computing-inverse-of-matrix-dont-think-its-possible-in-my-case/98478/5 "2023-05-08T16:03:53Z")

</div>

Amazing, thanks. I clearly don’t have a good understanding of the relevant linear algebra here…will read up a bit…

Straightforward \>2x speedup for free! I expect for larger system sizes the speedup will be even greater!

```julia
@btime dX1 = (A*inv(B)*C) \ K
@btime dX2 = (A/B * C) \ K
@btime dX3 = (A * (B \ C)) \ K

40.912 ms (13 allocations: 12.22 MiB)
17.451 ms (13 allocations: 12.21 MiB)
15.032 ms (11 allocations: 11.63 MiB)

```
