import Base: Complex
struct Complex{T}
re::T
im::T
end
gives
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
Is there a new type of complex number that doesnât fit this description from Wikipedia?
In mathematics, a complex number is an element of a number system that extends the real numbers with a specific element denoted i, called the imaginary unit and satisfying the equationi^{2}=-1; every complex number can be expressed in the form a+bi, where a and b are real numbers.
Is there a reason not to simply construct a tuple (or static array) of ComplexF64s, 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.
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.
You could have a look at 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.
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> const a = 1
1
julia> const a = "test"
ERROR: invalid redefinition of constant a
Stacktrace:
[1] top-level scope
@ REPL[2]:1
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).
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.
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.