# Is there a \\ operator for Unitful?

**URL:** https://discourse.julialang.org/t/is-there-a-operator-for-unitful/92195
**Category:** New to Julia
**Tags:** linearalgebra, unitful, sympy
**Created:** [December 27, 2022, 7:19pm UTC](https://discourse.julialang.org/t/is-there-a-operator-for-unitful/92195 "2022-12-27T19:19:05Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![FelixHeox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felixheox/32/44015_2.png) [@FelixHeox](https://discourse.julialang.org/u/FelixHeox)
#### Post date: [December 27, 2022, 7:19pm UTC](https://discourse.julialang.org/t/is-there-a-operator-for-unitful/92195/1 "2022-12-27T19:19:05Z")

</div>

I was running into a problem using the \ (linear system solver) operator in Julia. I guess I wrongly assumed the \ operator was overloaded for matrices/vectors with entries of type \<: Number (witch unitful types are). Is there any way to easily overload the \ operator to accept unitful types, or perhaps a different operator/function that works for all types that are subtypes of Number (since I plan on using Sym and Float/Integer types in my code as well)

Environment: Pluto on MacOS  
Packages: Unitful, Sympy, Plots, PlutoUI  
Code:

```julia
let
	a11 = 1u"m"
	a12 = 2u"ft"
	a21 = 5u"mm"
	a22 = 0u"inch"
	A = [a11 a12; a21 a22]
	b = [3u"inch"; 1u"mm"]
	x = A \ b # This line throws error
end

```

Error:  
`MethodError: no method matching (Unitful.Quantity{Rational{Int64}})(::Rational{Int64})`

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [December 27, 2022, 7:33pm UTC](https://discourse.julialang.org/t/is-there-a-operator-for-unitful/92195/2 "2022-12-27T19:33:02Z")

</div>

I don’t think there is an easy way to define a unique, unambiguous solution to that example problem. Which should the units of the elements of `x`? Moreover, in this case it’s only a matter of scale between units of the same kind, but for arbitrary units in `A` and `b`, it might be impossible to solve.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [December 27, 2022, 7:54pm UTC](https://discourse.julialang.org/t/is-there-a-operator-for-unitful/92195/3 "2022-12-27T19:54:20Z")

</div>

Maybe something along these lines:

```julia
using LinearAlgebra, Unitful
import LinearAlgebra: \

(\)(A::Matrix{Quantity{T,U,V}}, B::AbstractVecOrMat) where {T,U,V} =
  Quantity.(ustrip(A)\ustrip(B), unit(eltype(B))/unit(eltype(A)))

```

With this definition:

```julia
julia> A\b
2-element Vector{Rational{Int64}}:
    1//5
 -619//3048

julia> c = ustrip(b)
2-element reinterpret(Rational{Int64}...):
 381//5000
   1//1000

julia> A\c
2-element Vector{Quantity{Rational{Int64}...}}:
       1//5 m^-1
 -619//3048 m^-1

```

But this was a quick sketch, might be ins-and-outs to fix yet.

---

<div class="post-metadata">

### Author: ![FelixHeox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felixheox/32/44015_2.png) [@FelixHeox](https://discourse.julialang.org/u/FelixHeox)
#### Post date: [December 27, 2022, 8:58pm UTC](https://discourse.julialang.org/t/is-there-a-operator-for-unitful/92195/4 "2022-12-27T20:58:26Z")

</div>

Oh yeah, it makes sense that the ‘b’ vector would be in units of Length\*mysteryunit since Ax = b, and x is unknown. Maybe x can be solved given the units of b?

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [December 27, 2022, 11:18pm UTC](https://discourse.julialang.org/t/is-there-a-operator-for-unitful/92195/5 "2022-12-27T23:18:49Z")

</div>

Using the one-argument [`ustrip`](https://painterqubits.github.io/Unitful.jl/stable/manipulations/#Unitful.ustrip) method is generally a bad idea: you don’t know what you’re stripping, it works well only for the trivial case where you have all homogeneous quantities.

---

<div class="post-metadata">

### Author: ![FelixHeox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felixheox/32/44015_2.png) [@FelixHeox](https://discourse.julialang.org/u/FelixHeox)
#### Post date: [December 27, 2022, 11:56pm UTC](https://discourse.julialang.org/t/is-there-a-operator-for-unitful/92195/6 "2022-12-27T23:56:20Z")

</div>

Thanks for the advice! I changed up the method a little bit to ensure that all unit of elements in the matrices A and B are the same before using the system solver.

```julia
function Base.:\(A::Matrix{Quantity{T,U,V}}, B::AbstractVecOrMat) where {T,U,V}
		if typeof(unit(eltype(A))) ≠ Unitful.FreeUnits{(), NoDims, nothing}
			A = map(x -> uconvert(unit(eltype(A)), x), A) 
			B = map(x -> uconvert(unit(eltype(B)), x), B)
			return Quantity.(ustrip(A)\ustrip(B), unit(eltype(B))/unit(eltype(A)))
		end
	end

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [December 28, 2022, 12:42am UTC](https://discourse.julialang.org/t/is-there-a-operator-for-unitful/92195/7 "2022-12-28T00:42:08Z")

</div>

There should be another definition of `\` covering the case when `A` is a non-Unitful matrix and `B` is Unitful. Should be pretty similar to the one already defined.
