# What is the best way to define abstract math?

**URL:** <https://discourse.julialang.org/t/what-is-the-best-way-to-define-abstract-math/15343>\
**Category:** Internals & Design\
**Created:** [September 22, 2018, 4:55pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-define-abstract-math/15343 "2018-09-22T16:55:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [September 22, 2018, 4:55pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-define-abstract-math/15343/1 "2018-09-22T16:55:56Z")

</div>

I want to define Clifford Algebra, something already realized in

> **[GitHub - BBN-Q/Cliffords.jl: Efficient calculation of Clifford circuits in...](https://github.com/BBN-Q/Cliffords.jl)**
>
> Efficient calculation of Clifford circuits in Julia. - GitHub - BBN-Q/Cliffords.jl: Efficient calculation of Clifford circuits in Julia.

But, I don’t think it can be helpful to my project. Because I want its **arithmatic structure rather than these types**.  
It it possible to specify the arithmatic structure between self defined types like `MyX`, `MyY` and `MyZ` like

```julia
@group * Pauli begin # `*` operation, defined on Pauli Group
        MyX == X
        MyY == Y
        MyZ == Z
end

```

so that `MyX * MyY` can return `im*MyZ` (Since we have `im*Z == X * Y` in Clifford.jl) ?

Or Is there any examples to define group operations using macros?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 22, 2018, 5:12pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-define-abstract-math/15343/2 "2018-09-22T17:12:29Z")

</div>

Do you mean that you want symbolic computation? In that case, I’m not sure that there’s a good answer in Julia.

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [September 22, 2018, 5:35pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-define-abstract-math/15343/3 "2018-09-22T17:35:52Z")

</div>

It not symbolic computation. Ii is about defining a set of methods (like \*, +) on existing types easily.

e.g. defining `+` operation on a circulant group of N elements. Here, the circulant group is what I mean `arithmetic structure`

By the way, I am also interested in why symbolic computation is hard for Julia? Could you please give a link or explain a bit? Thanks!

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 22, 2018, 5:44pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-define-abstract-math/15343/4 "2018-09-22T17:44:20Z")

</div>

There are several efforts to do symbolic computation in Julia, but it’s a difficult problem. There was a talk at JuliaCon 2018 with a bit of a review.

Julia makes it very easy to define methods on your types; see the docs.  
I’m not sure what you mean by a “circulant group of N elements”. Is the following what you have in mind?

```julia
julia> struct Mod{N}
           i::Int
       end

julia> import Base: +

julia> x = Mod{10}(5)
Mod{10}(5)

julia> y = Mod{10}(6)
Mod{10}(6)

julia> +(x::Mod{N}, y::Mod{N}) where {N} = Mod{N}( (x.i + y.i) % N )
+ (generic function with 181 methods)

julia> x + y
Mod{10}(1)

```

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [September 22, 2018, 5:56pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-define-abstract-math/15343/5 "2018-09-22T17:56:10Z")

</div>

Sorry for my misleading description, what I mean is more general finite group like

```julia
ulia> abstract type CircElem end
julia> struct One <: CircElem end
julia> struct Two <: CircElem end
julia> struct Three <: CircElem end

julia> +(::One, ::Two) = Three()
+ (generic function with 1 method)
julia> +(::Two, ::Two) = One()
+ (generic function with 2 methods)
...

```

Now I have some existing codes like

```julia
julia> struct MyOne end
julia> struct MyTwo end
julia> struct MyThree end

julia> # can I copy the definition of + from CircElem ?

```

PS: I want to define arithmetics in the first place, so these `relations` be reused (i.e. type independent relations)!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 22, 2018, 6:10pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-define-abstract-math/15343/6 "2018-09-22T18:10:56Z")

</div>

Using the type system for run-time computation like this is probably not a great idea. It’s still fairly unclear to me what you’re try to get here. Do you want something that defines algebraic operations for you?

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [September 22, 2018, 6:38pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-define-abstract-math/15343/7 "2018-09-22T18:38:06Z")

</div>

Thanks for your suggestion.

`Clifford.jl` also uses type system to define finite groups.  
By doing this, Pauli operators can be used in function dispatch.

Also thanks @dpsanders, now I think it is indeed related to symbolic computation. Although the author of JuliaCon talk [Symbolic Mathematics in Julia — JuliaCon 2018, London, UK](http://juliacon.org/2018/talks_workshops/41/) states that Julia is suited for symbolic programing, his repo is dependent on SymPy (very inefficient in my opinion).

I will try to figure out by my self.
