# Solving sparse, linear equations?

**URL:** <https://discourse.julialang.org/t/solving-sparse-linear-equations/90238>\
**Category:** Numerics\
**Tags:** linearalgebra, sparse\
**Created:** [November 14, 2022, 2:20pm UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238 "2022-11-14T14:20:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 14, 2022, 2:20pm UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/1 "2022-11-14T14:20:49Z")

</div>

I have a linear equation `AA*z = bb` where `AA` is a full rank, sparse matrix :

```julia
julia> AA

650×650 SparseMatrixCSC{Float64, Int64} with 2280 stored entries:
...

```

When I try to solve the equation, I get an error message:

```julia
julia> AA \ bb

MethodError: no method matching lu!(::SparseMatrixCSC{Float64, Int64}, ::RowMaximum; check=true)
Closest candidates are:
  lu!(::StridedMatrix{T...

```

NOTE: `Matrix(AA) \ bb` works without a hitch, but that will kill the advantage of introducing a sparse matrix, I guess…

I then tried to download `LinearSolve.jl` (presented in JuliaCon 2022).

```julia
julia> Pkg.add("LinearSolve")

Resolving package versions...
Output exceeds the size limit. Open the full output data in a text editor
Unable to automatically download/install artifact 'Qt5Base' from sources listed in 'C:\Users\User_name\.julia\packages\Qt5Base_jll\RCHuc\Artifacts.toml'.
Sources attempted:
- https://pkg.julialang.org/artifact/14c036187f02f2def7e45b19b27ea5d4a5873e21
    Error: IOError: could not spawn setenv(`7z.exe ...

```

Has anyone have similar experiences?

- Why doesn’t `AA \ bb` work?
- Why can’t I add `LinearSolve`?

---

<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:** [November 14, 2022, 2:25pm UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/2 "2022-11-14T14:25:58Z")

</div>

> [@BLI](#):
>
> When I try to solve the equation, I get an error message:

i can’t replicate your problem. This works for me:

```julia
julia> using SparseArrays, LinearAlgebra

julia> AA = sprand(650,650, 2280/650^2) + I;

julia> summary(AA)
"650×650 SparseMatrixCSC{Float64, Int64} with 2950 stored entries"

julia> AA \ rand(650);

```

```julia

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 14, 2022, 3:05pm UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/3 "2022-11-14T15:05:13Z")

</div>

Arrgh. I found the problem… In my case, `bb` is a sparse 650\times 1 matrix. Seems like I need to convert it to a dense vector first. So for my data, the following works:

```julia
z = AA \ Matrix(bb)[:]

```

Is there a better way to do this? Perhaps I should just let `bb` be dense all the way, since that doesn’t really waste a lot of storage space?

---

<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:** [November 14, 2022, 3:08pm UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/4 "2022-11-14T15:08:23Z")

</div>

in general you probably shouldn’t be using Nx1 matrices in general. They tends to come from matlabisms like `rand(n,1)` instead of `rand(n)`. making sure your types match the algebra you expect will save you time in the long run.

---

<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:** [November 14, 2022, 4:01pm UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/5 "2022-11-14T16:01:37Z")

</div>

> [@BLI](#):
>
> In my case, `bb` is a sparse 650×1 matrix.

Yes, sparse right-hand sides are not supported. See [Sparse solve with sparse rhs](https://discourse.julialang.org/t/sparse-solve-with-sparse-rhs/81595)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 14, 2022, 4:11pm UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/6 "2022-11-14T16:11:35Z")

</div>

OK – I don’t really need a sparse RHS in this case (discrete optimal control with scalar inputs), but in general that would be needed.

Btw, seems like the solution is found ca. 10 times faster when I keep `AA` as a sparse matrix, using `@time` to measure it.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 15, 2022, 8:41am UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/7 "2022-11-15T08:41:22Z")

</div>

> [@BLI](#):
>
> ```julia
> julia> Pkg.add("LinearSolve")
> 
> Resolving package versions...
> Output exceeds the size limit. Open the full output data in a text editor
> Unable to automatically download/install artifact 'Qt5Base' from sources listed in 'C:\Users\User_name\.julia\packages\Qt5Base_jll\RCHuc\Artifacts.toml'.
> Sources attempted:
> - https://pkg.julialang.org/artifact/14c036187f02f2def7e45b19b27ea5d4a5873e21
> Error: IOError: could not spawn setenv(`7z.exe ..
> 
> ```

Can you open an issue? Qt5 should not be in the dependency tree, so I’m confused. Is this a clean environment with only LinearSolve?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 15, 2022, 11:32am UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/8 "2022-11-15T11:32:00Z")

</div>

Hm… I re-tried adding `LinearSolve`, this time from the REPL with now imported packages, and now it seems to install:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/2/62a72b94501459c66fefd991d26d1d7886193e4a.png)

NOTE 1: there is a reference to “downloaded artifact: Qt5Base”.  
NOTE 2: in my previous attempt, I tried to download `LinearSolve` in VSCode after I had added a few other packages.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 15, 2022, 11:38am UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/9 "2022-11-15T11:38:55Z")

</div>

But you also have ControlSystems. That’s not a clean environment. As mentioned earlier, please test this in a clean environment.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 15, 2022, 12:02pm UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/10 "2022-11-15T12:02:18Z")

</div>

You are right – it was not a clean environment. Still, `LinearSolve` now works in my “global” environment.  
= =  
Yeah, I started looking into `Pkg.jl` with the idea of beginning to use the environment facility. What stopped me was the following:

- I didn’t find info on how to specify the valid Julia version in the environment. In other words: I understand how I can specify the versions of various packages in a new environment, but not how I can also specify which version of Julia to use. Maybe I didn’t read the documentation properly…
- The idea of specifying the Julia version for a certain environment is important, I think… When I upgraded Julia from v. 1.6.2 to 1.6.3 (if I’m right), package `Turing` stopped working, and it took weeks before it worked again.
- Thus, if I had created an environment with specified versions of certain packages and the environment did not contain information of which Julia version to use, I would either have to remember which Julia version I could use with the environment [error prone…], or I would have to stop upgrading Julia.

OK – it is possible the environment holds information about which Julia version to use – e.g., the Julia version I specify when I create the environment, but I didn’t see information about that in the documentation. [I checked it out perhaps 6 months ago. I haven’t checked the documentation for updates.]

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 15, 2022, 12:09pm UTC](https://discourse.julialang.org/t/solving-sparse-linear-equations/90238/11 "2022-11-15T12:09:00Z")

</div>

As of Julia v1.7 that information is in the manifest.
