# Alternative Complex type

**URL:** https://discourse.julialang.org/t/alternative-complex-type/26668
**Category:** New to Julia
**Created:** [July 23, 2019, 6:43am UTC](https://discourse.julialang.org/t/alternative-complex-type/26668 "2019-07-23T06:43:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [July 23, 2019, 6:43am UTC](https://discourse.julialang.org/t/alternative-complex-type/26668/1 "2019-07-23T06:43:12Z")

</div>

Is there a complex type saving the angle and the radius instead of the imaginary and the real part?

I think if there are a lot of multiplications this would be faster.

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [July 23, 2019, 8:12am UTC](https://discourse.julialang.org/t/alternative-complex-type/26668/2 "2019-07-23T08:12:59Z")

</div>

I don’t think there is but it would be relatively easy to implement, e.g.

```julia
struct PolarComplex{T<:Real} <: Number
    R::T
    θ::T
    PolarComplex(R::T, θ::T) where T <: Real = 
        new{T}(R, mod(θ,2π))
end

PolarComplex(x::Real, y::Real) = PolarComplex(promote(x,y)...)
 
Base.angle(x::PolarComplex) = x.θ
Base.abs(x::PolarComplex) = x.R

Base.:*(x::PolarComplex, y::PolarComplex) = 
    PolarComplex( abs(x)*abs(y), angle(x)+angle(y) )

z1 = (exp(2im)) * (3*exp(4im))
z1 = PolarComplex(abs(z1), angle(z1))

z2 = PolarComplex(1.0,2.0) * PolarComplex(3.0,4.0) 

@assert z1 == z2

```

The promotion, conversion and constructor part is a bit more complicated but you could pretty much copy the [Complex](https://github.com/JuliaLang/julia/blob/master/base/complex.jl) implementation in Base.

I don’t know if it’s any faster though (I shouldn’t do the mod probably).

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [July 23, 2019, 9:26am UTC](https://discourse.julialang.org/t/alternative-complex-type/26668/3 "2019-07-23T09:26:34Z")

</div>

I think there are still some safety checks going on, but I have no idea how to remove those.

```julia
> z1p = PolarComplex(abs(z1), angle(z1))
> @code_typed z1p*z1p
CodeInfo(
1 ─ %1 = (Base.getfield)(x, :R)::Float64
│ %2 = (Base.getfield)(y, :R)::Float64
│ %3 = (Base.mul_float)(%1, %2)::Float64
│ %4 = (Base.getfield)(x, :θ)::Float64
│ %5 = (Base.getfield)(y, :θ)::Float64
│ %6 = (Base.add_float)(%4, %5)::Float64
│ %7 = (Base.rem_float)(%6, 6.28319)::Float64
│ %8 = (Base.eq_float)(%7, 0.0)::Bool
│ %9 = (Base.and_int)(%8, true)::Bool
│ %10 = (Base.and_int)(%9, true)::Bool
└── goto #3 if not %10
2 ─ %12 = (Base.copysign_float)(%7, 6.28319)::Float64
└── goto #6
3 ─ %14 = (Base.lt_float)(0.0, %7)::Bool
│ %15 = (Base.eq_float)(0.0, %7)::Bool
│ %16 = (Base.and_int)(%15, false)::Bool
│ %17 = (Base.or_int)(%14, %16)::Bool
│ %18 = (%17 === true)::Bool
│ %19 = (Base.not_int)(%18)::Bool
└── goto #5 if not %19
4 ─ %21 = (Base.add_float)(%7, 6.28319)::Float64
└── goto #6
5 ─ goto #6
6 ┄ %24 = φ (#2 => %12, #4 => %21, #5 => %7)::Float64
│ %25 = %new(PolarComplex{Float64}, %3, %24)::PolarComplex{Float64}
└── goto #7
7 ─ return %25
) => PolarComplex{Float64}

```

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 23, 2019, 9:48am UTC](https://discourse.julialang.org/t/alternative-complex-type/26668/4 "2019-07-23T09:48:48Z")

</div>

This is only because of the `mod(θ,2π)` call (it would probably be more exact to use `mod2pi(θ)` here instead). You could leave this out instead, but if you’re doing a lot of operations, it could accumulate quite quickly and result in a loss of precision.

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [July 23, 2019, 1:58pm UTC](https://discourse.julialang.org/t/alternative-complex-type/26668/5 "2019-07-23T13:58:52Z")

</div>

You could remove it yes, I mainly put it there because otherwise my assertion at the end was failing.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 23, 2019, 2:02pm UTC](https://discourse.julialang.org/t/alternative-complex-type/26668/6 "2019-07-23T14:02:05Z")

</div>

You can always overload the `==` operator to do that, but if you’re calling this method quite often, it’s probably better to use `mod2pi` in the constructor.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [August 19, 2019, 4:05am UTC](https://discourse.julialang.org/t/alternative-complex-type/26668/7 "2019-08-19T04:05:58Z")

</div>

Something that might make this implementation better is to make the `Pi` part of the radius implicit. This would make it better able to represent angles that are multiples of `Pi`.
