How to use SparseArrays single precision (Float32)?

Hi All,

I was just experimenting with a Float32 matrix because I want to see if it’s faster and with enough precision.

However to my surprise, I realized that this is not doing what I am expecting:

  1. backslash ‘’ (ldiv) is promoting the type and returning Float64
  2. Trying to use cholesky(A) returns an unexpected error
  3. Trying to use directly CHOLMOD.Sparse also promotes to Float64

I don’t know if I’m missing something here.
Does anyone have an idea that could point me to the right direction?

I know that CHOLMOD can handle double and single precision floats, but I can’t find where is this being promoted.

using Random: seed!
using SparseArraysThis text will be hidden
using LinearAlgebra
using SuiteSparse.CHOLMOD

seed!(1234)
n = 10
A = sprand(Float32, n, n, 0.1)
b = randn(Float32, n, 1)
A = A + A' + 10*I #10×10 SparseMatrixCSC{Float32,Int64}

isposdef!(A) #true
issymmetric(A) #true

A\b # 10×1 Array{Float64,2}

typeof(A) # SparseMatrixCSC{Float32,Int64}
F = cholesky(A) # ERROR: ArgumentError: sparse matrix is not symmetric/Hermitian


CHOLMOD.Sparse(A) #10×10 Sparse{Float64}

Yes, but not always. I tried some other values, and got Float32.

I checked with:

julia> @edit A\b  # In 0.5.2, no longer works in my 1.2.0-DEV.55, and I don't remember the workaround

And saw: “Matrix division using a polyalgorithm.”

so it seems type-unstable, and part of what it calls needs to keep the type the same. It seems to be this line: “return lufact(A) \ B” i.e. lufact promoting to Float64.

issymmetric(A) #true

Well, I got false, but that’s I think not the important part. And “This text will be hidden”. Nope. :slight_smile:

Actually I found this issue https://github.com/JuliaLang/julia/issues/25986

1 Like