I’ve been working kind of stealth on a Julia package to try and draw people into the language. The field I’m toying in heavily involves complex arithematic and when it doesn’t it’s still very convenient to sometimes represent real vectors as complex ones. I’d like to start off by saying I sincerely appreciate how complex numbers aren’t second class citizens in base julia, every operation I want to perform just works. Thank you for that.

I was wondering if I could start a discussion on whether it makes sense to add some additional wrapper functions. For example I often find myself doing things like the following:

For Matrices/>1-tensors this is a slightly more interesting problem. For example say we have a 2-tensor with complex numbers but we want to decompose it into it’s real and imaginary parts for certain operations. It would increase the tensor order by 1.

Not sure 100% where I am going with this but for some operations convenient decomposition of complex numbers to their real and imaginary parts into vector constituents is really handy. Doing so with the real/imag operators is great, but, can be a little lack-luster.

I have other suggestions but want to see how this goes first,

If I get it correctly, I think what you are suggesting is already provided by https://github.com/piever/StructArrays.jl (for general structs, not just Complex Numbers).

@favba - interesting package. Although I do appreciate this solution, I was moreso hoping for changes to Julia’s base functionality rather then importing a package to conveniently do fundamental mathematical operations.

Depending on ones view, complex numbers naturally represent a vector with 2 elements. So it seems convenient, atleast to me, to include wrappers for these types of interchanges. Or maybe the lack of perspective is on my part.

Edit - If other’s agree I can draft some boilerplate generic code for what I am talking about. I would of course prefer other users working in this domain’s input because I’d hate to waste effort and make something other’s don’t like.

Woah, now that’s a very interesting solution. It doesn’t treat the real and imaginary parts as orthogonal, but very interesting way to go around it. Evens and Odds would designate whether or not something is real or imaginary. Then maybe pass by reference to a different array structure? Hm…

Or, something like this?

z = rand(ComplexF64, 4)
New = permutedims(reshape( reinterpret( Float64, z) , 2, : ), (2, 1))

I don’t see a keyword argument for reshape to go by rows/cols. That might be a nice way around the final permuteddims statement.

That’s because reshape also avoids a copy if it can, which means that it is constrained by Julia’s underlying column-major memory layout. You can use transpose to get an in-place view.

For example, the matrix X below is an in-place view of the same data as the complex array z:

I see, I presumed incorrectly permuteddims was also a view.

I’m scratching my head trying to work out how to do this with say a Matrix, or a tensor. Tried a few naive things without success. Is there a more generic version of a Transpose that wraps positions? The code for how Transpose works is a bit hard to eyeball.