# How to overload Base.Complex?

**URL:** <https://discourse.julialang.org/t/how-to-overload-base-complex/91128>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 2, 2022, 6:28am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128 "2022-12-02T06:28:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 2, 2022, 6:28am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/1 "2022-12-02T06:28:29Z")

</div>

```julia
import Base: Complex

struct Complex{T}
    re::T
    im::T
end

```

gives

```julia
cannot assign a value to variable Base.Complex from module Main

Stacktrace:
 [1] top-level scope
   @ In[6]:1
 [2] eval
   @ .\boot.jl:360 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base .\loading.jl:1116

```

Thanks.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 2, 2022, 6:40am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/2 "2022-12-02T06:40:28Z")

</div>

You can overload functions, but there’s no concept of overloading concrete types.

What are you trying to do here? Maybe if you elaborate your intent, we can find another way.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 2, 2022, 6:42am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/3 "2022-12-02T06:42:55Z")

</div>

Just want to define complex types over more general number types than real numbers, because the original definition in Base is

```julia
struct Complex{T <: Real}
    re::T
    im::T
end

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 2, 2022, 6:48am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/4 "2022-12-02T06:48:52Z")

</div>

Is there a new type of complex number that doesn’t fit [this description](https://en.wikipedia.org/wiki/Complex_number) from Wikipedia?

> In [mathematics](https://en.wikipedia.org/wiki/Mathematics), a **complex number** is an element of a [number system](https://en.wikipedia.org/wiki/Number_system) that extends the [real numbers](https://en.wikipedia.org/wiki/Real_number) with a specific element denoted i, called the [imaginary unit](https://en.wikipedia.org/wiki/Imaginary_unit) and satisfying the [equation](https://en.wikipedia.org/wiki/Equation) i^{2}=-1; every complex number can be expressed in the form a+bi, where a and b are real numbers.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 2, 2022, 6:59am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/5 "2022-12-02T06:59:53Z")

</div>

How if I want to have a complex over tuples of real numbers?

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 2, 2022, 7:03am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/6 "2022-12-02T07:03:50Z")

</div>

So if I understand you correctly, you have a tuple `(a, b)` representing a+bi, and would like it to become a `Complex(a, b)`?

If so:

```julia
julia> my_t = (1,2)
(1, 2)

julia> Complex(my_t...)
1 + 2im

```

The `Complex` constructor can be overloaded, allowing tuples to be passed directly into the constructor:

```julia
julia> Base.Complex(t::Tuple{Real,Real}) = Complex(t...)

julia> Complex(my_t)
1 + 2im

```

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 2, 2022, 7:06am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/7 "2022-12-02T07:06:18Z")

</div>

No. I mean I want to have something like

```julia
Complex(Tuple{Float,Float},Tuple{Float,Float})

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 2, 2022, 7:09am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/8 "2022-12-02T07:09:04Z")

</div>

What would the meaning of such a thing be?

Is each `Tuple` representing a single real number, for example by mantissa and exponent?

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 2, 2022, 7:09am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/9 "2022-12-02T07:09:25Z")

</div>

More precisely, what I want is

```julia
Complex(NTuple{N,Float},NTuple{N,Float})

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 2, 2022, 7:12am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/10 "2022-12-02T07:12:07Z")

</div>

Okay. So this is basically a representation for a sequence of complex numbers, if I understand correctly?

What’s the reason for not re-packaging these numbers into a `Vector{ComplexF64}`?

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 2, 2022, 7:13am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/11 "2022-12-02T07:13:37Z")

</div>

For performance reasons (like in StructArrays.jl, but want to have my own realization)

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 2, 2022, 7:22am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/12 "2022-12-02T07:22:24Z")

</div>

I think you mean [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl)?

Is there a reason not to simply construct a tuple (or static array) of `ComplexF64`s, e.g. `NTuple{N,ComplexF64}`? Why split apart the real and imaginary components into separate sections of memory?

The main reason for using a `Complex` type is so that the arithmetic operations are defined. If you aren’t planning on using arithmetic, then the representation becomes more arbitrary.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 2, 2022, 7:29am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/13 "2022-12-02T07:29:55Z")

</div>

Can you follow the same approach as StructArrays do? They don’t need to redefine any type like this.  
Also, why don’t StructArrays work for you as-is? Using StaticArrays within StructArrays would give the same memory layout as the suggested tuple-in-complex type.

If you **really** want a complex-of-tuple type, just define it without importing from Base. It would be your own type that is free to change in arbitrary ways, and free to store anything inside.

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [December 2, 2022, 7:39am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/14 "2022-12-02T07:39:10Z")

</div>

You could have a look at [Quaternions.jl](https://github.com/JuliaGeometry/Quaternions.jl). They contain 4 real values. It seems that you want to start from scratch for something new with 4 reals, and the Quaternions package can perhaps give some ideas, how to do this in Julia.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 2, 2022, 8:02am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/15 "2022-12-02T08:02:59Z")

</div>

Thank you all, I will consider your suggestions.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 2, 2022, 8:10am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/16 "2022-12-02T08:10:19Z")

</div>

Anyway, just curious about why struct cannot be overloaded, because even const can be overloaded

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [December 2, 2022, 10:53am UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/17 "2022-12-02T10:53:26Z")

</div>

Structs are useful since the compiler can know how their data is best represented in memory (assuming they are type safe). If you could change the definition of a struct at any time then the compiler will have a hard time figuring this out. I think in theory it might be possible to change the definition of a struct (since you can also change a function definition), but it will probably make compilation slower.  
By the way, constants cannot be “overloaded”, at least not in the way you want to change the struct, i.e. changing the type.

```julia
julia> const a = 1
1

julia> const a = "test"
ERROR: invalid redefinition of constant a
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 2, 2022, 12:35pm UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/18 "2022-12-02T12:35:43Z")

</div>

> [@photor](#):
>
> Anyway, just curious about why struct cannot be overloaded, because even const can be overloaded

I think you’re confusing overloading with subtyping?

i.e. it sounds like you want a new subtype of `Complex`, but in Julia all concrete types are final for performance reasons (because it allows the compiler to specialize not only their memory layout but also to perform aggressive devirtualization / elimination of dynamic dispatch; this is also why [C++ added a `final` keyword](https://devblogs.microsoft.com/cppblog/the-performance-benefits-of-final-classes/)).

Of course, you are free to define your _own_ type called `Complex` in your own module, but then it will be a completely distinct type that happens to share the same name, and won’t work with any functions or types that use `Base.Complex`.

---

<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 2, 2022, 1:07pm UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/19 "2022-12-02T13:07:29Z")

</div>

If I understand correctly, the optimization you want, is a ‘column-store’ of complex numbers i.e. a vector of complex numbers for which the memory layout has the real parts continuous in memory, and then the imaginary parts continuous in memory.

Is this the goal?

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 2, 2022, 3:18pm UTC](https://discourse.julialang.org/t/how-to-overload-base-complex/91128/20 "2022-12-02T15:18:23Z")

</div>

Yes, exactly, for simd optimization, just like StructArrays.jl.

[Next page](https://discourse.julialang.org/t/how-to-overload-base-complex/91128.md?page=2)
