JuliaPhysics / JuliaPhysics/Unitful.jl

Compatibility with backslash and sparse arrays

Open
#150 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
675
Forks
124
Avg merge
3h 38m
Merged PRs (30d)
1

Description

So I am not sure what the issue is, or if I am not doing things properly, but it seems that it should be doable to make `Unitful.jl` work with linear algebra. I tend to do array multiplications and solve linear systems a lot via backslash. Here is an example of unit-less things I do:
```julia
# no-unit Array example that works
κ = rand(3, 3)
c = rand(3)
s = κ * c
c ≈ κ \ s

# no-unit sparse Array example that works
using SparseArrays, LinearAlgebra
κ = sprand(3, 3, 0.5) + I
c = rand(3)
s = κ * c
c ≈ κ \ s
```

Now doing that with unit-full scalars is not an issue:
```julia
using Unitful
# Scalar example that works
κ = rand()u"1/s"
c = rand()u"mol/m^3"
s = κ * c
c ≈ κ \ s
```

But it breaks on two important things for me:
1. Using `\` on arrays raises an error:
```julia
# Array example that works for multiplication only
κ = rand(3, 3)u"1/s"
c = rand(3 )u"mol/m^3"
s = κ * c
c ≈ κ \ s # Gives DimensionError
```
My guess is that the factorization is a problem, as I do not know how would one split the units of `κ` into the units of its factors. (E.g., what are the units of `L` and `U` in `κ = L * U`?). Is it possible to give a single unit to a whole array and tell `\` to do the unit conversion on the side? (I.e., do the `κ \ s` evaluation *without* units, and assign it the unit of `s` divided by the unit of `κ`.)

2. Using `*` on sparse arrays raises an error:
```julia
# Sparse Array example that does not work
κ = (sprand(3, 3, 0.5) + I)u"1/s"
c = rand(3)u"mol/m^3"
s = κ * c # Gives DimensionError
```
My guess here is it comes from the zeros of the sparse matrix, which are not given a unit? Maybe doing the unit conversion on the side is still doable?

**EDIT**: I realize this is a partial duplicate of issue #46. The only new feature request here is thus the sparse matrix one.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.