# Inversion and multiplication of sparse matrices

**URL:** <https://discourse.julialang.org/t/inversion-and-multiplication-of-sparse-matrices/73819>\
**Category:** Performance\
**Tags:** sparse, matrices\
**Created:** [December 30, 2021, 3:07pm UTC](https://discourse.julialang.org/t/inversion-and-multiplication-of-sparse-matrices/73819 "2021-12-30T15:07:12Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [December 30, 2021, 8:11pm UTC](https://discourse.julialang.org/t/inversion-and-multiplication-of-sparse-matrices/73819/7 "2021-12-30T20:11:51Z")

</div>

Coming from Matlab, I was expecting the same behavior too.

As far as I can tell, Julia’s `\` accepts as right-hand side `b` only vectors like `rand(4)` or single-column matrices like `rand(4,1)`. The result is of the same type as `b`.  
The vector form seems to be preferred, not all linear solvers accept a single-column matrix.

See also [Avoiding memory allocation for solves with multiple right-hand sides - General Usage / Performance - JuliaLang](https://discourse.julialang.org/t/avoiding-memory-allocation-for-solves-with-multiple-right-hand-sides/67879)

```julia
using LinearAlgebra, SparseArrays

function solve_multiRHS!(A, b)
    lu!(A)
    for i = 1:length(b)
        b[i] = A\b[i]
    end
    return b
end

A = rand(4,4)
b = Vector{Float64}[]
for i = 1:3
    push!(b, rand(4))
end 

solve_multiRHS!(A, b)

```

The `sparse` operator, when needed, is then applied to `A` and the _individual_ vectors of `b`.

---

_[View the full topic](https://discourse.julialang.org/t/inversion-and-multiplication-of-sparse-matrices/73819)._
