I have a sparse array of Quantities with Units from Unitful.jl .
I would like to use the conjugate gradient method (cg) from IterativeSolvers.jl on it. However that is only defined for reals. And AbstractQuantity is not a subtype of real.
How do I best resolve this?
Can I for example make AbstractQuantity real? Can I somehow ignore the units? Is there a better solution (because not all Quantities are real, obviously)?
I think this is called unit stripping. Iβm not sure that itβs the preferred way of doing things, but at least it should work. Here is a made up example:
using Unitful
Ξ± = 1u"m^2/s"
dx = 1u"mm"
A = Ξ±/dx^2 * [2 1 0;
1 2 1;
0 1 2]
S = ones(3) * u"K/s"
A_stripped = ustrip.(u"s^(-1)", A) # Careful to pick
S_stripped = ustrip.(u"K/s", S) # compatible units
T_stripped = A_stripped \ S_stripped # in this part where
T = T_stripped * u"K" # we manipulate plain Floats
@assert A * T β S
1 Like
Thanks a lot.
ustrip
is exactly what I need. It also works on sparse matricies, for example:
using Unitful
using SparseArrays
using IterativeSolvers
v = [1.,2.]u"cm"
A = spdiagm(v)
ustrip(A)
note that there is no β.β, so it does not apply a for loop on every element of the potentially large sparse matrix, but does it in a smart fashion by applying strip to the underlying vectors.