# Represent dimensions with primes

**URL:** <https://discourse.julialang.org/t/represent-dimensions-with-primes/84029>\
**Category:** Tooling\
**Tags:** unitful\
**Created:** [July 11, 2022, 1:44am UTC](https://discourse.julialang.org/t/represent-dimensions-with-primes/84029 "2022-07-11T01:44:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmoored4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmoored4/32/18311_2.png) [@dmoored4](https://discourse.julialang.org/u/dmoored4)\
**Post date:** [July 11, 2022, 1:44am UTC](https://discourse.julialang.org/t/represent-dimensions-with-primes/84029/1 "2022-07-11T01:44:02Z")

</div>

I use Unitful in every single project so this is not to suggest that there is any kind of inadequacy! More of just a thought exercise. I was wondering if there would be any benefit if the different core dimensions were represented under the hood by unique primes and then any division could be performed with rational `//` division. The derived dimensions would then be multiples of primes and/or rational expressions of primes and/or multiples of those primes. It seems this would make keeping track of and simplifying units simpler and maybe faster?

Obviously everything is working fine! Just wanted to throw this out there and see what people think.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [July 11, 2022, 3:18am UTC](https://discourse.julialang.org/t/represent-dimensions-with-primes/84029/2 "2022-07-11T03:18:38Z")

</div>

it would simplify everything … maybe too much, some problems like to keep a specific (set of) derived units because their interrelationships, while simplifiable make sense themselves in the problem domain. If that is not a concern, then its worth mocking up and seeing how it goes.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [July 11, 2022, 4:33am UTC](https://discourse.julialang.org/t/represent-dimensions-with-primes/84029/3 "2022-07-11T04:33:14Z")

</div>

This is a fun exercise in using simple types and overloading.

### Step 0: write some functions to help generate prime numbers:

```julia-auto
function isprime(num::Integer)
    if num <= 0
        return false
    end
    for j = 2:round(Int64, sqrt(num), RoundDown)
        if (num % j) == 0
            return false
        end
    end
    return true
end

ListOfPrimesUpTo(n) = filter(x -> isprime(x), 1:n)

```

### Step 1: define a simple type structure to keep track of dimensions:

```julia-auto
@enum DimensionPrimes begin
    Length = 2
    Mass = 3
    Time = 5
    Current = 7
    Temperature = 11
    Luminosity = 13
end

```

### Step 2: overload a few methods to help things work:

```julia-auto
Base.inv(a::DimensionPrimes) = 1//Integer(a)

Base.:*(a::DimensionPrimes, b::DimensionPrimes) = Integer(a)*Integer(b)

Base.:^(a::DimensionPrimes, b::Integer) = Integer(a)^b

```

### Step 3: “test”:

```julia-auto
# Short-form of dimension variables:
julia> L = Length; M = Mass; T = Time;

julia> Force = M*L*T^-2
6//25

```

---

<div class="post-metadata">

**Author:** ![dmoored4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmoored4/32/18311_2.png) [@dmoored4](https://discourse.julialang.org/u/dmoored4)\
**Post date:** [July 11, 2022, 8:59pm UTC](https://discourse.julialang.org/t/represent-dimensions-with-primes/84029/4 "2022-07-11T20:59:22Z")

</div>

Interesting. I’d messed around a little bit and found that indeed kinetic energy ended up with the same rational expression as potential energy. Your little demo with overloading the base operators has been very instructional for me, thanks!
