# How do I use symbolic variables with Unitful?

**URL:** https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420
**Category:** General Usage
**Tags:** physics, symbolic, unitful
**Created:** [March 3, 2021, 4:25pm UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420 "2021-03-03T16:25:29Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [March 3, 2021, 4:25pm UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420/1 "2021-03-03T16:25:29Z")

</div>

I am trying to do some basic Physics problems using Julia. I have had some success using Unitful, but I do not know how I can solve problems for general parameters (i.e., get a closed form of the solution in terms of the missing variables).

E.g., I want to do something like:

```julia
force(m, a) = m*a

m = 10u"kg"
@symbol a # pseudocode
a = (a)u"m/s^2" # give it a unit
print(force(m, a))

```

This should print `10*a N`. (`N` is Newton, defined by Unitful).

Of course, this example is trivial, but it is more interesting in bigger problems.

---

<div class="post-metadata">

### Author: ![Bernard\_GODARD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernard_godard/32/4155_2.png) [@Bernard\_GODARD](https://discourse.julialang.org/u/Bernard_GODARD)
#### Post date: [March 3, 2021, 5:50pm UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420/2 "2021-03-03T17:50:15Z")

</div>

You need a CAS (Computer Algebra System) library to perform symbolic computations.  
ModelingToolkit is such a library implemented in julia.

However ModelingToolkit does not implement multiplying a variable with a general Number but only with Real and Complex and unfortunately Unitful quantity are subtype of Number but not of Real:  
I am not sure why this is the case: it looks like it could be a subtype of Real but there may be a good reason for this.

Also even if it worked it would not print 10 \* a N. but something like (10 N)a

---

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [March 3, 2021, 6:18pm UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420/3 "2021-03-03T18:18:48Z")

</div>

I am starting to suspect whether Julia is a bad choice for this type of work, though SymPy does have some [support](https://docs.sympy.org/latest/modules/physics/units/examples.html) (which does not seem to have good Julian wrappers). Do you know any good CASes that work with units?

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [March 3, 2021, 6:27pm UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420/4 "2021-03-03T18:27:14Z")

</div>

I suggest openning an issue on Symbolics.jl  
which is now the CAS behind ModellingToolkit, as of this week.

> **[GitHub - JuliaSymbolics/Symbolics.jl: Symbolic programming for the next...](https://github.com/JuliaSymbolics/Symbolics.jl)**
>
> Symbolic programming for the next generation of numerical software - GitHub - JuliaSymbolics/Symbolics.jl: Symbolic programming for the next generation of numerical software

---

<div class="post-metadata">

### Author: ![Bernard\_GODARD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernard_godard/32/4155_2.png) [@Bernard\_GODARD](https://discourse.julialang.org/u/Bernard_GODARD)
#### Post date: [March 6, 2021, 1:53pm UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420/5 "2021-03-06T13:53:57Z")

</div>

[https://github.com/JuliaSymbolics/Symbolics.jl/issues/93](https://github.com/JuliaSymbolics/Symbolics.jl/issues/93)

---

<div class="post-metadata">

### Author: ![Bernard\_GODARD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernard_godard/32/4155_2.png) [@Bernard\_GODARD](https://discourse.julialang.org/u/Bernard_GODARD)
#### Post date: [March 6, 2021, 2:36pm UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420/6 "2021-03-06T14:36:13Z")

</div>

@NightMachinary you can try as explained in the github issue:

```julia
julia> using Unitful

julia> using ModelingToolkit

julia> import Base:*

julia> *(x::Unitful.AbstractQuantity,y::Num) = Quantity(x.val*y, unit(x))
* (generic function with 735 methods)

julia> *(y::Num,x::Unitful.AbstractQuantity) = x*y
* (generic function with 736 methods)

julia> @variables var_a
(var_a,)

julia> force(m,a)=m*a
force (generic function with 1 method)

julia> a=var_a*u"m/s^2"
var_a m s^-2

julia> m=10u"kg"
10 kg

julia> force(m,a)
10var_a kg m s^-2

```

---

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [March 10, 2021, 9:41am UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420/7 "2021-03-10T09:41:39Z")

</div>

I am now using SymPy, and it somewhat works out of the box.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [March 10, 2021, 9:59am UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420/8 "2021-03-10T09:59:05Z")

</div>

[https://frinklang.org/](https://frinklang.org/) is a language that does a lot of units stuff and some algebra.

---

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [March 10, 2021, 10:05am UTC](https://discourse.julialang.org/t/how-do-i-use-symbolic-variables-with-unitful/56420/9 "2021-03-10T10:05:15Z")

</div>

It seems closed source though. Wouldn’t Mathematica be a much better option then?
