# Remove units from sparse matrix

**URL:** <https://discourse.julialang.org/t/remove-units-from-sparse-matrix/121112>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 9, 2024, 4:29pm UTC](https://discourse.julialang.org/t/remove-units-from-sparse-matrix/121112 "2024-10-09T16:29:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)\
**Post date:** [October 9, 2024, 4:29pm UTC](https://discourse.julialang.org/t/remove-units-from-sparse-matrix/121112/1 "2024-10-09T16:29:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [October 9, 2024, 5:33pm UTC](https://discourse.julialang.org/t/remove-units-from-sparse-matrix/121112/2 "2024-10-09T17:33:08Z")

</div>

> [@Fourier](#):
>
> Can I somehow ignore the units?

I think this is called [unit stripping](https://painterqubits.github.io/Unitful.jl/stable/manipulations/#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:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)\
**Post date:** [October 9, 2024, 8:36pm UTC](https://discourse.julialang.org/t/remove-units-from-sparse-matrix/121112/3 "2024-10-09T20:36:34Z")

</div>

Thanks a lot.  
`ustrip` is exactly what I need. It also works on sparse matricies, for example:

```julia
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.
