Solving sparse, linear equations?

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

julia> AA

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

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

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> 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?

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

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);
1 Like

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:

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?

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.

3 Likes

Yes, sparse right-hand sides are not supported. See Sparse solve with sparse rhs

1 Like

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.

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?

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

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.

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

1 Like

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.]

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

1 Like